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

Do not retry test if beforeAll fails #8227

Merged
merged 6 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -13,6 +13,7 @@

### Fixes

- `[jest-circus]` Fix test retries with beforeAll/beforeEach failures ([#8227](https://github.com/facebook/jest/pull/8227))
- `[expect]` Fix circular references in iterable equality ([#8160](https://github.com/facebook/jest/pull/8160))
- `[jest-changed-files]` Change method of obtaining git root ([#8052](https://github.com/facebook/jest/pull/8052))
- `[jest-each]` Fix test function type ([#8145](https://github.com/facebook/jest/pull/8145))
Expand Down
31 changes: 31 additions & 0 deletions e2e/__tests__/testRetries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,35 @@ describe('Test Retries', () => {
expect(jsonResult.numPendingTests).toBe(0);
expect(jsonResult.testResults[0].testResults[0].invocations).toBe(1);
});

it('tests are not retried if beforeAll hook failure occurs', () => {
let jsonResult;

const reporterConfig = {
reporters: [
['<rootDir>/reporters/RetryReporter.js', {output: outputFilePath}],
],
};

runJest('test-retries', [
'--config',
JSON.stringify(reporterConfig),
'beforeAllFailure.test.js',
]);

const testOutput = fs.readFileSync(outputFilePath, 'utf8');

try {
jsonResult = JSON.parse(testOutput);
} catch (err) {
throw new Error(
`Can't parse the JSON result from ${outputFileName}, ${err.toString()}`,
);
}

expect(jsonResult.numPassedTests).toBe(0);
expect(jsonResult.numFailedTests).toBe(1);
expect(jsonResult.numPendingTests).toBe(0);
expect(jsonResult.testResults[0].testResults[0].invocations).toBe(1);
});
});
17 changes: 17 additions & 0 deletions e2e/test-retries/__tests__/beforeAllFailure.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* 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.
*/
'use strict';

jest.retryTimes(3);

beforeAll(() => {
throw new Error('Failure in beforeAll');
});

it('should not be retried because hook failure occurred', () => {
throw new Error('should not be invoked');
});
7 changes: 6 additions & 1 deletion packages/jest-circus/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ const _runTestsForDescribeBlock = async (describeBlock: DescribeBlock) => {
const deferredRetryTests = [];

for (const test of describeBlock.tests) {
const hasErrorsBeforeTestRun = test.errors.length > 0;
await _runTest(test);

if (retryTimes > 0 && test.errors.length > 0) {
if (
hasErrorsBeforeTestRun === false &&
retryTimes > 0 &&
test.errors.length > 0
) {
deferredRetryTests.push(test);
}
}
Expand Down