-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Describe the bug
In vitest 4.0.16 browser mode (@vitest/browser + Playwright runner), when a raw Symbol value is passed directly as a console.log / console.error argument, the baseFormat function in @vitest/utils throws a TypeError.
The root cause is that baseFormat uses str += \${x}`for non-object trailing arguments. JavaScript does not allow implicit string conversion of Symbols via template literals, so this throwsTypeError: Cannot convert a Symbol value to a string`.
// DOES crash in vitest 4.0.16
console.log('value:', Symbol('test'))
// → TypeError: Cannot convert a Symbol value to a string
// Does NOT crash (Symbol as object key is handled differently)
console.error('Test error:', { status: 400, [Symbol('rtk-query')]: { type: 'error' } })This is a regression from 4.0.15.
Reproduction
https://github.com/Suto-Michimasa/vitest-symbol-repro
Steps to reproduce
git clone https://github.com/Suto-Michimasa/vitest-symbol-repro
cd vitest-symbol-repro
pnpm install
pnpm test:browserStack trace
TypeError: Cannot convert a Symbol value to a string
at baseFormat (@vitest/utils@4.0.16/dist/display.js:700:15)
at browserFormat (@vitest/utils@4.0.16/dist/display.js:711:9)
at processLog (__vitest_browser__/tester.js:1202:10)
at console.error (__vitest_browser__/tester.js:1198:23)
Root cause
In @vitest/utils baseFormat function:
// display.js:700 (simplified)
str += `${x}` // Throws TypeError when x is a SymbolThe fix should use String(x) instead of template literal interpolation, as String(Symbol('test')) safely returns "Symbol(test)".
Regression confirmation
Downgrading to 4.0.15 resolves the issue:
pnpm up vitest@4.0.15 @vitest/browser@4.0.15 @vitest/browser-playwright@4.0.15
pnpm test:browser # passesExpected behavior
- Raw Symbol values should be safely handled during console output formatting (e.g., converted via
String(symbol)) - Tests should pass without TypeError
Actual behavior
- Tests fail with
TypeError: Cannot convert a Symbol value to a string - The browser runner crashes when formatting raw Symbol values in console arguments
System Info
- vitest: 4.0.16
- @vitest/browser: 4.0.16
- @vitest/browser-playwright: 4.0.16
- playwright: 1.57.0
- Browser: Chromium (via Playwright)
- Node.js: 20+
- OS: macOS
Used Package Manager
pnpm
Validations
- Check that there isn't already an issue that reports the same bug
- The provided reproduction is a minimal reproducible example