Skip to content

Commit 10bb779

Browse files
authored
expect: Improve report when assertion fails, part 6 (#7621)
* expect: Improve report when assertion fails, part 6 * Fix flow errors * Move new function to reduce noise in diff * Update CHANGELOG.md * Convert ANSI codes in promise/async snapshots * Rewrite promise/async snapshot tests * Reduce length of labels and handle non-error values * Add tests for non-error values * Fix prettier lint error * Update ExpectAPI.md * Improve grammar in ExpectAPI.md * Delete an exception from Received function did not throw * Correct typo in ExpectAPI.md * Correct type and report for non-error class
1 parent eae9d45 commit 10bb779

File tree

6 files changed

+559
-312
lines changed

6 files changed

+559
-312
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
- `[jest-config]` Allow % based configuration of `--max-workers` ([#7494](https://github.com/facebook/jest/pull/7494))
5151
- `[jest-runner]` Instantiate the test environment class with the current `testPath` ([#7442](https://github.com/facebook/jest/pull/7442))
5252
- `[jest-config]` Always resolve jest-environment-jsdom from jest-config ([#7476](https://github.com/facebook/jest/pull/7476))
53+
- `[expect]` Improve report when assertion fails, part 6 ([#7621](https://github.com/facebook/jest/pull/7621))
5354

5455
### Fixes
5556

docs/ExpectAPI.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,14 @@ test('throws on octopus', () => {
11681168
});
11691169
```
11701170

1171-
If you want to test that a specific error gets thrown, you can provide an argument to `toThrow`. The argument can be a string that should be contained in the error message, a class for the error, or a regex that should match the error message. For example, let's say that `drinkFlavor` is coded like this:
1171+
To test that a specific error is thrown, you can provide an argument:
1172+
1173+
- regular expression: error message **matches** the pattern
1174+
- string: error message **includes** the substring
1175+
- error object: error message is **equal to** the message property of the object
1176+
- error class: error object is **instance of** class
1177+
1178+
For example, let's say that `drinkFlavor` is coded like this:
11721179

11731180
```js
11741181
function drinkFlavor(flavor) {
@@ -1193,6 +1200,7 @@ test('throws on octopus', () => {
11931200

11941201
// Test the exact error message
11951202
expect(drinkOctopus).toThrowError(/^yuck, octopus flavor$/);
1203+
expect(drinkOctopus).toThrowError(new Error('yuck, octopus flavor'));
11961204

11971205
// Test that we get a DisgustingFlavorError
11981206
expect(drinkOctopus).toThrowError(DisgustingFlavorError);

0 commit comments

Comments
 (0)