Skip to content

Conversation

@thor-juhasz
Copy link
Contributor

@thor-juhasz thor-juhasz commented Jan 23, 2026

Description

Implements mockThrow(foo) and mockThrowOnce(foo) as shorthands for mockImplementation(() => { throw foo }) and mockImplementationOnce(() => { throw foo }).

Having a non-error + error path for synchronous functions (mockReturnValue + mockThrow) mirrors the async counterparts (mockResolvedValue + mockRejectedValue), and makes synchronous error paths immediately visible and intention-revealing in tests.

I'm open to renaming these if wanted though. I considered mockThrownValue, but just didn't see the need.

Note

pnpm run test → All pass
pnpm run test:ci → Fails due to an existing test skipped only when GITHUB_ACTIONS is set

Affected test: test/cli/test/watch/stdin.test.ts:93
As far as I'm aware, it's unrelated to the changes in this PR.

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.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

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

@netlify
Copy link

netlify bot commented Jan 23, 2026

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit a48ddfb
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/69770c96f91f39000826633c
😎 Deploy Preview https://deploy-preview-9512--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.

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.

This makes sense to me, I like 👍

Let's mark it as 4.1 feature and fix the constructor use case. Let's also add a test for this use case

docs/api/mock.md Outdated
console.log(myMockFn(), myMockFn(), myMockFn(), myMockFn())
```

## mockThrow
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
## mockThrow
## mockThrow <Version>4.1.0</Version> {#mockthrow}

docs/api/mock.md Outdated
myMockFn() // throws Error<'error message'>
```

## mockThrowOnce
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
## mockThrowOnce
## mockThrowOnce <Version>4.1.0</Version> {#mockthrowonce}

}

mock.mockThrow = function mockThrow(value) {
return mock.mockImplementation(() => {
Copy link
Member

Choose a reason for hiding this comment

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

Let's support throwing errors inside a constructor (new Mock())

Suggested change
return mock.mockImplementation(() => {
return mock.mockImplementation(function () {

}

mock.mockThrowOnce = function mockThrowOnce(value) {
return mock.mockImplementationOnce(() => {
Copy link
Member

Choose a reason for hiding this comment

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

Let's support throwing errors inside a constructor (new Mock())

Suggested change
return mock.mockImplementationOnce(() => {
return mock.mockImplementationOnce(function () {

@sheremet-va
Copy link
Member

Also, please rebase from main, it has some changes in this file (mainly switching to function() in shorthand methods)

@thor-juhasz
Copy link
Contributor Author

@sheremet-va thanks!

I've applied your suggestions and rebased on latest main.

mock.mockReset()
expect(mock()).toBe(undefined)
})

Copy link
Member

Choose a reason for hiding this comment

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

Can you add a test with the constructor I mentioned in #9512 (review) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am unsure what you mean here...
The mockReturn and mockReturnOnce test cases do not cover any such thing, they just return whatever you pass.
Likewise, mockThrow and mockThrowOnce just throw whatever you pass.

I did change them to use function () { .. } instead of () => { .. } as you requested, but fail to understand what you want tested here.

Copy link
Member

@sheremet-va sheremet-va Jan 27, 2026

Choose a reason for hiding this comment

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

I am asking you to mock a class that throws an error vi.fn(class {}) in the constructor (vi.fn(class {}).mockThrow())

Others don’t have it because they don’t support classes

@sheremet-va sheremet-va added this to the 4.1.0 milestone Jan 27, 2026
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.

2 participants