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

[Circus] Add timeout event, and don't print both timeout and expect.assertions error #7201

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -45,6 +45,7 @@
- `[jest-runtime]` Fix missing coverage when using negative glob pattern in `testMatch` ([#7170](https://github.com/facebook/jest/pull/7170))
- `[*]` Ensure `maxWorkers` is at least 1 (was 0 in some cases where there was only 1 CPU) ([#7182](https://github.com/facebook/jest/pull/7182))
- `[jest-runtime]` Fix transform cache invalidation when requiring a test file from multiple projects ([#7186](https://github.com/facebook/jest/pull/7186))
- `[jest-circus]` Don't print both timeout and expect.assertions error ([#7201](https://github.com/facebook/jest/pull/7201))

### Chore & Maintenance

Expand Down
19 changes: 19 additions & 0 deletions e2e/__tests__/__snapshots__/timeouts.assertions.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`exceeds the timeout synchronously 1`] = `
"Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites.
"
`;

exports[`exceeds the timeout, without reporting expect.assertions 1`] = `
"Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites.
"
`;
71 changes: 71 additions & 0 deletions e2e/__tests__/timeouts.assertions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. 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.
* @flow
*/

'use strict';

import path from 'path';
import {cleanup, extractSummary, writeFiles} from '../Utils';
import {skipSuiteOnJasmine} from '../../scripts/ConditionalTest';

import runJest from '../runJest';

const DIR = path.resolve(__dirname, '../timeouts');

skipSuiteOnJasmine();
beforeEach(() => cleanup(DIR));
afterAll(() => cleanup(DIR));

test('exceeds the timeout, without reporting expect.assertions', () => {
writeFiles(DIR, {
'__tests__/a-banana.js': `
jest.setTimeout(20);

test('banana', () => {
expect.assertions(2);
return new Promise(resolve => {
setTimeout(resolve, 100);
});
});
`,
'package.json': '{}',
});

const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false']);
const {rest, summary} = extractSummary(stderr);
expect(rest).toMatch(
/(jest\.setTimeout|jasmine\.DEFAULT_TIMEOUT_INTERVAL|Exceeded timeout)/,
rickhanlonii marked this conversation as resolved.
Show resolved Hide resolved
);
expect(rest).not.toMatch(/Expected two assertions/);
expect(summary).toMatchSnapshot();
expect(status).toBe(1);
});

test('exceeds the timeout synchronously', () => {
writeFiles(DIR, {
'__tests__/a-banana.js': `
jest.setTimeout(20);

test('banana', () => {
expect.assertions(2);
const startTime = Date.now();
while (Date.now() - startTime < 100) {
}
});
`,
'package.json': '{}',
});

const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false']);
const {rest, summary} = extractSummary(stderr);
expect(rest).toMatch(
/(jest\.setTimeout|jasmine\.DEFAULT_TIMEOUT_INTERVAL|Exceeded timeout)/,
);
expect(rest).not.toMatch(/Expected two assertions/);
expect(summary).toMatchSnapshot();
expect(status).toBe(1);
});
4 changes: 4 additions & 0 deletions packages/jest-circus/src/event_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ const handler: EventHandler = (event, state): void => {
event.test.errors.push([error, asyncError]);
break;
}
case 'test_fn_timeout': {
event.test.expired = true;
break;
}
case 'test_retry': {
event.test.errors = [];
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ const eventHandler = (event: Event) => {
};

const _addExpectedAssertionErrors = (test: TestEntry) => {
if (test.expired) return;
Copy link
Member

Choose a reason for hiding this comment

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

instead of a new prop here, we could also check if test.errors > 0 and then not add from expect?


const failures = extractExpectedAssertionsErrors();
const errors = failures.map(failure => failure.error);
test.errors = test.errors.concat(errors);
Expand Down
9 changes: 7 additions & 2 deletions packages/jest-circus/src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const _runTest = async (test: TestEntry): Promise<void> => {

// `afterAll` hooks should not affect test status (pass or fail), because if
// we had a global `afterAll` hook it would block all existing tests until
// this hook is executed. So we dispatche `test_done` right away.
// this hook is executed. So we dispatch `test_done` right away.
dispatch({name: 'test_done', test});
};

Expand Down Expand Up @@ -160,7 +160,12 @@ const _callCircusTest = (

return callAsyncCircusFn(test.fn, testContext, {isHook: false, timeout})
.then(() => dispatch({name: 'test_fn_success', test}))
.catch(error => dispatch({error, name: 'test_fn_failure', test}));
.catch(message => {
if (/^(Error: )?Exceeded timeout/.test(message)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Not crazy about string matching here, any alternatives?

Copy link
Member

Choose a reason for hiding this comment

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

We might consider adding a property to the error we reject with, which can be set to timeout, assertion etc? Kind of like code for node core errors. Not sure

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought to do that but we don't reject with an Error we reject with a message (oops)

Copy link
Member

Choose a reason for hiding this comment

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

seems fixable :D

dispatch({name: 'test_fn_timeout', test});
}
dispatch({error: message, name: 'test_fn_failure', test});
});
};

export default run;
1 change: 1 addition & 0 deletions packages/jest-circus/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const makeTest = (
asyncError,
duration: null,
errors: [],
expired: false,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if timedOut would be better name?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was between both, not strongly opinionated either way

Copy link
Member

@SimenB SimenB Oct 18, 2018

Choose a reason for hiding this comment

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

more of a fan of failureReason: 'timeout' | 'error' | 'assertion-error' or something.
error being anything not JestAssertionError (and maybe node core assertion)

fn,
invocations: 0,
mode: _mode,
Expand Down
7 changes: 6 additions & 1 deletion types/Circus.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export type Event =
error: Exception,
test: TestEntry,
|}
| {|
name: 'test_fn_timeout',
test: TestEntry,
|}
| {|
name: 'test_retry',
test: TestEntry,
Expand All @@ -113,7 +117,7 @@ export type Event =
// test failure is defined by presence of errors in `test.errors`,
// `test_done` indicates that the test and all its hooks were run,
// and nothing else will change it's state in the future. (except third
// party extentions/plugins)
// party extensions/plugins)
name: 'test_done',
test: TestEntry,
|}
Expand Down Expand Up @@ -201,6 +205,7 @@ type TestError = Exception | Array<[?Exception, Exception]>; // the error from t
export type TestEntry = {|
asyncError: Exception, // Used if the test failure contains no usable stack trace
errors: TestError,
expired: ?boolean,
fn: ?TestFn,
invocations: number,
mode: TestMode,
Expand Down