From 383f17916608768858beafcbfcce645190ca4ec8 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Wed, 2 Oct 2024 18:17:36 +0200 Subject: [PATCH] fix(browser): throw an error if "@vitest/browser/context" is imported outside of the browser mode (#6570) --- packages/browser/context.js | 18 +++++++++++++++++- packages/vitest/src/create/browser/creator.ts | 7 ++----- .../fails/node-browser-context.test.ts | 3 +++ test/cli/test/__snapshots__/fails.test.ts.snap | 2 ++ 4 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 test/cli/fixtures/fails/node-browser-context.test.ts diff --git a/packages/browser/context.js b/packages/browser/context.js index 97779fdf5917..dad0fea7e131 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.'), +) diff --git a/packages/vitest/src/create/browser/creator.ts b/packages/vitest/src/create/browser/creator.ts index be0d20a04559..6ebe00ac756c 100644 --- a/packages/vitest/src/create/browser/creator.ts +++ b/packages/vitest/src/create/browser/creator.ts @@ -258,11 +258,8 @@ async function generateWorkspaceFile(options: { `import { defineWorkspace } from 'vitest/config'`, '', 'export default defineWorkspace([', - ' // This will keep running your existing tests.', - ' // If you don\'t need to run those in Node.js anymore,', - ' // You can safely remove it from the workspace file', - ' // Or move the browser test configuration to the config file.', - ` '${relativeRoot}',`, + ' // If you want to keep running your existing tests in Node.js, uncomment the next line.', + ` // '${relativeRoot}',`, ` {`, ` extends: '${relativeRoot}',`, ` test: {`, diff --git a/test/cli/fixtures/fails/node-browser-context.test.ts b/test/cli/fixtures/fails/node-browser-context.test.ts new file mode 100644 index 000000000000..af854dc4012a --- /dev/null +++ b/test/cli/fixtures/fails/node-browser-context.test.ts @@ -0,0 +1,3 @@ +import { page } from '@vitest/browser/context' + +console.log(page) diff --git a/test/cli/test/__snapshots__/fails.test.ts.snap b/test/cli/test/__snapshots__/fails.test.ts.snap index 3ae6946dadf5..2b27922890ed 100644 --- a/test/cli/test/__snapshots__/fails.test.ts.snap +++ b/test/cli/test/__snapshots__/fails.test.ts.snap @@ -58,6 +58,8 @@ exports[`should fail nested-suite.test.ts > nested-suite.test.ts 1`] = `"Asserti exports[`should fail no-assertions.test.ts > no-assertions.test.ts 1`] = `"Error: expected any number of assertion, but got none"`; +exports[`should fail node-browser-context.test.ts > node-browser-context.test.ts 1`] = `"Error: @vitest/browser/context can be imported only inside the Browser Mode. Your test is running in forks pool. Make sure your regular tests are excluded from the "test.include" glob pattern."`; + exports[`should fail primitive-error.test.ts > primitive-error.test.ts 1`] = `"Unknown Error: 42"`; exports[`should fail snapshot-with-not.test.ts > snapshot-with-not.test.ts 1`] = `