Skip to content

Test file starting with vi.mock() fails to mock the module #7470

@Redpoint1

Description

@Redpoint1

Describe the bug

If the test file starts with vi.mock(), then the module isn't mocked. Other vi.mock() after the first are not affected, even if they are right after the first one. This issue started with vitest 3.0.0, vitest<3 are not affected.

❯ npm install && npm run test:ui

up to date in 510ms

14 packages are looking for funding
  run `npm fund` for details

> test:ui
> vitest --ui


 DEV  v3.0.5 /home/projects/vitest-dev-vitest-bqqlcrmh
      UI started at http://localhost:51204/__vitest__/

stdout | src/bug.test.js > am i bugged?
{ first: [Function: first] }
{
  second: [Function: second] {
    getMockName: [Function (anonymous)],
    mockName: [Function (anonymous)],
    mockClear: [Function (anonymous)],
    mockReset: [Function (anonymous)],
    mockRestore: [Function (anonymous)],
    getMockImplementation: [Function (anonymous)],
    mockImplementation: [Function (anonymous)],
    mockImplementationOnce: [Function (anonymous)],
    withImplementation: [Function: withImplementation],
    mockReturnThis: [Function (anonymous)],
    mockReturnValue: [Function (anonymous)],
    mockReturnValueOnce: [Function (anonymous)],
    mockResolvedValue: [Function (anonymous)],
    mockResolvedValueOnce: [Function (anonymous)],
    mockRejectedValue: [Function (anonymous)],
    mockRejectedValueOnce: [Function (anonymous)]
  }
}
{
  third: [Function: third] {
    getMockName: [Function (anonymous)],
    mockName: [Function (anonymous)],
    mockClear: [Function (anonymous)],
    mockReset: [Function (anonymous)],
    mockRestore: [Function (anonymous)],
    getMockImplementation: [Function (anonymous)],
    mockImplementation: [Function (anonymous)],
    mockImplementationOnce: [Function (anonymous)],
    withImplementation: [Function: withImplementation],
    mockReturnThis: [Function (anonymous)],
    mockReturnValue: [Function (anonymous)],
    mockReturnValueOnce: [Function (anonymous)],
    mockResolvedValue: [Function (anonymous)],
    mockResolvedValueOnce: [Function (anonymous)],
    mockRejectedValue: [Function (anonymous)],
    mockRejectedValueOnce: [Function (anonymous)]
  }
}

 ❯ src/bug.test.js (1 test | 1 failed) 6ms
   × am i bugged? 6ms
     → expected false to be true // Object.is equality

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  src/bug.test.js > am i bugged?
AssertionError: expected false to be true // Object.is equality

- Expected
+ Received

- true
+ false

 ❯ null.<anonymous> src/bug.test.js:16:42
     14|   expect(vi.isMockFunction(third.third)).toBe(true)
     15|   expect(vi.isMockFunction(second.second)).toBe(true);
     16|   expect(vi.isMockFunction(first.first)).toBe(true);
       |                                          ^
     17| });
     18| 
 ❯ _0x6ed099/< ../../../blitz.7f809d15.js:40:25097
 ❯ _0x6ed099 ../../../blitz.7f809d15.js:40:25059

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯


 Test Files  1 failed (1)
      Tests  1 failed (1)
   Start at  02:49:48
   Duration  2.71s (transform 50ms, setup 0ms, collect 103ms, tests 5ms, environment 0ms, prepare 506ms)

 FAIL  Tests failed. Watching for file changes...
       press h to show help, press q to quit

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-bqqlcrmh?file=src%2Fbug.test.js,src%2Fmockmefirst.js,src%2Fgood.test.js&initialPath=__vitest__/

By changing the order of the vi.mock() in bug.test.js, you can see in console.log that only the first one is not mocked each time.

In good.test.js i moved the vi.mock() after the imports and all of them are mocked properly.

System Info

System:
    OS: Linux 6.8 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
    Memory: 5.06 GB / 15.33 GB
    Container: Yes
    Shell: 5.8.1 - /usr/bin/zsh
  Binaries:
    Node: 22.9.0 - ~/.nvm/versions/node/v22.9.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v22.9.0/bin/yarn
    npm: 10.8.3 - ~/.nvm/versions/node/v22.9.0/bin/npm
  Browsers:
    Chromium: 133.0.6943.53
  npmPackages:
    @vitejs/plugin-vue: ^5.2.1 => 5.2.1 
    @vitest/coverage-v8: ^3.0.5 => 3.0.5 
    vite: ^6.1.0 => 6.1.0 
    vitest: ^3.0.5 => 3.0.5 

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions