Skip to content

Commit

Permalink
expect: Improve report when assertion fails, part 6 (#7621)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pedrottimark authored Jan 20, 2019
1 parent eae9d45 commit 10bb779
Show file tree
Hide file tree
Showing 6 changed files with 559 additions and 312 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
- `[jest-config]` Allow % based configuration of `--max-workers` ([#7494](https://github.com/facebook/jest/pull/7494))
- `[jest-runner]` Instantiate the test environment class with the current `testPath` ([#7442](https://github.com/facebook/jest/pull/7442))
- `[jest-config]` Always resolve jest-environment-jsdom from jest-config ([#7476](https://github.com/facebook/jest/pull/7476))
- `[expect]` Improve report when assertion fails, part 6 ([#7621](https://github.com/facebook/jest/pull/7621))

### Fixes

Expand Down
10 changes: 9 additions & 1 deletion docs/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,14 @@ test('throws on octopus', () => {
});
```

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:
To test that a specific error is thrown, you can provide an argument:

- regular expression: error message **matches** the pattern
- string: error message **includes** the substring
- error object: error message is **equal to** the message property of the object
- error class: error object is **instance of** class

For example, let's say that `drinkFlavor` is coded like this:

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

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

// Test that we get a DisgustingFlavorError
expect(drinkOctopus).toThrowError(DisgustingFlavorError);
Expand Down
Loading

0 comments on commit 10bb779

Please sign in to comment.