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 all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- `[jest-jasmine2]` Fail synchronous test timeouts ([#7074](https://github.com/facebook/jest/pull/7074))
- `[jest-jasmine2]` Better error message when a describe block is empty ([#6372](https://github.com/facebook/jest/pull/6372))
- `[jest-circus]` Better error message when a describe block is empty ([#6372](https://github.com/facebook/jest/pull/6372))
- `[expect]` Add empty string check to `toThrow` matcher ([#6836](https://github.com/facebook/jest/pull/6836))

### Chore & Maintenance

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> Error</>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this text include 'apple'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test file defines a custom error with a fixed message for the snapshot - I’ve just reused that which is why it doesn’t say apple

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems silly, doesn't it? Is the error inconsistent if we drop the fixed message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it does a bit! I’ll give it a go and see what happens, maybe it was giving inconsistent line numbers 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I just noticed this comment above the custom error/stack:

Custom Error class because node versions have different stack trace strings.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it still valid?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, toThrowErrorMatchingSnapshot only snapshots the message, not stack

<red> <dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</></>"
`;

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> Error</>
<red> <dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</></>"
`;

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

Expand Down
8 changes: 8 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,14 @@ class customError extends Error {
class Err extends customError {}
class Err2 extends customError {}

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

test('to throw or not to throw', () => {
jestExpect(() => {
throw new customError('apple');
Expand Down
6 changes: 5 additions & 1 deletion packages/expect/src/to_throw_matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ export const createMatcher = (matcherName: string, fromPromise?: boolean) => (
}

if (typeof expected === 'string') {
expected = new RegExp(escapeStrForRegex(expected));
if (expected === '') {
expected = new RegExp('^$');
} else {
expected = new RegExp(escapeStrForRegex(expected));
}
}

if (typeof expected === 'function') {
Expand Down
8 changes: 7 additions & 1 deletion packages/jest-regex-util/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
jest.mock('path');

import {replacePathSepForRegex} from '../index';
import {replacePathSepForRegex, escapeStrForRegex} from '../index';
import path from 'path';

describe('replacePathSepForRegex()', () => {
Expand All @@ -13,6 +13,12 @@ describe('replacePathSepForRegex()', () => {
});
});

describe('escapeStrForRegex', () => {
it('returns regex for given string: [0-9]', () => {
expect(escapeStrForRegex('[0-9]')).toBe('\\[0-9\\]');
});
});

describe('win32', () => {
beforeEach(() => (path.sep = '\\'));

Expand Down