From 72c35c40f03da78ea5073dd805a144ce59003874 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Tue, 25 Apr 2023 10:54:01 +0200 Subject: [PATCH 1/2] fix: throw an error if Vitest cannot access its internal state --- 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..4c7864f722ca 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 (to fix this, run "npx vitest")' + + '\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, From 30bb3268aeadc8224eaf71c4546d1877bd48bea5 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Thu, 27 Apr 2023 14:32:29 +0200 Subject: [PATCH 2/2] chore: remove misleading note --- packages/vitest/src/integrations/vi.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vitest/src/integrations/vi.ts b/packages/vitest/src/integrations/vi.ts index 4c7864f722ca..bd4d9517cfc3 100644 --- a/packages/vitest/src/integrations/vi.ts +++ b/packages/vitest/src/integrations/vi.ts @@ -172,7 +172,7 @@ function createVitest(): VitestUtils { 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 (to fix this, run "npx vitest")' + + '\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)