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: Improve report when assertion fails, part 6 #7621

Merged
merged 15 commits into from
Jan 20, 2019
Merged
Show file tree
Hide file tree
Changes from 7 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 @@ -49,6 +49,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
256 changes: 126 additions & 130 deletions packages/expect/src/__tests__/__snapshots__/toThrowMatchers.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.toThrow() error class did not throw at all 1`] = `
"<dim>expect(</><red>function</><dim>).toThrow(</><green>type</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>toThrow<dim>(</><green>expected</><dim>)</>

Expected the function to throw an error of type:
<green>\\"Err\\"</>
But it didn't throw anything."
Expected error name: <green>\\"Err\\"</>

Received function did not throw an exception"
`;

exports[`.toThrow() error class threw, but class did not match 1`] = `
"<dim>expect(</><red>function</><dim>).toThrow(</><green>type</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>toThrow<dim>(</><green>expected</><dim>)</>

Expected error name: <green>\\"Err2\\"</>
Received error name: <red>\\"Error\\"</>

Expected the function to throw an error of type:
<green>\\"Err2\\"</>
Instead, it threw:
<red> Error</>
<red> <dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</></>"
Received error message: <red>\\"apple\\"</>

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

exports[`.toThrow() error class threw, but should not have 1`] = `
"<dim>expect(</><red>function</><dim>).not.toThrow(</><green>type</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toThrow<dim>(</><green>expected</><dim>)</>

Expected error name: <green>\\"Err\\"</>
Received error name: <red>\\"Error\\"</>

Expected the function not to throw an error of type:
<green>\\"Err\\"</>
Instead, it threw:
<red> Error</>
<red> <dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</></>"
Received error message: <red>\\"apple\\"</>

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

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

<bold>Matcher error</>: <red>received</> value must be a function

Expand All @@ -38,126 +40,124 @@ Received has value: <red>\\"a string\\"</>"
`;

exports[`.toThrow() invalid arguments 1`] = `
"<dim>expect(</><red>received</><dim>)[.not].toThrow(</><green>expected</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toThrow<dim>(</><green>expected</><dim>)</>

<bold>Matcher error</>: <green>expected</> value must be a string or regular expression or Error
<bold>Matcher error</>: <green>expected</> value must be a string or regular expression or class or error

Expected has type: number
Expected has value: <green>111</>"
`;

exports[`.toThrow() promise/async throws if Error-like object is returned did not throw at all 1`] = `
[Error: expect(function).toThrow(undefined)
"<dim>expect(</><red>received</><dim>).</>rejects<dim>.</>toThrow<dim>()</>

Expected the function to throw an error.
But it didn't throw anything.]
Received function did not throw an exception"
`;

exports[`.toThrow() promise/async throws if Error-like object is returned threw, but class did not match 1`] = `
[Error: expect(function).toThrow(type)
"<dim>expect(</><red>received</><dim>).</>rejects<dim>.</>toThrow<dim>(</><green>expected</><dim>)</>

Expected error name: <green>\\"Err2\\"</>
Received error name: <red>\\"Error\\"</>

Received error message: <red>\\"async apple\\"</>

Expected the function to throw an error of type:
"Err2"
Instead, it threw:
 Error
 at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)]
<dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</>"
`;

exports[`.toThrow() promise/async throws if Error-like object is returned threw, but should not have 1`] = `
[Error: expect(function).not.toThrow()
"<dim>expect(</><red>received</><dim>).</>rejects<dim>.</>not<dim>.</>toThrow<dim>()</>

Expected the function not to throw an error.
Instead, it threw:
 Error
 at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)]
Received error name: <red>\\"Error\\"</>
Received error message: <red>\\"async apple\\"</>

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

exports[`.toThrow() regexp did not throw at all 1`] = `
"<dim>expect(</><red>function</><dim>).toThrow(</><green>regexp</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>toThrow<dim>(</><green>expected</><dim>)</>

Expected error pattern: <green>/apple/</>

Expected the function to throw an error matching:
<green>/apple/</>
But it didn't throw anything."
Received function did not throw an exception"
`;

exports[`.toThrow() regexp threw, but message did not match 1`] = `
"<dim>expect(</><red>function</><dim>).toThrow(</><green>regexp</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>toThrow<dim>(</><green>expected</><dim>)</>

