Skip to content

Random SyntaxError when running in browser mode + SWC plugin #6857

@todorgrigorov

Description

@todorgrigorov

Describe the bug

When I run the vitest command I get the following error:

Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Error:   × Expected ';', '}' or <eof>
   ╭─[/__vitest_test__/__test__/3f88ef57-aceb-412a-b54a-57248d1f6580/%2FUsers%2Ftgrigorov%2FCode%2Fmy-vite-app%2Fsrc%2FApp.test.tsx:2:1]
 1 │ 
 2 │       html {
   ·       ──┬─ ─
   ·         ╰── This is the expression part of an expression statement
 3 │         padding: 0;
 4 │         margin: 0;
 5 │       }
   ╰────


Caused by:
    Syntax Error
1  |  
2  |        html {
   |            ^
3  |          padding: 0;
4  |          margin: 0;
// vite.config.ts
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";

// https://vite.dev/config/
export default defineConfig({
  plugins: [react()],
  test: {
    globals: true,
    browser: {
      enabled: true,
      name: "chromium",
      headless: true,
      provider: "playwright",
      screenshotFailures: false,
    },
  },
});
// App.test.tsx
import {test} from 'vitest'
import { render } from 'vitest-browser-react';
import App from './App';

test('App', () => {
	render(<App />)
})

Any help on debugging/mitigation would be appreciated!

Reproduction

  1. Clone https://github.com/todorgrigorov/vitest-browser-bug
  2. npm i
  3. npx vitest
  4. Observe error in browser/terminal

System Info

System:
    OS: macOS 15.0.1
    CPU: (11) arm64 Apple M3 Pro
    Memory: 107.03 MB / 18.00 GB
    Shell: 5.9 - /bin/zsh
Binaries:
    Node: 20.17.0 - ~/.nvm/versions/node/v20.17.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v20.17.0/bin/yarn
    npm: 10.8.2 - ~/.nvm/versions/node/v20.17.0/bin/npm
    pnpm: 9.12.0 - /opt/homebrew/bin/pnpm
Browsers:
    Brave Browser: 129.1.70.119
    Chrome: 130.0.6723.92
    Safari: 18.0.1
npmPackages:
    @vitejs/plugin-react-swc: ^3.5.0 => 3.7.1 
    @vitest/browser: ^2.1.4 => 2.1.4 
    vite: ^5.4.10 => 5.4.10 
    vitest: ^2.1.4 => 2.1.4

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions