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

SyntaxError: The requested module '@vitest/browser/context' does not provide an export named 'page' #6525

Open
6 tasks done
rikisamurai opened this issue Sep 18, 2024 · 7 comments
Labels
feat: browser Issues and PRs related to the browser runner p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@rikisamurai
Copy link

rikisamurai commented Sep 18, 2024

Describe the bug

When I try Browser Mode | Guide | Vitest in a react-ts template vite app, I got the error.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-8xj8ao?file=vite.config.ts
run

npm run test:browser
image

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitejs/plugin-react: ^4.3.1 => 4.3.1 
    @vitest/browser: ^2.1.1 => 2.1.1 
    @vitest/ui: latest => 2.1.1 
    vite: latest => 5.4.6 
    vitest: latest => 2.1.1

Used Package Manager

npm

Validations

@AriPerkkio
Copy link
Member

You cannot use 'vitest-browser-react' outside Vitest Browser mode. In the vitest.workspace.ts there is project that's not using Browser Mode, and it's importing test file that's for browser mode:

export default defineWorkspace([
  'vite.config.ts',
  // ^^ This is not browser mode

  {
    extends: 'vite.config.ts',
    test: {
      browser: {
        enabled: true,
        name: 'chromium',
        provider: 'playwright',
        // https://playwright.dev
        providerOptions: {},
      },
    },
  },
]);

Does this work?

export default defineWorkspace([
-  'vite.config.ts',
+  {
+    extends: 'vite.config.ts',
+    // Non-Browser mode tests are here:
+    include: ['tests/**'],
+  },
  {
    extends: 'vite.config.ts',
    test: {
+     // Tests for browser mode are here
+     include: ['vitest-example/**'],
      browser: {
        enabled: true,
        name: 'chromium',
        provider: 'playwright',
        // https://playwright.dev
        providerOptions: {},
      },
    },
  },
]);

I didn't test this but that's my first guess.

@rikisamurai
Copy link
Author

You cannot use 'vitest-browser-react' outside Vitest Browser mode. In the vitest.workspace.ts there is project that's not using Browser Mode, and it's importing test file that's for browser mode:

export default defineWorkspace([
  'vite.config.ts',
  // ^^ This is not browser mode

  {
    extends: 'vite.config.ts',
    test: {
      browser: {
        enabled: true,
        name: 'chromium',
        provider: 'playwright',
        // https://playwright.dev
        providerOptions: {},
      },
    },
  },
]);

Does this work?

export default defineWorkspace([
-  'vite.config.ts',
+  {
+    extends: 'vite.config.ts',
+    // Non-Browser mode tests are here:
+    include: ['tests/**'],
+  },
  {
    extends: 'vite.config.ts',
    test: {
+     // Tests for browser mode are here
+     include: ['vitest-example/**'],
      browser: {
        enabled: true,
        name: 'chromium',
        provider: 'playwright',
        // https://playwright.dev
        providerOptions: {},
      },
    },
  },
]);

I didn't test this but that's my first guess.

Thank you very much! Sorry for the confusion. I used the command npx vitest init browser directly for initialization. However, the official documentation indicates that the node and browser configurations should be separated.

If you need to run some tests using Node-based runner, you can define a workspace file with separate configurations for different testing strategies:

@AriPerkkio
Copy link
Member

I used the command npx vitest init browser directly for initialization.

Looks like there is bug in this command. We need to fix the vitest.workspace.ts that this command creates:

const workspaceContent = [
`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}',`,
` {`,
` extends: '${relativeRoot}',`,
` test: {`,
` browser: {`,
` enabled: true,`,
` name: '${options.browser}',`,
` provider: '${options.provider}',`,
options.provider !== 'preview' && ` // ${getProviderDocsLink(options.provider)}`,
options.provider !== 'preview' && ` providerOptions: {},`,
` },`,
` },`,
` },`,
`])`,
'',
].filter(c => typeof c === 'string').join('\n')

@AriPerkkio AriPerkkio added feat: browser Issues and PRs related to the browser runner p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Sep 18, 2024
@rallets
Copy link

rallets commented Sep 25, 2024

Hi, I also met the same problem. My workspace configuration now looks like this:

// /vitest.workspace.ts
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.

	{
		// Non-Browser mode tests are here:
		test: {
			include: ['src/**/*.test.ts'],
			name: 'unit',
			environment: 'node',
		},
	},
	{
		test: {
			// Tests for browser mode are here
			setupFiles: ['./vitest.setup.ts'],
			include: ['vitest-example/**/*.test.tsx', 'src/**/*.test.tsx'],
			name: 'browser',
			browser: {
				enabled: true,
				name: 'chromium',
				provider: 'playwright',
				// https://playwright.dev
				providerOptions: {
					launch: {
						devtools: true,
					}
				},
			},
		},
	},
])

and vitest.setup.ts

import '@testing-library/jest-dom/vitest'
import { cleanup } from '@testing-library/react'
import { afterEach } from 'vitest'

afterEach(() => {
	cleanup()
})

vitest.setup.ts is important otherwise tests running many times manually from the UI will fail. I think you can also have

afterEach(() => {
	cleanup()
})

in each test file, but I haven't tested it.

A repo with a full setup is available here:
https://github.com/rallets/vite-react-vitest

@AriPerkkio
Copy link
Member

@rallets in your case there are browser tests included by this glob:

// Non-Browser mode tests are here:
test: {
  include: ['src/**/*.test.ts'],

https://github.com/rallets/vite-react-vitest/blob/7a5ba4f20a6f6b9bf47710d033089724593b4e24/src/pages/Form.test.tsx#L2

@rallets
Copy link

rallets commented Sep 25, 2024

Hi @AriPerkkio why are you saying so? That include should state that those tests are "node" tests. Isn't?
My setup works correctly, or did I miss something?

@AriPerkkio
Copy link
Member

AriPerkkio commented Sep 25, 2024

Oh right, there is *.ts and *.tsx extensions. It is working correctly in your setup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: browser Issues and PRs related to the browser runner p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

No branches or pull requests

3 participants