Skip to content
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

[expect]: add empty string check to toThrow matcher #6836

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## master

## Fixes

- `[expect]` Add empty string check to `toThrow` matcher ([#6836](https://github.com/facebook/jest/pull/6836))

## 23.5.0

### Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ Instead, it threw:
<red> <dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</></>"
`;

exports[`.toThrow() fails when given an empty string 1`] = `
"<dim>expect(</><red>function</><dim>).toThrow(</><green>string</><dim>)</>

Expected the function to throw an error matching:
<green>\\"\\"</>
Instead, it threw:
<red> apple</>
<red> <dim>at toThrowMatchingStringOrRegexp (</>packages/expect/src/to_throw_matchers.js<dim>:109:13)</></>"
`;

exports[`.toThrow() invalid actual 1`] = `
"<dim>expect(</><red>function</><dim>).toThrow(</><green>undefined</><dim>)</>

Expand Down Expand Up @@ -153,6 +163,16 @@ Instead, it threw:
<red> <dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</></>"
`;

exports[`.toThrowError() fails when given an empty string 1`] = `
"<dim>expect(</><red>function</><dim>).toThrow(</><green>string</><dim>)</>

Expected the function to throw an error matching:
<green>\\"\\"</>
Instead, it threw:
<red> apple</>
<red> <dim>at toThrowMatchingStringOrRegexp (</>packages/expect/src/to_throw_matchers.js<dim>:109:13)</></>"
`;

exports[`.toThrowError() invalid actual 1`] = `
"<dim>expect(</><red>function</><dim>).toThrowError(</><green>undefined</><dim>)</>

Expand Down
6 changes: 6 additions & 0 deletions packages/expect/src/__tests__/to_throw_matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class customError extends Error {
class Err extends customError {}
class Err2 extends customError {}

test('fails when given an empty string', () => {
expect(() => jestExpect(() => {
throw 'apple';
}).toThrow('')).toThrowErrorMatchingSnapshot();
});

test('to throw or not to throw', () => {
jestExpect(() => {
throw new customError('apple');
Expand Down
2 changes: 1 addition & 1 deletion packages/expect/src/to_throw_matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const toThrowMatchingStringOrRegexp = (
error = new Error(error);
}

const pass = !!(error && error.message.match(pattern));
const pass = !!(error && value && error.message.match(pattern));
const message = pass
? () =>
matcherHint('.not' + name, 'function', getType(value)) +
Expand Down