mirror of
https://github.com/vernu/textbee.git
synced 2026-07-31 01:17:52 -04:00
Establish the testing safety net for the web dashboard before refactoring. - Vitest + React Testing Library + jsdom for unit/component tests - MSW node server mocks the API; unhandled requests throw so tests can never reach a real backend - Playwright e2e boots the real Next app but intercepts every /api/v1 call with shared fixtures, and mints a NextAuth session cookie to run authed - Shared fixtures in test/fixtures.ts back both layers - Seed tests: Button unit tests, MSW sanity checks, and a dashboard e2e happy path plus an auth-guard redirect Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
59 lines
1.8 KiB
TypeScript
59 lines
1.8 KiB
TypeScript
import '@testing-library/jest-dom/vitest'
|
|
import { cleanup } from '@testing-library/react'
|
|
import { afterAll, afterEach, beforeAll, vi } from 'vitest'
|
|
import { server } from './msw/server'
|
|
|
|
// The axios browser client's interceptor calls next-auth's getSession(), which
|
|
// would otherwise trigger a real /api/auth/session fetch. Mock it so the
|
|
// interceptor can attach the Bearer token without any network access.
|
|
const hoisted = vi.hoisted(() => ({ accessToken: 'test-access-token' }))
|
|
vi.mock('next-auth/react', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('next-auth/react')>()
|
|
return {
|
|
...actual,
|
|
getSession: async () => ({
|
|
user: {
|
|
id: 'user_test_1',
|
|
name: 'Test User',
|
|
email: 'test.user@example.com',
|
|
accessToken: hoisted.accessToken,
|
|
},
|
|
expires: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
|
|
}),
|
|
}
|
|
})
|
|
|
|
// Fail loudly on any request that is not explicitly mocked. This guarantees a
|
|
// test can never silently fall through to a real backend.
|
|
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }))
|
|
afterEach(() => {
|
|
cleanup()
|
|
server.resetHandlers()
|
|
})
|
|
afterAll(() => server.close())
|
|
|
|
// jsdom lacks these browser APIs that Radix UI / next-themes rely on.
|
|
if (!window.matchMedia) {
|
|
window.matchMedia = vi.fn().mockImplementation((query: string) => ({
|
|
matches: false,
|
|
media: query,
|
|
onchange: null,
|
|
addEventListener: vi.fn(),
|
|
removeEventListener: vi.fn(),
|
|
addListener: vi.fn(),
|
|
removeListener: vi.fn(),
|
|
dispatchEvent: vi.fn(),
|
|
}))
|
|
}
|
|
|
|
class ResizeObserverMock {
|
|
observe() {}
|
|
unobserve() {}
|
|
disconnect() {}
|
|
}
|
|
window.ResizeObserver = window.ResizeObserver || (ResizeObserverMock as any)
|
|
|
|
if (!window.HTMLElement.prototype.scrollIntoView) {
|
|
window.HTMLElement.prototype.scrollIntoView = vi.fn()
|
|
}
|