Skip to content

Commit b5dbcc1

Browse files
authored
Merge branch 'master' into ts-jest-resolver
2 parents b909b2d + a4a04b2 commit b5dbcc1

15 files changed

Lines changed: 409 additions & 326 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
### Features
44

5+
- `[expect]`: Improve report when matcher fails, part 7 ([#7866](https://github.com/facebook/jest/pull/7866))
6+
57
### Fixes
68

79
- `[jest-cli]` Refactor `-o` and `--coverage` combined ([#7611](https://github.com/facebook/jest/pull/7611))
10+
- `[expect]` Fix custom async matcher stack trace ([#7652](https://github.com/facebook/jest/pull/7652))
811

912
### Chore & Maintenance
1013

@@ -26,6 +29,7 @@
2629
- `[jest-mock]`: Migrate to TypeScript ([#7847](https://github.com/facebook/jest/pull/7847), [#7850](https://github.com/facebook/jest/pull/7850))
2730
- `[jest-worker]`: Migrate to TypeScript ([#7853](https://github.com/facebook/jest/pull/7853))
2831
- `[jest-haste-map]`: Migrate to TypeScript ([#7854](https://github.com/facebook/jest/pull/7854))
32+
- `[docs]`: Fix image paths in SnapshotTesting.md for current and version 24 ([#7872](https://github.com/facebook/jest/pull/7872))
2933
- `[jest-resolve]`: Migrate to TypeScript ([#7871](https://github.com/facebook/jest/pull/7871))
3034

3135
### Performance

CONTRIBUTING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ _Before_ submitting a pull request, please make sure the following is done…
7272

7373
1. If you haven't already, complete the CLA.
7474

75+
#### Changelog entries
76+
77+
All changes that add a feature to or fix a bug in any of Jest's packages require a changelog entry containing the names of the packages affected, a description of the change, and the number of and link to the pull request. Try to match the structure of the existing entries.
78+
79+
For significant changes to the documentation or website and things like cleanup, refactoring, and dependency updates, the "Chore & Maintenance" section of the changelog can be used.
80+
81+
You can add or edit the changelog entry in the GitHub web interface once you have opened the pull request and know the number and link to it.
82+
7583
#### Testing
7684

7785
Code that is written needs to be tested to ensure that it achieves the desired behaviour. Tests either fall into a unit test or an integration test.

docs/SnapshotTesting.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ exports[`renders correctly 1`] = `
4141

4242
The snapshot artifact should be committed alongside code changes, and reviewed as part of your code review process. Jest uses [pretty-format](https://github.com/facebook/jest/tree/master/packages/pretty-format) to make snapshots human-readable during code review. On subsequent test runs Jest will simply compare the rendered output with the previous snapshot. If they match, the test will pass. If they don't match, either the test runner found a bug in your code (in this case, it's `<Link>` component) that should be fixed, or the implementation has changed and the snapshot needs to be updated.
4343

44-
> Note: The snapshot is directly scoped to the data you render – in our example it's `<Link />` component with page prop passed to it. This implies that even if any other file has missing props (Say, `App.js`) in the `<Link />` component, it will still pass the test as the test doesn't know the usage of `<Link />` component and it's scoped only to the `Link.react.js`.
45-
> Also, Rendering the same component with different props in other snapshot tests will not affect the first one, as the tests don't know about each other.
44+
> Note: The snapshot is directly scoped to the data you render – in our example it's `<Link />` component with page prop passed to it. This implies that even if any other file has missing props (Say, `App.js`) in the `<Link />` component, it will still pass the test as the test doesn't know the usage of `<Link />` component and it's scoped only to the `Link.react.js`. Also, Rendering the same component with different props in other snapshot tests will not affect the first one, as the tests don't know about each other.
4645
4746
More information on how snapshot testing works and why we built it can be found on the [release blog post](https://jestjs.io/blog/2016/07/27/jest-14.html). We recommend reading [this blog post](http://benmccormick.org/2016/09/19/testing-with-jest-snapshots-first-impressions/) to get a good sense of when you should use snapshot testing. We also recommend watching this [egghead video](https://egghead.io/lessons/javascript-use-jest-s-snapshot-testing-feature?pl=testing-javascript-with-jest-a36c4074) on Snapshot Testing with Jest.
4847

@@ -64,7 +63,7 @@ it('renders correctly', () => {
6463

6564
In that case, Jest will print this output:
6665

67-
![](/website/static/img/content/failedSnapshotTest.png)
66+
![](/img/content/failedSnapshotTest.png)
6867

6968
Since we just updated our component to point to a different address, it's reasonable to expect changes in the snapshot for this component. Our snapshot test case is failing because the snapshot for our updated component no longer matches the snapshot artifact for this test case.
7069

@@ -84,17 +83,17 @@ You can try out this functionality by cloning the [snapshot example](https://git
8483

8584
Failed snapshots can also be updated interactively in watch mode:
8685

87-
![](/website/static/img/content/interactiveSnapshot.png)
86+
![](/img/content/interactiveSnapshot.png)
8887

8988
Once you enter Interactive Snapshot Mode, Jest will step you through the failed snapshots one test at a time and give you the opportunity to review the failed output.
9089

9190
From here you can choose to update that snapshot or skip to the next:
9291

93-
![](/website/static/img/content/interactiveSnapshotUpdate.gif)
92+
![](/img/content/interactiveSnapshotUpdate.gif)
9493

9594
Once you're finished, Jest will give you a summary before returning back to watch mode:
9695

97-
![](/website/static/img/content/interactiveSnapshotDone.png)
96+
![](/img/content/interactiveSnapshotDone.png)
9897

9998
### Inline Snapshots
10099

e2e/__tests__/__snapshots__/customMatcherStackTrace.test.js.snap

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`custom async matchers 1`] = `
4+
FAIL __tests__/asynchronous.test.js
5+
✕ showing the stack trace for an async matcher
6+
7+
● showing the stack trace for an async matcher
8+
9+
We expect the stack trace and code fence for this matcher to be shown in the console.
10+
11+
9 |
12+
10 | test('showing the stack trace for an async matcher', async () => {
13+
> 11 | await expect(true).toThrowCustomAsyncMatcherError();
14+
| ^
15+
12 | });
16+
13 |
17+
14 | async function toThrowCustomAsyncMatcherError() {
18+
19+
at Object.toThrowCustomAsyncMatcherError (__tests__/asynchronous.test.js:11:22)
20+
`;
21+
322
exports[`works with custom matchers 1`] = `
4-
FAIL __tests__/customMatcher.test.js
23+
FAIL __tests__/sync.test.js
524
Custom matcher
625
passes
726
fails
@@ -19,10 +38,10 @@ FAIL __tests__/customMatcher.test.js
1938
47 |
2039
48 | // This expecation fails due to an error we throw (intentionally)
2140
22-
at Error (__tests__/customMatcher.test.js:45:13)
23-
at baz (__tests__/customMatcher.test.js:43:23)
24-
at bar (__tests__/customMatcher.test.js:42:23)
25-
at foo (__tests__/customMatcher.test.js:52:7)
26-
at Object.callback (__tests__/customMatcher.test.js:11:18)
27-
at Object.toCustomMatch (__tests__/customMatcher.test.js:53:8)
41+
at Error (__tests__/sync.test.js:45:13)
42+
at baz (__tests__/sync.test.js:43:23)
43+
at bar (__tests__/sync.test.js:42:23)
44+
at foo (__tests__/sync.test.js:52:7)
45+
at Object.callback (__tests__/sync.test.js:11:18)
46+
at Object.toCustomMatch (__tests__/sync.test.js:53:8)
2847
`;

e2e/__tests__/__snapshots__/expectAsyncMatcher.test.js.snap

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,40 @@ FAIL __tests__/failure.test.js
1010
● fail with expected non promise values
1111
1212
Expected value to have length:
13+
2
14+
Received:
15+
1
16+
received.length:
17+
1
18+
19+
11 |
20+
12 | it('fail with expected non promise values', () =>
21+
> 13 | expect([1]).toHaveLengthAsync(Promise.resolve(2)));
22+
| ^
23+
14 |
24+
15 | it('fail with expected non promise values and not', () =>
25+
16 | expect([1, 2]).not.toHaveLengthAsync(Promise.resolve(2)));
1326
14-
2
15-
Received:
16-
1
17-
received.length:
18-
1
27+
at Object.toHaveLengthAsync (__tests__/failure.test.js:13:15)
1928
2029
● fail with expected non promise values and not
2130
2231
Expected value to not have length:
32+
2
33+
Received:
34+
1,2
35+
received.length:
36+
2
37+
38+
14 |
39+
15 | it('fail with expected non promise values and not', () =>
40+
> 16 | expect([1, 2]).not.toHaveLengthAsync(Promise.resolve(2)));
41+
| ^
42+
17 |
43+
18 | it('fail with expected promise values', () =>
44+
19 | expect(Promise.resolve([1])).resolves.toHaveLengthAsync(Promise.resolve(2)));
2345
24-
2
25-
Received:
26-
1,2
27-
received.length:
28-
2
46+
at Object.toHaveLengthAsync (__tests__/failure.test.js:16:22)
2947
3048
● fail with expected promise values
3149
@@ -36,19 +54,15 @@ FAIL __tests__/failure.test.js
3654
received.length:
3755
1
3856
39-
22 |
40-
23 | it('fail with expected promise values', async () => {
41-
> 24 | await (expect(Promise.resolve([1])): any).resolves.toHaveLengthAsync(
42-
| ^
43-
25 | Promise.resolve(2)
44-
26 | );
45-
27 | });
57+
17 |
58+
18 | it('fail with expected promise values', () =>
59+
> 19 | expect(Promise.resolve([1])).resolves.toHaveLengthAsync(Promise.resolve(2)));
60+
| ^
61+
20 |
62+
21 | it('fail with expected promise values and not', () =>
63+
22 | expect(Promise.resolve([1, 2])).resolves.not.toHaveLengthAsync(
4664
47-
at Object.toHaveLengthAsync (__tests__/failure.test.js:24:54)
48-
at asyncGeneratorStep (__tests__/failure.test.js:11:103)
49-
at _next (__tests__/failure.test.js:13:194)
50-
at __tests__/failure.test.js:13:364
51-
at Object.<anonymous> (__tests__/failure.test.js:13:97)
65+
at Object.toHaveLengthAsync (__tests__/failure.test.js:19:41)
5266
5367
● fail with expected promise values and not
5468
@@ -59,17 +73,13 @@ FAIL __tests__/failure.test.js
5973
received.length:
6074
2
6175
62-
28 |
63-
29 | it('fail with expected promise values and not', async () => {
64-
> 30 | await (expect(Promise.resolve([1, 2])).resolves.not: any).toHaveLengthAsync(
65-
| ^
66-
31 | Promise.resolve(2)
67-
32 | );
68-
33 | });
69-
70-
at Object.toHaveLengthAsync (__tests__/failure.test.js:30:61)
71-
at asyncGeneratorStep (__tests__/failure.test.js:11:103)
72-
at _next (__tests__/failure.test.js:13:194)
73-
at __tests__/failure.test.js:13:364
74-
at Object.<anonymous> (__tests__/failure.test.js:13:97)
76+
20 |
77+
21 | it('fail with expected promise values and not', () =>
78+
> 22 | expect(Promise.resolve([1, 2])).resolves.not.toHaveLengthAsync(
79+
| ^
80+
23 | Promise.resolve(2)
81+
24 | ));
82+
25 |
83+
84+
at Object.toHaveLengthAsync (__tests__/failure.test.js:22:48)
7585
`;

e2e/__tests__/customMatcherStackTrace.test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {extractSummary} from '../Utils';
1212
import {wrap} from 'jest-snapshot-serializer-raw';
1313

1414
test('works with custom matchers', () => {
15-
const {stderr} = runJest('custom-matcher-stack-trace');
15+
const {stderr} = runJest('custom-matcher-stack-trace', ['sync.test.js']);
1616

1717
let {rest} = extractSummary(stderr);
1818

@@ -23,3 +23,13 @@ test('works with custom matchers', () => {
2323

2424
expect(wrap(rest)).toMatchSnapshot();
2525
});
26+
27+
test('custom async matchers', () => {
28+
const {stderr} = runJest('custom-matcher-stack-trace', [
29+
'asynchronous.test.js',
30+
]);
31+
32+
const {rest} = extractSummary(stderr);
33+
34+
expect(wrap(rest)).toMatchSnapshot();
35+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
expect.extend({toThrowCustomAsyncMatcherError});
9+
10+
test('showing the stack trace for an async matcher', async () => {
11+
await expect(true).toThrowCustomAsyncMatcherError();
12+
});
13+
14+
async function toThrowCustomAsyncMatcherError() {
15+
const message = () =>
16+
'We expect the stack trace and code fence for this matcher to be shown in the console.';
17+
return {message, pass: false};
18+
}

e2e/custom-matcher-stack-trace/__tests__/customMatcher.test.js renamed to e2e/custom-matcher-stack-trace/__tests__/sync.test.js

File renamed without changes.

e2e/expect-async-matcher/__tests__/failure.test.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,21 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
7-
'use strict';
87

98
import {toHaveLengthAsync} from '../matchers';
109

11-
expect.extend({
12-
toHaveLengthAsync,
13-
});
10+
expect.extend({toHaveLengthAsync});
1411

15-
it('fail with expected non promise values', async () => {
16-
await (expect([1]): any).toHaveLengthAsync(Promise.resolve(2));
17-
});
12+
it('fail with expected non promise values', () =>
13+
expect([1]).toHaveLengthAsync(Promise.resolve(2)));
1814

19-
it('fail with expected non promise values and not', async () => {
20-
await (expect([1, 2]): any).not.toHaveLengthAsync(Promise.resolve(2));
21-
});
15+
it('fail with expected non promise values and not', () =>
16+
expect([1, 2]).not.toHaveLengthAsync(Promise.resolve(2)));
2217

23-
it('fail with expected promise values', async () => {
24-
await (expect(Promise.resolve([1])): any).resolves.toHaveLengthAsync(
25-
Promise.resolve(2)
26-
);
27-
});
18+
it('fail with expected promise values', () =>
19+
expect(Promise.resolve([1])).resolves.toHaveLengthAsync(Promise.resolve(2)));
2820

29-
it('fail with expected promise values and not', async () => {
30-
await (expect(Promise.resolve([1, 2])).resolves.not: any).toHaveLengthAsync(
21+
it('fail with expected promise values and not', () =>
22+
expect(Promise.resolve([1, 2])).resolves.not.toHaveLengthAsync(
3123
Promise.resolve(2)
32-
);
33-
});
24+
));

e2e/expect-async-matcher/__tests__/success.test.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,21 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
7-
'use strict';
87

98
import {toHaveLengthAsync} from '../matchers';
109

11-
expect.extend({
12-
toHaveLengthAsync,
13-
});
10+
expect.extend({toHaveLengthAsync});
1411

15-
it('works with expected non promise values', async () => {
16-
await (expect([1]): any).toHaveLengthAsync(Promise.resolve(1));
17-
});
12+
it('works with expected non promise values', () =>
13+
expect([1]).toHaveLengthAsync(Promise.resolve(1)));
1814

19-
it('works with expected non promise values and not', async () => {
20-
await (expect([1, 2]): any).not.toHaveLengthAsync(Promise.resolve(1));
21-
});
15+
it('works with expected non promise values and not', () =>
16+
expect([1, 2]).not.toHaveLengthAsync(Promise.resolve(1)));
2217

23-
it('works with expected promise values', async () => {
24-
await (expect(Promise.resolve([1])).resolves: any).toHaveLengthAsync(
25-
Promise.resolve(1)
26-
);
27-
});
18+
it('works with expected promise values', () =>
19+
expect(Promise.resolve([1])).resolves.toHaveLengthAsync(Promise.resolve(1)));
2820

29-
it('works with expected promise values and not', async () => {
30-
await (expect(Promise.resolve([1, 2])).resolves.not: any).toHaveLengthAsync(
21+
it('works with expected promise values and not', () =>
22+
expect(Promise.resolve([1, 2])).resolves.not.toHaveLengthAsync(
3123
Promise.resolve(1)
32-
);
33-
});
24+
));

0 commit comments

Comments
 (0)