diff --git a/packages/vitest/src/integrations/vi.ts b/packages/vitest/src/integrations/vi.ts index 1666909a934f..bd4d9517cfc3 100644 --- a/packages/vitest/src/integrations/vi.ts +++ b/packages/vitest/src/integrations/vi.ts @@ -168,6 +168,16 @@ function createVitest(): VitestUtils { let _config: null | ResolvedConfig = null const workerState = getWorkerState() + + if (!workerState) { + const errorMsg = 'Vitest failed to access its internal state.' + + '\n\nOne of the following is possible:' + + '\n- "vitest" is imported directly without running "vitest" command' + + '\n- "vitest" is imported inside "globalSetup" (to fix this, use "setupFiles" instead, because "globalSetup" runs in a different context)' + + '\n- Otherwise, it might be a Vitest bug. Please report it to https://github.com/vitest-dev/vitest/issues\n' + throw new Error(errorMsg) + } + const _timers = new FakeTimers({ global: globalThis, config: workerState.config.fakeTimers,