Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
6 tasks done
Redpoint1 opened this issue Feb 12, 2025 · 1 comment
Open
6 tasks done

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

Redpoint1 opened this issue Feb 12, 2025 · 1 comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@Redpoint1
Copy link

Redpoint1 commented Feb 12, 2025

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

@hi-ogawa
Copy link
Contributor

Having vi.mock manually at the top shouldn't be necessary since Vitest transforms in that way, but I think it's a bug if already moved ones are broken. It's possible that the difference is due to Vite 6's change on ssr transform.

Here is an output of transform dump.

VITE_NODE_DEBUG_DUMP=1 npm run test
/// bad
const __vite_ssr_import_0__ = await __vite_ssr_import__("vitest", {"importedNames":["vi","test","expect"]});__vite_ssr_import_0__.vi.mock('src/mockmesecond');
__vite_ssr_import_0__.vi.mock('src/mockmethird');
const __vi_import_0__ = await __vite_ssr_dynamic_import__("src/mockmefirst");
const __vi_import_1__ = await __vite_ssr_dynamic_import__("src/mockmesecond");
const __vi_import_2__ = await __vite_ssr_dynamic_import__("src/mockmethird");
__vite_ssr_import_0__.vi.mock('src/mockmefirst');
...
/// good
const __vite_ssr_import_0__ = await __vite_ssr_import__("vitest", {"importedNames":["vi","test","expect"]});__vite_ssr_import_0__.vi.mock('src/mockmefirst');
__vite_ssr_import_0__.vi.mock('src/mockmesecond');
__vite_ssr_import_0__.vi.mock('src/mockmethird');
const __vi_import_0__ = await __vite_ssr_dynamic_import__("src/mockmefirst");
const __vi_import_1__ = await __vite_ssr_dynamic_import__("src/mockmesecond");
const __vi_import_2__ = await __vite_ssr_dynamic_import__("src/mockmethird");

@hi-ogawa hi-ogawa added p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

No branches or pull requests

2 participants