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

fix: mock properties of classes/functions (fix #1523) #1648

Merged
merged 1 commit into from Jul 16, 2022

Conversation

simon-abbott
Copy link
Contributor

Previously classes weren't mocked properly because their prototypes were not being copied, so they lost all their methods. This PR 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 #1523.

@netlify
Copy link

netlify bot commented Jul 15, 2022

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 1728548
🔍 Latest deploy log https://app.netlify.com/sites/vitest-dev/deploys/62d1ed151cb3d20008988f31
😎 Deploy Preview https://deploy-preview-1648--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@simon-abbott simon-abbott marked this pull request as draft July 15, 2022 21:26
@simon-abbott
Copy link
Contributor Author

Not quite ready yet. Found a problem with importing CJS modules. Will be fixed momentarily.

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 simon-abbott marked this pull request as ready for review July 15, 2022 23:43
@simon-abbott
Copy link
Contributor Author

CJS issue fixed, this PR is good to go.

Copy link
Member

@sheremet-va sheremet-va left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍🏻
Thank you!

@antfu antfu merged commit 4aeeee0 into vitest-dev:main Jul 16, 2022
@simon-abbott simon-abbott deleted the fix-class-automocking branch July 18, 2022 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vi.mock() removes functions from classes
3 participants