Skip to content

Commit

Permalink
fix(jest-runner): handle test failures with circular objects (#10981)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Dec 30, 2020
1 parent c2f152d commit 4c4162b
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- `[jest-reporter]` Handle empty files when reporting code coverage with V8 ([#10819](https://github.com/facebook/jest/pull/10819))
- `[jest-resolve]` Replace read-pkg-up with escalade package ([#10781](https://github.com/facebook/jest/pull/10781))
- `[jest-resolve]` Disable `jest-pnp-resolver` for Yarn 2 ([#10847](https://github.com/facebook/jest/pull/10847))
- `[jest-runner]` Handle test failures with circular objects ([#10981](https://github.com/facebook/jest/pull/10981))
- `[jest-runtime]` [**BREAKING**] Do not inject `global` variable into module wrapper ([#10644](https://github.com/facebook/jest/pull/10644))
- `[jest-runtime]` [**BREAKING**] remove long-deprecated `jest.addMatchers`, `jest.resetModuleRegistry`, and `jest.runTimersToTime` ([#9853](https://github.com/facebook/jest/pull/9853))
- `[jest-runtime]` Fix stack overflow and promise deadlock when importing mutual dependant ES module ([#10892](https://github.com/facebook/jest/pull/10892))
Expand Down
53 changes: 53 additions & 0 deletions e2e/__tests__/__snapshots__/circularInequality.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`handles circular inequality properly 1`] = `
FAIL __tests__/test-1.js
● test
expect(received).toEqual(expected) // deep equality
- Expected - 1
+ Received + 3
- Object {}
+ Object {
+ "ref": [Circular],
+ }
3 | foo.ref = foo;
4 |
> 5 | expect(foo).toEqual({});
| ^
6 | });
at Object.toEqual (__tests__/test-1.js:5:15)
FAIL __tests__/test-2.js
● test
expect(received).toEqual(expected) // deep equality
- Expected - 1
+ Received + 3
- Object {}
+ Object {
+ "ref": [Circular],
+ }
3 | foo.ref = foo;
4 |
> 5 | expect(foo).toEqual({});
| ^
6 | });
at Object.toEqual (__tests__/test-2.js:5:15)
`;

exports[`handles circular inequality properly 2`] = `
Test Suites: 2 failed, 2 total
Tests: 2 failed, 2 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites.
`;
58 changes: 58 additions & 0 deletions e2e/__tests__/circularInequality.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {tmpdir} from 'os';
import * as path from 'path';
import {wrap} from 'jest-snapshot-serializer-raw';
import {
cleanup,
createEmptyPackage,
extractSortedSummary,
writeFiles,
} from '../Utils';
import {runContinuous} from '../runJest';

const tempDir = path.resolve(tmpdir(), 'circular-inequality-test');

beforeEach(() => {
createEmptyPackage(tempDir);
});

afterEach(() => {
cleanup(tempDir);
});

test('handles circular inequality properly', async () => {
const testFileContent = `
it('test', () => {
const foo = {};
foo.ref = foo;
expect(foo).toEqual({});
});
`;

writeFiles(tempDir, {
'__tests__/test-1.js': testFileContent,
'__tests__/test-2.js': testFileContent,
});

const {end, waitUntil} = runContinuous(
tempDir,
['--no-watchman', '--watch-all'],
// timeout in case the `waitUntil` below doesn't fire
{stripAnsi: true, timeout: 5000},
);

await waitUntil(({stderr}) => stderr.includes('Ran all test suites.'));

const {stderr} = await end();

const {summary, rest} = extractSortedSummary(stderr);
expect(wrap(rest)).toMatchSnapshot();
expect(wrap(summary)).toMatchSnapshot();
});
7 changes: 6 additions & 1 deletion packages/jest-runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,12 @@ export default class TestRunner {

const worker = new Worker(TEST_WORKER_PATH, {
exposedMethods: ['worker'],
forkOptions: {stdio: 'pipe'},
forkOptions: {
// use advanced serialization in order to transfer objects with circular references
// @ts-expect-error: option does not exist on the node 10 types
serialization: 'advanced',
stdio: 'pipe',
},
maxRetries: 3,
numWorkers: this._globalConfig.maxWorkers,
setupArgs: [
Expand Down

0 comments on commit 4c4162b

Please sign in to comment.