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

vi.mock() removes functions from classes #1523

Closed
6 tasks done
Kjir opened this issue Jun 21, 2022 · 3 comments · Fixed by #1648
Closed
6 tasks done

vi.mock() removes functions from classes #1523

Kjir opened this issue Jun 21, 2022 · 3 comments · Fixed by #1648

Comments

@Kjir
Copy link

Kjir commented Jun 21, 2022

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

System:
    OS: Linux 5.18 Arch Linux
    CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
    Memory: 5.16 GB / 15.51 GB
    Container: Yes
    Shell: 3.4.1 - /usr/bin/fish
  Binaries:
    Node: 16.15.1 - ~/.nodenv/shims/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 8.5.5 - ~/.nodenv/shims/npm
  Browsers:
    Brave Browser: 102.1.39.122
  npmPackages:
    vite: ^2.9.9 => 2.9.12
    vitest: latest => 0.15.2

Used Package Manager

npm

Validations

@nieyuyao
Copy link
Contributor

nieyuyao commented Jun 28, 2022

We got a new function fn by calling the spyOn when mocking class. But fn.prototype is not set to point to class.prototype? Does it seem like a problem with tinyspy?

@sheremet-va
Copy link
Member

We got a new function fn by calling the spyOn when mocking class. But fn.prototype is not set to point to class.prototype? 🤔

🤔

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 prototype on top of it 🤔 Doesn't seem to be reliable.

If someone has time and ideas on how to improve auto mocking, please, open a discussion or PR 😄

simon-abbott added a commit to simon-abbott/vitest that referenced this issue Jul 15, 2022
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.
simon-abbott added a commit to simon-abbott/vitest that referenced this issue Jul 15, 2022
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.
@simon-abbott
Copy link
Contributor

I put up a PR fixing this! #1648

simon-abbott added a commit to simon-abbott/vitest that referenced this issue Jul 15, 2022
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.
@github-actions github-actions bot locked and limited conversation to collaborators Jun 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants