Skip to content

Commit 4c4162b

Browse files
authored
fix(jest-runner): handle test failures with circular objects (#10981)
1 parent c2f152d commit 4c4162b

4 files changed

Lines changed: 118 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
- `[jest-reporter]` Handle empty files when reporting code coverage with V8 ([#10819](https://github.com/facebook/jest/pull/10819))
4242
- `[jest-resolve]` Replace read-pkg-up with escalade package ([#10781](https://github.com/facebook/jest/pull/10781))
4343
- `[jest-resolve]` Disable `jest-pnp-resolver` for Yarn 2 ([#10847](https://github.com/facebook/jest/pull/10847))
44+
- `[jest-runner]` Handle test failures with circular objects ([#10981](https://github.com/facebook/jest/pull/10981))
4445
- `[jest-runtime]` [**BREAKING**] Do not inject `global` variable into module wrapper ([#10644](https://github.com/facebook/jest/pull/10644))
4546
- `[jest-runtime]` [**BREAKING**] remove long-deprecated `jest.addMatchers`, `jest.resetModuleRegistry`, and `jest.runTimersToTime` ([#9853](https://github.com/facebook/jest/pull/9853))
4647
- `[jest-runtime]` Fix stack overflow and promise deadlock when importing mutual dependant ES module ([#10892](https://github.com/facebook/jest/pull/10892))
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`handles circular inequality properly 1`] = `
4+
FAIL __tests__/test-1.js
5+
● test
6+
7+
expect(received).toEqual(expected) // deep equality
8+
9+
- Expected - 1
10+
+ Received + 3
11+
12+
- Object {}
13+
+ Object {
14+
+ "ref": [Circular],
15+
+ }
16+
17+
3 | foo.ref = foo;
18+
4 |
19+
> 5 | expect(foo).toEqual({});
20+
| ^
21+
6 | });
22+
23+
at Object.toEqual (__tests__/test-1.js:5:15)
24+
25+
FAIL __tests__/test-2.js
26+
● test
27+
28+
expect(received).toEqual(expected) // deep equality
29+
30+
- Expected - 1
31+
+ Received + 3
32+
33+
- Object {}
34+
+ Object {
35+
+ "ref": [Circular],
36+
+ }
37+
38+
3 | foo.ref = foo;
39+
4 |
40+
> 5 | expect(foo).toEqual({});
41+
| ^
42+
6 | });
43+
44+
at Object.toEqual (__tests__/test-2.js:5:15)
45+
`;
46+
47+
exports[`handles circular inequality properly 2`] = `
48+
Test Suites: 2 failed, 2 total
49+
Tests: 2 failed, 2 total
50+
Snapshots: 0 total
51+
Time: <<REPLACED>>
52+
Ran all test suites.
53+
`;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
import {tmpdir} from 'os';
9+
import * as path from 'path';
10+
import {wrap} from 'jest-snapshot-serializer-raw';
11+
import {
12+
cleanup,
13+
createEmptyPackage,
14+
extractSortedSummary,
15+
writeFiles,
16+
} from '../Utils';
17+
import {runContinuous} from '../runJest';
18+
19+
const tempDir = path.resolve(tmpdir(), 'circular-inequality-test');
20+
21+
beforeEach(() => {
22+
createEmptyPackage(tempDir);
23+
});
24+
25+
afterEach(() => {
26+
cleanup(tempDir);
27+
});
28+
29+
test('handles circular inequality properly', async () => {
30+
const testFileContent = `
31+
it('test', () => {
32+
const foo = {};
33+
foo.ref = foo;
34+
35+
expect(foo).toEqual({});
36+
});
37+
`;
38+
39+
writeFiles(tempDir, {
40+
'__tests__/test-1.js': testFileContent,
41+
'__tests__/test-2.js': testFileContent,
42+
});
43+
44+
const {end, waitUntil} = runContinuous(
45+
tempDir,
46+
['--no-watchman', '--watch-all'],
47+
// timeout in case the `waitUntil` below doesn't fire
48+
{stripAnsi: true, timeout: 5000},
49+
);
50+
51+
await waitUntil(({stderr}) => stderr.includes('Ran all test suites.'));
52+
53+
const {stderr} = await end();
54+
55+
const {summary, rest} = extractSortedSummary(stderr);
56+
expect(wrap(rest)).toMatchSnapshot();
57+
expect(wrap(summary)).toMatchSnapshot();
58+
});

packages/jest-runner/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,12 @@ export default class TestRunner {
166166

167167
const worker = new Worker(TEST_WORKER_PATH, {
168168
exposedMethods: ['worker'],
169-
forkOptions: {stdio: 'pipe'},
169+
forkOptions: {
170+
// use advanced serialization in order to transfer objects with circular references
171+
// @ts-expect-error: option does not exist on the node 10 types
172+
serialization: 'advanced',
173+
stdio: 'pipe',
174+
},
170175
maxRetries: 3,
171176
numWorkers: this._globalConfig.maxWorkers,
172177
setupArgs: [

0 commit comments

Comments
 (0)