Expected the function to throw an error matching:
<green>/banana/</>
Instead, it threw:
<red> Error</>
<red> <dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</></>"
Expected error pattern: <green>/banana/</>
Received error message: <red>\\"apple\\"</>

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

exports[`.toThrow() regexp threw, but should not have 1`] = `
"<dim>expect(</><red>function</><dim>).not.toThrow(</><green>regexp</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toThrow<dim>(</><green>expected</><dim>)</>

Expected error pattern: <green>/apple/</>
Received error message: <red>\\"apple\\"</>

Expected the function not to throw an error matching:
<green>/apple/</>
Instead, it threw:
<red> Error</>
<red> <dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</></>"
<dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</>"
`;

exports[`.toThrow() strings did not throw at all 1`] = `
"<dim>expect(</><red>function</><dim>).toThrow(</><green>string</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>toThrow<dim>(</><green>expected</><dim>)</>

Expected the function to throw an error matching:
<green>\\"apple\\"</>
But it didn't throw anything."
Expected error pattern: <green>\\"apple\\"</>

Received function did not throw an exception"
`;

exports[`.toThrow() strings threw, but message did not match 1`] = `
"<dim>expect(</><red>function</><dim>).toThrow(</><green>string</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>toThrow<dim>(</><green>expected</><dim>)</>

Expected error pattern: <green>\\"banana\\"</>
Received error message: <red>\\"apple\\"</>

Expected the function to throw an error matching:
<green>\\"banana\\"</>
Instead, it threw:
<red> Error</>
<red> <dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</></>"
<dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</>"
`;

exports[`.toThrow() strings threw, but should not have 1`] = `
"<dim>expect(</><red>function</><dim>).not.toThrow(</><green>string</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toThrow<dim>(</><green>expected</><dim>)</>

Expected error pattern: <green>\\"apple\\"</>
Received error message: <red>\\"apple\\"</>

Expected the function not to throw an error matching:
<green>\\"apple\\"</>
Instead, it threw:
<red> Error</>
<red> <dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</></>"
<dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</>"
`;

exports[`.toThrowError() error class did not throw at all 1`] = `
"<dim>expect(</><red>function</><dim>).toThrowError(</><green>type</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>toThrowError<dim>(</><green>expected</><dim>)</>

Expected the function to throw an error of type:
<green>\\"Err\\"</>
But it didn't throw anything."
Expected error name: <green>\\"Err\\"</>

Received function did not throw an exception"
`;

exports[`.toThrowError() error class threw, but class did not match 1`] = `
"<dim>expect(</><red>function</><dim>).toThrowError(</><green>type</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>toThrowError<dim>(</><green>expected</><dim>)</>

Expected error name: <green>\\"Err2\\"</>
Received error name: <red>\\"Error\\"</>

Expected the function to throw an error of type:
<green>\\"Err2\\"</>
Instead, it threw:
<red> Error</>
<red> <dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</></>"
Received error message: <red>\\"apple\\"</>
Copy link
Collaborator

Choose a reason for hiding this comment

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

oh, love this separation, makes it way easier to trace

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Proximity for the win :)


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

exports[`.toThrowError() error class threw, but should not have 1`] = `
"<dim>expect(</><red>function</><dim>).not.toThrowError(</><green>type</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toThrowError<dim>(</><green>expected</><dim>)</>

Expected error name: <green>\\"Err\\"</>
Received error name: <red>\\"Error\\"</>

Expected the function not to throw an error of type:
<green>\\"Err\\"</>
Instead, it threw:
<red> Error</>
<red> <dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</></>"
Received error message: <red>\\"apple\\"</>

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

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

<bold>Matcher error</>: <red>received</> value must be a function

Expand All @@ -166,92 +166,88 @@ Received has value: <red>\\"a string\\"</>"
`;

exports[`.toThrowError() invalid arguments 1`] = `
"<dim>expect(</><red>received</><dim>)[.not].toThrowError(</><green>expected</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toThrowError<dim>(</><green>expected</><dim>)</>

<bold>Matcher error</>: <green>expected</> value must be a string or regular expression or Error
<bold>Matcher error</>: <green>expected</> value must be a string or regular expression or class or error

