-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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 .toThrow for promises #4884
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -416,15 +416,15 @@ For example, this code tests that the promise rejects with reason `'octopus'`: | |
```js | ||
test('rejects to octopus', () => { | ||
// make sure to add a return statement | ||
return expect(Promise.reject('octopus')).rejects.toBe('octopus'); | ||
return expect(Promise.reject(new Error('octopus'))).rejects.toThrow('octopus'); | ||
}); | ||
``` | ||
|
||
Alternatively, you can use `async/await` in combination with `.rejects`. | ||
|
||
```js | ||
test('rejects to octopus', async () => { | ||
await expect(Promise.reject('octopus')).rejects.toBe('octopus'); | ||
await expect(Promise.reject(new Error('octopus'))).rejects.toThrow('octopus'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if we should say that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. currently, it mentions
Change it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or a separate line describing rejecting promises and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lsentkiewicz this came up in #4945, mind sending a PR with clarification? 🙂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. really painful way to discover why @SimenB you were right, it is probably a good idea to have a mention to it in the docs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you send a PR fixing the docs? |
||
}); | ||
``` | ||
|
||
|
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 old example is still valid, right?
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.
(although it's not recommended to reject with a non-error)
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.
yes, it still works