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
vi.mock()
removes functions from classes
#1523
Comments
We got a new function |
🤔 The logic when auto mocking is not quite right. I'm thinking if we maybe should follow jest's approach and collect "metadata" for an object and then recreate it? Right now it just takes all properties (including prototype) and reassigns them, and then also puts original If someone has time and ideas on how to improve auto mocking, please, open a discussion or PR 😄 |
Previously classes weren't mocked properly because their prototypes were not being copied, so they lost all their methods. This commit rewrites the automocker to properly handle: - Functions with properties (namely classes) - Circular objects (also to handle classes, thanks JavaScript) With these changes classes can now be properly automocked! This fixes vitest-dev#1523.
Previously classes weren't mocked properly because their prototypes were not being copied, so they lost all their methods. This commit rewrites the automocker to properly handle: - Functions with properties (namely classes) - Circular objects (also to handle classes, thanks JavaScript) With these changes classes can now be properly automocked! This fixes vitest-dev#1523.
I put up a PR fixing this! #1648 |
Previously classes weren't mocked properly because their prototypes were not being copied, so they lost all their methods. This commit rewrites the automocker to properly handle: - Functions with properties (namely classes) - Circular objects (also to handle classes, thanks JavaScript) With these changes classes can now be properly automocked! This fixes vitest-dev#1523.
Describe the bug
When mocking a module with
vi.mock("./some/module")
, some unexpected behaviour happens: classes exported by the module won't have the correct functions when instantiated.Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-8vqu9r?file=test/basic.test.ts
The same code (transformed in CommonJS format) works with jest, which is creating an inconsistency when migrating (cfr. #1521 )
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: