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

Mocking a module that is a class will have all of its instances to share the same 'mock' #4281

Closed
6 tasks done
rafaelmaiolla opened this issue Oct 9, 2023 · 0 comments · Fixed by #4564
Closed
6 tasks done
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) pr welcome

Comments

@rafaelmaiolla
Copy link

Describe the bug

When using vi.mock('./SomeClass.ts'); to mock a module that is a class exported as default export class SomeClass {...}, it will have all of its instances to share the same 'mock' attribute in the methods.

And if your code create multiple instances of that SomeClass, it is not possible to check if a method was called from one or the other instance as both will share the mock attribute.

This works fine in Jest, both checks above will be false.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-i4zx4z?file=test%2Fbasic.test.ts

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (12) x64 Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz   
    Memory: 5.79 GB / 31.91 GB
  Binaries:
    Node: 18.15.0 - ~\.nvm\versions\node\v18.15.0\bin\node.EXE
    npm: 9.5.0 - ~\.nvm\versions\node\v18.15.0\bin\npm.CMD    
  npmPackages:
    @vitejs/plugin-react-swc: 3.4.0 => 3.4.0
    @vitest/coverage-v8: 0.34.6 => 0.34.6
    vite: 4.4.11 => 4.4.11
    vitest: 0.34.6 => 0.34.6

Used Package Manager

npm

Validations

@sheremet-va sheremet-va added bug pr welcome p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Oct 31, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Dec 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) pr welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants