It is a file where you place your logic that you want to run before each test file. You can add your Testing Hooks over here and Mocks, and spies that you want to reuse across the codebase. This is a good place to add cleanup function from React Testing Library, reset your handlers from MSW.
import 'dotenv/config'
import './db-setup.ts'
import '#app/utils/env.server.ts'
import '@testing-library/jest-dom/vitest'
import { installGlobals } from '@remix-run/node'
import { cleanup } from '@testing-library/react'
import { afterEach, beforeEach, vi, type SpyInstance } from 'vitest'
import { server } from '../mocks/index.ts'
import './custom-matchers.ts'
installGlobals()
afterEach(() => server.resetHandlers())
afterEach(() => cleanup())
export let consoleError: SpyInstance<Parameters<typeof console.error>>
beforeEach(() => {
const originalConsoleError = console.error
consoleError = vi.spyOn(console, 'error')
consoleError.mockImplementation(
(...args: Parameters<typeof console.error>) => {
originalConsoleError(...args)
throw new Error(
'Console error was called. Call consoleError.mockImplementation(() => {}) if this is expected.',
)
},
)
})