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

feat(mocker): introduce @vitest/mocker package, allow { spy: true } instead of a factory #6289

Merged
merged 41 commits into from
Aug 21, 2024

Conversation

sheremet-va
Copy link
Member

@sheremet-va sheremet-va commented Aug 6, 2024

Description

This PR introduces a way to spy on all exports instead of automocking the whole module by overriding every exported method with an empty function.

Closes #6099

import { method } from 'library'

vi.mock('library', { spy: true })

vi.isMockFunction(method) === true
method() // calls original method
expect(method).toHaveBeenCalled()

This is particularly useful in the browser mode where it is not possible to call spyOn on an import.

TODO:

  • Docs
  • More tests
  • Browser support
  • Refactor (there is currently a lot of duplicated logic between the browser and node, and logic for "behaviour" is similar to "type" in the browser)

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:.

Copy link

netlify bot commented Aug 6, 2024

Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit 007556c
🔍 Latest deploy log https://app.netlify.com/sites/vitest-dev/deploys/66c5eebf851d5a000840176c
😎 Deploy Preview https://deploy-preview-6289--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 configuration.

@sheremet-va sheremet-va force-pushed the feat/allow-spying-mocking branch from d32ab38 to c545d0a Compare August 13, 2024 11:05
@sheremet-va sheremet-va marked this pull request as ready for review August 13, 2024 16:17
@sheremet-va sheremet-va force-pushed the feat/allow-spying-mocking branch from dd042c1 to 9ff4d79 Compare August 15, 2024 17:00
@sheremet-va sheremet-va changed the title feat(vitest): allow auto spying on all exports feat(mocker): introduce @vitest/mocker package Aug 16, 2024
@sheremet-va sheremet-va changed the title feat(mocker): introduce @vitest/mocker package feat(mocker): introduce @vitest/mocker package, allow { spy: true } instead of a factory Aug 19, 2024
@sheremet-va sheremet-va force-pushed the feat/allow-spying-mocking branch from 9a9e90b to 81ce390 Compare August 19, 2024 16:58
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.

cannot mock spyOn namespace import in browser mode
1 participant