Expected has type: number
Expected has value: <green>111</>"
`;

exports[`.toThrowError() promise/async throws if Error-like object is returned did not throw at all 1`] = `
[Error: expect(function).toThrow(undefined)
"<dim>expect(</><red>received</><dim>).</>rejects<dim>.</>toThrowError<dim>()</>

Expected the function to throw an error.
But it didn't throw anything.]
Received function did not throw an exception"
`;

exports[`.toThrowError() promise/async throws if Error-like object is returned threw, but class did not match 1`] = `
[Error: expect(function).toThrow(type)
"<dim>expect(</><red>received</><dim>).</>rejects<dim>.</>toThrowError<dim>(</><green>expected</><dim>)</>

Expected error name: <green>\\"Err2\\"</>
Received error name: <red>\\"Error\\"</>

Received error message: <red>\\"async apple\\"</>

Expected the function to throw an error of type:
"Err2"
Instead, it threw:
 Error
 at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)]
<dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</>"
`;

exports[`.toThrowError() promise/async throws if Error-like object is returned threw, but should not have 1`] = `
[Error: expect(function).not.toThrow()
"<dim>expect(</><red>received</><dim>).</>rejects<dim>.</>not<dim>.</>toThrowError<dim>()</>

Expected the function not to throw an error.
Instead, it threw:
 Error
 at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)]
Received error name: <red>\\"Error\\"</>
Received error message: <red>\\"async apple\\"</>

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

exports[`.toThrowError() regexp did not throw at all 1`] = `
"<dim>expect(</><red>function</><dim>).toThrowError(</><green>regexp</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>toThrowError<dim>(</><green>expected</><dim>)</>

Expected error pattern: <green>/apple/</>

Expected the function to throw an error matching:
<green>/apple/</>
But it didn't throw anything."
Received function did not throw an exception"
`;

exports[`.toThrowError() regexp threw, but message did not match 1`] = `
"<dim>expect(</><red>function</><dim>).toThrowError(</><green>regexp</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>toThrowError<dim>(</><green>expected</><dim>)</>

Expected the function to throw an error matching:
<green>/banana/</>
Instead, it threw:
<red> Error</>
<red> <dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</></>"
Expected error pattern: <green>/banana/</>
Received error message: <red>\\"apple\\"</>

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

exports[`.toThrowError() regexp threw, but should not have 1`] = `
"<dim>expect(</><red>function</><dim>).not.toThrowError(</><green>regexp</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toThrowError<dim>(</><green>expected</><dim>)</>

Expected error pattern: <green>/apple/</>
Received error message: <red>\\"apple\\"</>

Expected the function not to throw an error matching:
<green>/apple/</>
Instead, it threw:
<red> Error</>
<red> <dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</></>"
<dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</>"
`;

exports[`.toThrowError() strings did not throw at all 1`] = `
"<dim>expect(</><red>function</><dim>).toThrowError(</><green>string</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>toThrowError<dim>(</><green>expected</><dim>)</>

Expected the function to throw an error matching:
<green>\\"apple\\"</>
But it didn't throw anything."
Expected error pattern: <green>\\"apple\\"</>

Received function did not throw an exception"
`;

exports[`.toThrowError() strings threw, but message did not match 1`] = `
"<dim>expect(</><red>function</><dim>).toThrowError(</><green>string</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>toThrowError<dim>(</><green>expected</><dim>)</>

Expected error pattern: <green>\\"banana\\"</>
Received error message: <red>\\"apple\\"</>

Expected the function to throw an error matching:
<green>\\"banana\\"</>
Instead, it threw:
<red> Error</>
<red> <dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</></>"
<dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</>"
`;

exports[`.toThrowError() strings threw, but should not have 1`] = `
"<dim>expect(</><red>function</><dim>).not.toThrowError(</><green>string</><dim>)</>
"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toThrowError<dim>(</><green>expected</><dim>)</>

Expected error pattern: <green>\\"apple\\"</>
Received error message: <red>\\"apple\\"</>

Expected the function not to throw an error matching:
<green>\\"apple\\"</>
Instead, it threw:
<red> Error</>
<red> <dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</></>"
<dim>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<dim>:24:74)</>"
`;
Loading