Skip to content

globalSetup

  • Type: string | string[]

Path to global setup files, relative to project root.

A global setup file can either export named functions setup and teardown or a default function that returns a teardown function (example).

INFO

Multiple globalSetup files are possible. setup and teardown are executed sequentially with teardown in reverse order.

WARNING

Global setup runs only if there is at least one running test. This means that global setup might start running during watch mode after test file is changed (the test file will wait for global setup to finish before running).

Beware that the global setup is running in a different global scope, so your tests don't have access to variables defined here. However, you can pass down serializable data to tests via provide method:

ts
import { inject } from 'vitest'

inject('wsPort') === 3000
ts
import type { TestProject } from 'vitest/node'

export default function setup(project: TestProject) {
  project.provide('wsPort', 3000)
}

declare module 'vitest' {
  export interface ProvidedContext {
    wsPort: number
  }
}
ts
import type { GlobalSetupContext } from 'vitest/node'

export default function setup({ provide }: GlobalSetupContext) {
  provide('wsPort', 3000)
}

declare module 'vitest' {
  export interface ProvidedContext {
    wsPort: number
  }
}

Since Vitest 3, you can define a custom callback function to be called when Vitest reruns tests. If the function is asynchronous, the runner will wait for it to complete before executing tests. Note that you cannot destruct the project like { onTestsRerun } because it relies on the context.

globalSetup.ts
ts
import type { TestProject } from 'vitest/node'

export default function setup(project: TestProject) {
  project.onTestsRerun(async () => {
    await restartDb()
  })
}

Released under the MIT License.