Skip to content

Commit

Permalink
fix(browser): throw an error if "@vitest/browser/context" is imported…
Browse files Browse the repository at this point in the history
… outside of the browser mode (#6570)
  • Loading branch information
sheremet-va authored Oct 2, 2024
1 parent 821400b commit 383f179
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
18 changes: 17 additions & 1 deletion packages/browser/context.js
Original file line number Diff line number Diff line change
@@ -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.'),
)
7 changes: 2 additions & 5 deletions packages/vitest/src/create/browser/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {`,
Expand Down
3 changes: 3 additions & 0 deletions test/cli/fixtures/fails/node-browser-context.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { page } from '@vitest/browser/context'

console.log(page)
2 changes: 2 additions & 0 deletions test/cli/test/__snapshots__/fails.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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`] = `
Expand Down

0 comments on commit 383f179

Please sign in to comment.