vitest: v1.1.2 Causes "Expression expected" errors
Describe the bug
After upgrading from v1.1.1 to v1.1.2 all my tests are failing with:
Error: Expression expected
❯ error node_modules/.pnpm/rollup@4.9.2/node_modules/rollup/dist/es/shared/parseAst.js:337:30
❯ nodeConverters node_modules/.pnpm/rollup@4.9.2/node_modules/rollup/dist/es/shared/parseAst.js:2084:9
❯ convertNode node_modules/.pnpm/rollup@4.9.2/node_modules/rollup/dist/es/shared/parseAst.js:969:12
❯ convertProgram node_modules/.pnpm/rollup@4.9.2/node_modules/rollup/dist/es/shared/parseAst.js:960:48
❯ parseAstAsync node_modules/.pnpm/rollup@4.9.2/node_modules/rollup/dist/es/shared/parseAst.js:2150:20
❯ ssrTransformScript node_modules/.pnpm/vite@5.0.10_@types+node@20.10.6/node_modules/vite/dist/node/chunks/dep-R0I0XnyH.js:49846:15
❯ loadAndTransform node_modules/.pnpm/vite@5.0.10_@types+node@20.10.6/node_modules/vite/dist/node/chunks/dep-R0I0XnyH.js:49434:11
It seems to happen if you have:
- A setup file
- That setup file imports something from another file
- You pass that imported data to a
mockImplementation
method
Really weird…
Reproduction
Was pretty easy to create a repro:
https://stackblitz.com/edit/vitest-dev-vitest-3w2rlj?file=setupFile.ts
System Info
System:
OS: macOS 14.1.2
CPU: (8) arm64 Apple M1 Pro
Memory: 426.56 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.3.1 - ~/.nvm/versions/node/v20.3.1/bin/node
Yarn: 1.22.19 - ~/.nvm/versions/node/v20.3.1/bin/yarn
npm: 9.8.0 - ~/.nvm/versions/node/v20.3.1/bin/npm
pnpm: 8.10.4 - ~/.nvm/versions/node/v20.3.1/bin/pnpm
bun: 1.0.18 - ~/.bun/bin/bun
Browsers:
Chrome: 120.0.6099.129
Chrome Canary: 122.0.6225.0
Edge: 120.0.2210.91
Safari: 17.1.2
npmPackages:
@vitejs/plugin-react: 4.2.1 => 4.2.1
@vitest/coverage-v8: 1.1.2 => 1.1.2
vite: 5.0.10 => 5.0.10
vitest: 1.1.2 => 1.1.2
Used Package Manager
pnpm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
About this issue
- Original URL
- State: closed
- Created 6 months ago
- Reactions: 2
- Comments: 15 (9 by maintainers)
Getting the same error for v1.2.1:
Error: Expression expected ❯ error node_modules/rollup/dist/es/shared/parseAst.js:337:30 ❯ nodeConverters node_modules/rollup/dist/es/shared/parseAst.js:2084:9 ❯ convertNode node_modules/rollup/dist/es/shared/parseAst.js:969:12 ❯ convertProgram node_modules/rollup/dist/es/shared/parseAst.js:960:48 ❯ parseAstAsync node_modules/rollup/dist/es/shared/parseAst.js:2150:20 ❯ ssrTransformScript node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:50201:15
Yes, you can just remove
vi.hoisted
wrapper there. I am preparing a PR to throw a syntax error with a more clear error message in this case.It doesn’t matter where you put it, it is always hoisted.
This is not a bug, you import
npmlog
insidesilence-logging.ts
which is imported as a setup file before the test starts running, so it might not have an effect if it was imported inside another file that is not mocked. You need to follow the instructions in the error message.