Can I declare a setup file for a specific test or test file? #5673
-
I have the need to utilize different MSW server's depending on the test that is running. import { errorServer } from '@/mocks/msw/server'
import { afterAll, afterEach, beforeAll, vi } from 'vitest'
beforeAll(() => {
// Mocking new Date()
vi.useFakeTimers()
vi.setSystemTime(new Date('2024-05-03T11:00:00Z'))
// Establish API mocking before all tests.
errorServer.listen()
})
afterEach(() => errorServer.resetHandlers())
afterAll(() => {
vi.useRealTimers()
errorServer.close()
}) But I only want this particular file when testing against error states and have a separate setup file for non-error-state-related tests. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can extract // helper.ts
import { errorServer } from '@/mocks/msw/server'
import { afterAll, afterEach, beforeAll, vi } from 'vitest'
export function setupErrorServer() {
beforeAll(() => ... );
afterEach(() => ...);
afterAll(() => ...);
}
// some-file.test.ts
import { test } "vitest"
import { setupErrorServer } from "./helper"
setupErrorServer();
test("something", () => {
...
}) If you want to do Vitest magic, then you could technically check current file inside // my-setup.ts
beforeAll(ctx => { ctx.file; }) or you could use Vitest workspace https://vitest.dev/guide/workspace.html to have a completely separate |
Beta Was this translation helpful? Give feedback.
You can extract
beforeAll
etc... as a utility and ditchsetupFiles
completelyIf you want to do Vitest magic, then you could technically check current file inside
setupFiles
:or you could use Vitest workspace https://vitest.dev/guide/workspace.html to have a completel…