Generate a Vitest test file for a given source file, following project testing conventions
Generate a Vitest test file for the specified source file. The test file must be co-located with the source file (e.g., foo.ts -> foo.test.ts).
The user will provide a file path or describe the module to test.
// @vitest-environment jsdom — no blank lines above it@testing-library/react for rendering and querying (render, screen)@testing-library/jest-dom matchers (e.g., toHaveTextContent, toBeInTheDocument)getByRole, getByLabelText) rather than test IDsexport default — import accordinglyExample shape:
// @vitest-environment jsdom
import { render, screen } from '@testing-library/react'
import MyPage from './page'
describe(`MyPage`, () => {
it(`should render the heading`, () => {
render(<MyPage />)
expect(screen.getByRole(`heading`)).toHaveTextContent(`Expected text`)
})
})
src/app/api/)GET, POST, etc.) directly — they return a standard Responsedb instance directly for database assertionsbeforeEach@/db and @/const/... path aliases for importsExample shape:
import { db } from '@/db'
import { myTable } from '@/db/schema'
import { GET } from './route'
beforeEach(async () => {
await db.delete(myTable)
})
describe(`/api/my-endpoint`, () => {
it(`should return success`, async () => {
const response = await GET()
expect(response.status).toBe(200)
expect(await response.json()).toEqual({ success: true })
})
})
describe and it labels (not single/double quotes)it('returns 404 when resource not found')describe block per logical unit; nest when testing sub-behaviors