From fbb1468e90654d0ac5b3eae993db8a436ff4ab5e Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 27 Apr 2023 14:33:00 +0200 Subject: [PATCH] fix: throw an error if Vitest cannot access its internal state (#3250) --- packages/vitest/src/integrations/vi.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) 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,