Replies: 4 comments 9 replies
-
|
Did you find a way to achieve this? |
Beta Was this translation helpful? Give feedback.
-
Sounds exactly like a use-case for globalSetup. Does that work for you? |
Beta Was this translation helpful? Give feedback.
-
|
Yes, For something like Mongo, I would keep it on a separate integration-test config or Vitest project so unit tests do not pay the startup cost: // vitest.config.ts
import { defineConfig } from "vitest/config"
export default defineConfig({
test: {
include: ["**/*.integration.test.ts"],
globalSetup: ["./test/global-setup.ts"],
},
})// test/global-setup.ts
import type { TestProject } from "vitest/node"
export default async function setup(project: TestProject) {
const mongo = await startMongoSomehow()
project.provide("mongoUri", mongo.uri)
return async () => {
await mongo.stop()
}
}
declare module "vitest" {
export interface ProvidedContext {
mongoUri: string
}
}And in a test: import { inject } from "vitest"
const mongoUri = inject("mongoUri")I would avoid relying on plain globals here because setup runs outside the test file scope. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there,
I was wondering if it's possible to do a global setup for integration tests ONCE only. I've struggled to do this with Jest, as it seems super unreliable to do so.
What I would like to achieve:
Separate unit and integration tests (already done so by giving it a different file name pattern), run the unit tests just as they come, but for the integration tests, run the setup (starting in-memory Mongo, Redis, ...) only once and share the setup and eventually have a global teardown method taking care of the teardown afterwards.
In jest, this is supposed to be possible with testEnvironments, but they never worked for me, so I was wondering whether Vitest supports a similar approach that would enable a one-time setup/teardown (not per file, but for the entire test suite).
Beta Was this translation helpful? Give feedback.
All reactions