Skip to content

Commit 0439989

Browse files
committedFeb 25, 2025·
chore: improve browser test suite
chore: wip chore: wip
1 parent 32e7f8e commit 0439989

File tree

2 files changed

+94
-135
lines changed

2 files changed

+94
-135
lines changed
 

Diff for: ‎test/browser.test.ts

+93-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
11
import { describe, expect, it, mock, spyOn } from 'bun:test'
2-
import { loadConfig } from '../src/browser'
2+
import { isBrowser, loadConfig } from '../src/browser'
33

44
describe('browser', () => {
55
it('should detect browser environment correctly', () => {
6+
// Store original window and fetch
7+
const originalWindow = globalThis.window
8+
const originalFetch = globalThis.fetch
9+
10+
// Test non-browser environment
11+
// @ts-expect-error - mocking window
12+
globalThis.window = undefined
13+
// @ts-expect-error - mocking fetch
14+
globalThis.fetch = undefined
15+
expect(isBrowser()).toBe(false)
16+
17+
// Test browser environment with fetch
18+
// @ts-expect-error - mocking window
19+
globalThis.window = {}
20+
globalThis.fetch = () => Promise.resolve(new Response())
21+
expect(isBrowser()).toBe(true)
22+
23+
// Restore originals
24+
globalThis.window = originalWindow
25+
globalThis.fetch = originalFetch
26+
})
27+
28+
it('should handle browser environment', async () => {
629
// Mock window to simulate browser environment
730
const originalWindow = globalThis.window
831
// @ts-expect-error - mocking window
@@ -18,13 +41,20 @@ describe('browser', () => {
1841
globalThis.fetch = mockFetch
1942

2043
const defaultConfig = { port: 3000, host: 'localhost' }
21-
const result = loadConfig({
44+
const result = await loadConfig({
2245
name: 'test-app',
2346
endpoint: '/api/config',
2447
defaultConfig,
2548
})
2649

27-
expect(result).resolves.toEqual({ port: 3000, host: 'api-host' })
50+
expect(result).toEqual({ port: 3000, host: 'api-host' })
51+
expect(mockFetch).toHaveBeenCalledWith('/api/config', {
52+
method: 'GET',
53+
headers: {
54+
'Accept': 'application/json',
55+
'Content-Type': 'application/json',
56+
},
57+
})
2858

2959
// Restore window
3060
globalThis.window = originalWindow
@@ -38,6 +68,7 @@ describe('browser', () => {
3868

3969
const consoleSpy = spyOn(console, 'warn')
4070
const defaultConfig = { port: 3000, host: 'localhost' }
71+
4172
const result = await loadConfig({
4273
name: 'test-app',
4374
defaultConfig,
@@ -51,6 +82,62 @@ describe('browser', () => {
5182
consoleSpy.mockRestore()
5283
})
5384

85+
it('should handle browser fetch errors', async () => {
86+
// Mock window to simulate browser environment
87+
const originalWindow = globalThis.window
88+
// @ts-expect-error - mocking window
89+
globalThis.window = {}
90+
91+
const consoleSpy = spyOn(console, 'error')
92+
const mockFetch = mock(() => Promise.reject(new Error('Network error')))
93+
globalThis.fetch = mockFetch
94+
95+
const defaultConfig = { port: 3000, host: 'localhost' }
96+
const result = await loadConfig({
97+
name: 'test-app',
98+
endpoint: '/api/config',
99+
defaultConfig,
100+
})
101+
102+
expect(result).toEqual(defaultConfig)
103+
expect(consoleSpy).toHaveBeenCalledWith('Failed to load client config:', expect.any(Error))
104+
105+
// Restore window
106+
globalThis.window = originalWindow
107+
consoleSpy.mockRestore()
108+
})
109+
110+
it('should handle non-200 responses', async () => {
111+
// Mock window to simulate browser environment
112+
const originalWindow = globalThis.window
113+
// @ts-expect-error - mocking window
114+
globalThis.window = {}
115+
116+
const consoleSpy = spyOn(console, 'error')
117+
const mockFetch = mock(() =>
118+
Promise.resolve({
119+
ok: false,
120+
status: 404,
121+
}),
122+
)
123+
// @ts-expect-error - mocking fetch
124+
globalThis.fetch = mockFetch
125+
126+
const defaultConfig = { port: 3000, host: 'localhost' }
127+
const result = await loadConfig({
128+
name: 'test-app',
129+
endpoint: '/api/config',
130+
defaultConfig,
131+
})
132+
133+
expect(result).toEqual(defaultConfig)
134+
expect(consoleSpy).toHaveBeenCalledWith('Failed to load client config:', expect.any(Error))
135+
136+
// Restore window
137+
globalThis.window = originalWindow
138+
consoleSpy.mockRestore()
139+
})
140+
54141
it('should handle custom headers correctly', async () => {
55142
// Mock window to simulate browser environment
56143
const originalWindow = globalThis.window
@@ -99,6 +186,7 @@ describe('browser', () => {
99186
// @ts-expect-error - mocking window
100187
globalThis.window = {}
101188

189+
const consoleSpy = spyOn(console, 'error')
102190
const mockFetch = mock(() =>
103191
Promise.resolve({
104192
ok: true,
@@ -116,8 +204,10 @@ describe('browser', () => {
116204
})
117205

118206
expect(result).toEqual(defaultConfig)
207+
expect(consoleSpy).toHaveBeenCalledWith('Failed to load client config:', expect.any(Error))
119208

120209
// Restore window
121210
globalThis.window = originalWindow
211+
consoleSpy.mockRestore()
122212
})
123213
})

Diff for: ‎test/bunfig.test.ts

+1-132
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test'
1+
import { afterEach, beforeEach, describe, expect, it } from 'bun:test'
22
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
33
import { resolve } from 'node:path'
44
import process from 'node:process'
@@ -105,137 +105,6 @@ describe('bunfig', () => {
105105
expect(result).toEqual(defaultConfig)
106106
})
107107

108-
it('should handle browser environment', async () => {
109-
// Mock window to simulate browser environment
110-
const originalWindow = globalThis.window
111-
// @ts-expect-error - mocking window
112-
globalThis.window = {}
113-
114-
const mockFetch = mock(() =>
115-
Promise.resolve({
116-
ok: true,
117-
json: () => Promise.resolve({ host: 'api-host' }),
118-
}),
119-
)
120-
// @ts-expect-error - mocking fetch
121-
globalThis.fetch = mockFetch
122-
123-
const defaultConfig = { port: 3000, host: 'localhost' }
124-
const result = await loadConfig({
125-
name: 'test-app',
126-
endpoint: '/api/config',
127-
defaultConfig,
128-
})
129-
130-
expect(result).toEqual({ port: 3000, host: 'api-host' })
131-
expect(mockFetch).toHaveBeenCalledWith('/api/config', {
132-
method: 'GET',
133-
headers: {
134-
'Accept': 'application/json',
135-
'Content-Type': 'application/json',
136-
},
137-
})
138-
139-
// Restore window
140-
globalThis.window = originalWindow
141-
})
142-
143-
it('should handle browser fetch errors', async () => {
144-
// Mock window to simulate browser environment
145-
const originalWindow = globalThis.window
146-
// @ts-expect-error - mocking window
147-
globalThis.window = {}
148-
149-
const consoleSpy = spyOn(console, 'error')
150-
const mockFetch = mock(() => Promise.reject(new Error('Network error')))
151-
152-
globalThis.fetch = mockFetch
153-
154-
const defaultConfig = { port: 3000, host: 'localhost' }
155-
const result = await loadConfig({
156-
name: 'test-app',
157-
endpoint: '/api/config',
158-
defaultConfig,
159-
})
160-
161-
expect(result).toEqual(defaultConfig)
162-
expect(consoleSpy).toHaveBeenCalledWith('Failed to load client config:', expect.any(Error))
163-
164-
// Restore window
165-
globalThis.window = originalWindow
166-
consoleSpy.mockRestore()
167-
})
168-
169-
it('should handle non-200 responses in browser', async () => {
170-
// Mock window to simulate browser environment
171-
const originalWindow = globalThis.window
172-
// @ts-expect-error - mocking window
173-
globalThis.window = {}
174-
175-
const mockFetch = mock(() =>
176-
Promise.resolve({
177-
ok: false,
178-
status: 404,
179-
}),
180-
)
181-
// @ts-expect-error - mocking fetch
182-
globalThis.fetch = mockFetch
183-
184-
const defaultConfig = { port: 3000, host: 'localhost' }
185-
const result = await loadConfig({
186-
name: 'test-app',
187-
endpoint: '/api/config',
188-
defaultConfig,
189-
})
190-
191-
expect(result).toEqual(defaultConfig)
192-
193-
// Restore window
194-
globalThis.window = originalWindow
195-
})
196-
197-
it('should handle custom headers in browser', async () => {
198-
// Mock window to simulate browser environment
199-
const originalWindow = globalThis.window
200-
// @ts-expect-error - mocking window
201-
globalThis.window = {}
202-
203-
const mockFetch = mock(() =>
204-
Promise.resolve({
205-
ok: true,
206-
json: () => Promise.resolve({ host: 'api-host' }),
207-
}),
208-
)
209-
// @ts-expect-error - mocking fetch
210-
globalThis.fetch = mockFetch
211-
212-
const defaultConfig = { port: 3000, host: 'localhost' }
213-
const customHeaders = {
214-
'Authorization': 'Bearer token',
215-
'X-Custom-Header': 'value',
216-
}
217-
218-
const result = await loadConfig({
219-
name: 'test-app',
220-
endpoint: '/api/config',
221-
headers: customHeaders,
222-
defaultConfig,
223-
})
224-
225-
expect(result).toEqual({ port: 3000, host: 'api-host' })
226-
expect(mockFetch).toHaveBeenCalledWith('/api/config', {
227-
method: 'GET',
228-
headers: {
229-
...customHeaders,
230-
'Accept': 'application/json',
231-
'Content-Type': 'application/json',
232-
},
233-
})
234-
235-
// Restore window
236-
globalThis.window = originalWindow
237-
})
238-
239108
it('should correctly merge nested configuration objects', async () => {
240109
interface DatabaseConfig {
241110
host: string

0 commit comments

Comments
 (0)
Please sign in to comment.