Skip to content

mocking module with factory function transforms the original module #9622

@hi-ogawa

Description

@hi-ogawa

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:

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).

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.3

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

Labels

p3-minor-bugAn edge case that only affects very specific usage (priority)

Type

Projects

Status

Approved

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions