forked from Mxmilu666/frp
test(web): add frontend unit test baseline (#5451)
This commit is contained in:
25
web/test/setup.test.ts
Normal file
25
web/test/setup.test.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const capturedFetch = globalThis.fetch
|
||||
|
||||
describe('unit-test environment', () => {
|
||||
it('blocks real network requests before test modules are evaluated', async () => {
|
||||
await expect(capturedFetch('https://example.invalid/')).rejects.toThrow(
|
||||
'Real network requests are disabled in unit tests',
|
||||
)
|
||||
})
|
||||
|
||||
it('allows a test to replace fetch explicitly', async () => {
|
||||
const replacement = vi.fn(async () => new Response('ok'))
|
||||
vi.stubGlobal('fetch', replacement)
|
||||
|
||||
await expect(fetch('/test-endpoint')).resolves.toBeInstanceOf(Response)
|
||||
expect(replacement).toHaveBeenCalledWith('/test-endpoint')
|
||||
})
|
||||
|
||||
it('restores the network guard after a test replacement', async () => {
|
||||
await expect(fetch('/must-remain-isolated')).rejects.toThrow(
|
||||
'Real network requests are disabled in unit tests',
|
||||
)
|
||||
})
|
||||
})
|
||||
25
web/test/setup.ts
Normal file
25
web/test/setup.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { enableAutoUnmount } from '@vue/test-utils'
|
||||
import { afterAll, afterEach, vi } from 'vitest'
|
||||
|
||||
const originalFetch = globalThis.fetch
|
||||
const blockedFetch = vi.fn(async () => {
|
||||
throw new Error('Real network requests are disabled in unit tests')
|
||||
})
|
||||
|
||||
globalThis.fetch = blockedFetch as typeof fetch
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
vi.unstubAllGlobals()
|
||||
vi.clearAllMocks()
|
||||
globalThis.fetch = blockedFetch as typeof fetch
|
||||
document.body.innerHTML = ''
|
||||
})
|
||||
|
||||
// Vitest runs afterEach hooks in reverse registration order, so wrappers are
|
||||
// unmounted before the cleanup hook above clears the DOM.
|
||||
enableAutoUnmount(afterEach)
|
||||
|
||||
afterAll(() => {
|
||||
globalThis.fetch = originalFetch
|
||||
})
|
||||
Reference in New Issue
Block a user