From 67fb56b706e071d58f6fb9ec1befd45ecb4729f1 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Wed, 25 Sep 2024 12:21:36 +0200 Subject: [PATCH] fix(browser): throw an error if "@vitest/browser/context" is imported outside of the browser mode --- packages/browser/context.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/browser/context.js b/packages/browser/context.js index 97779fdf5917c..dad0fea7e1313 100644 --- a/packages/browser/context.js +++ b/packages/browser/context.js @@ -1,2 +1,18 @@ -// empty file to not break bundling // Vitest resolves "@vitest/browser/context" as a virtual module instead + +// fake exports for static analysis +export const page = null +export const server = null +export const userEvent = null +export const cdp = null +export const commands = null + +const pool = globalThis.__vitest_worker__?.ctx?.pool + +throw new Error( + // eslint-disable-next-line prefer-template + '@vitest/browser/context can be imported only inside the Browser Mode. ' + + (pool + ? `Your test is running in ${pool} pool. Make sure your regular tests are excluded from the "test.include" glob pattern.` + : 'Instead, it was imported outside of Vitest.'), +)