Skip to content

Can I declare a setup file for a specific test or test file? #5673

Answered by hi-ogawa
CodyBontecou asked this question in Q&A
Discussion options

You must be logged in to vote

You can extract beforeAll etc... as a utility and ditch setupFiles completely

// 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 setupFiles:

// my-setup.ts
beforeAll(ctx => { ctx.file; })

or you could use Vitest workspace https://vitest.dev/guide/workspace.html to have a completel…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@CodyBontecou
Comment options

Answer selected by CodyBontecou
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants