-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: implement mockThrow and mockThrowOnce
#9512
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
base: main
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for vitest-dev ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
f5d1c25 to
b7879ee
Compare
sheremet-va
left a comment
There was a problem hiding this 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ## mockThrow | |
| ## mockThrow <Version>4.1.0</Version> {#mockthrow} |
docs/api/mock.md
Outdated
| myMockFn() // throws Error<'error message'> | ||
| ``` | ||
|
|
||
| ## mockThrowOnce |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ## mockThrowOnce | |
| ## mockThrowOnce <Version>4.1.0</Version> {#mockthrowonce} |
packages/spy/src/index.ts
Outdated
| } | ||
|
|
||
| mock.mockThrow = function mockThrow(value) { | ||
| return mock.mockImplementation(() => { |
There was a problem hiding this comment.
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())
| return mock.mockImplementation(() => { | |
| return mock.mockImplementation(function () { |
packages/spy/src/index.ts
Outdated
| } | ||
|
|
||
| mock.mockThrowOnce = function mockThrowOnce(value) { | ||
| return mock.mockImplementationOnce(() => { |
There was a problem hiding this comment.
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())
| return mock.mockImplementationOnce(() => { | |
| return mock.mockImplementationOnce(function () { |
|
Also, please rebase from |
b7879ee to
a48ddfb
Compare
a48ddfb to
7e66b8b
Compare
|
@sheremet-va thanks! I've applied your suggestions and rebased on latest |
| mock.mockReset() | ||
| expect(mock()).toBe(undefined) | ||
| }) | ||
|
|
There was a problem hiding this comment.
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) ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Description
Implements
mockThrow(foo)andmockThrowOnce(foo)as shorthands formockImplementation(() => { throw foo })andmockImplementationOnce(() => { 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 passpnpm run test:ci→ Fails due to an existing test skipped only whenGITHUB_ACTIONSis setAffected test:
test/cli/test/watch/stdin.test.ts:93As 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:
pnpm-lock.yamlunless you introduce a new test example.Tests
pnpm test:ci.Documentation
pnpm run docscommand.Changesets
feat:,fix:,perf:,docs:, orchore:.