Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(browser): throw an error if "@vitest/browser/context" is imported outside of the browser mode #6570

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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