Closed rikisamurai closed 1 month ago
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.
You cannot use
'vitest-browser-react'
outside Vitest Browser mode. In thevitest.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:
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:
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
@rallets in your case there are browser tests included by this glob:
// Non-Browser mode tests are here: test: { include: ['src/**/*.test.ts'],
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?
Oh right, there is *.ts
and *.tsx
extensions. It is working correctly in your setup.
It now throws a useful error in 2.1.2. The "create" script also now comments on the old config. You can uncomment it to continue running Node.js tests, but they won't intervene with browser ones anymore.
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
System Info
Used Package Manager
npm
Validations