Skip to content

fix(spy)!: support spying on classes #6160

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

Merged
merged 18 commits into from
Jun 24, 2025

Conversation

sheremet-va
Copy link
Member

@sheremet-va sheremet-va commented Jul 18, 2024

Description

Closes #6104

This is a breaking change.

This is not supported anymore:

vi.spyOn(mock, 'HelloWorld').mockImplementationOnce(() => {
  const Mock = vi.fn()
  Mock.prototype.hello = vi.fn(() => 'hello world')
  return new Mock()
})
const hw = new mock.HelloWorld()

But you can replace it with a function:

+ vi.spyOn(mock, 'HelloWorld').mockImplementationOnce(function () {
  const Mock = vi.fn()
  Mock.prototype.hello = vi.fn(() => 'hello world')
  return new Mock()
})
const hw = new mock.HelloWorld()

But ideally you should instead do something like this:

const HelloWorld = vi.spyOn(mock, 'HelloWorld')
HelloWorld.prototype.hello = vi.fn(() => 'hello world')
const hw = new mock.HelloWorld()

Or this (previously not supported):

const HelloWorld = vi.spyOn(mock, 'HelloWorld').mockImplementation(class Mock {
  hello = vi.fn(() => 'hello world')
})

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

@sheremet-va sheremet-va marked this pull request as draft July 18, 2024 09:59
@sheremet-va sheremet-va added this to the 3.0.0 milestone Aug 15, 2024
@sheremet-va sheremet-va changed the title fix(spy): support spying on classes fix(spy)!: support spying on classes Nov 7, 2024
@sheremet-va sheremet-va modified the milestones: 3.0.0, 4.0.0 Dec 5, 2024
Copy link

netlify bot commented Jun 21, 2025

Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit 707e5e2
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/685aa809c9d473000801e7fd
😎 Deploy Preview https://deploy-preview-6160--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 project configuration.

@sheremet-va sheremet-va marked this pull request as ready for review June 21, 2025 16:28
AriPerkkio
AriPerkkio previously approved these changes Jun 22, 2025
@sheremet-va sheremet-va merged commit abc0d82 into vitest-dev:main Jun 24, 2025
15 checks passed
@sheremet-va sheremet-va deleted the fix/support-spyin-classes branch June 24, 2025 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Intl.RelativeTimeFormat and Intl.ListFormat can't be spied upon
2 participants