-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Describe the bug
See test case in #9617 or https://stackblitz.com/edit/vitest-dev-vitest-oqrkbbdz?file=test%2Fsyntax-error.test.js
import * as dep from './syntax-error.js'; // file content is "bad code"
vi.mock('./syntax-error.js', () => {
return { mocked: 'ok' };
});
test('can mock invalid module', () => {
expect(dep).toMatchObject({ mocked: 'ok' });
});❯ vitest
DEV v4.1.0-beta.3 /home/projects/vitest-dev-vitest-oqrkbbdz
❯ test/syntax-error.test.js (0 test)
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
FAIL test/syntax-error.test.js [ test/syntax-error.test.js ]
RollupError: Parse failure: Expected ';', '}' or <eof>
At file: /test/syntax-error.js:1:4
File: /test/syntax-error.js:1:4
1 | bad code
| ^
❯ getRollupError node_modules/rollup/dist/es/shared/parseAst.js:571:41
❯ convertProgram node_modules/rollup/dist/es/shared/parseAst.js:1277:26
❯ Module.parseAstAsync node_modules/rollup/dist/es/shared/parseAst.js:2263:106
❯ ssrTransformScript node_modules/vite/dist/node/chunks/config.js:15473:9
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯
Test Files 1 failed (1)
Tests no tests
Start at 13:54:30
Duration 630ms (transform 25ms, setup 0ms, import 0ms, tests 0ms, environment 0ms)From what I dug so far, I found that this early return for manual mocking isn't happening:
vitest/packages/vitest/src/runtime/moduleRunner/startVitestModuleRunner.ts
Lines 106 to 109 in c8614f6
| const resolvedMock = moduleRunner.mocker.getDependencyMock(rawId) | |
| if (resolvedMock?.type === 'manual' || resolvedMock?.type === 'redirect') { | |
| return { | |
| code: '', |
because at this point rawId = /test/syntax-error.js (url from project root) and registry.getById expects absolute path. I changed it here to registry.get (which uses registryByUrl) and the reproduction is fixed but I found some other test cases got broken (seems like importOriginal got broken).
vitest/packages/vitest/src/runtime/moduleRunner/bareModuleMocker.ts
Lines 190 to 193 in c8614f6
| public getDependencyMock(id: string): MockedModule | undefined { | |
| const registry = this.getMockerRegistry() | |
| return registry.getById(fixLeadingSlashes(id)) | |
| } |
Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-oqrkbbdz?file=test%2Fsyntax-error.test.js
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: 20.19.1 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 10.8.2 - /usr/local/bin/npm
pnpm: 8.15.6 - /usr/local/bin/pnpm
npmPackages:
vitest: beta => 4.1.0-beta.3Used Package Manager
npm
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.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status