Skip to content

Commit

Permalink
Do not retry test if beforeAll fails (#8227)
Browse files Browse the repository at this point in the history
## Summary

Fixes #8221

If test retries are enabled - do not retry tests when beforeAll/beforeEach fails.

## Test plan

Added additional e2e test to test for this regression
  • Loading branch information
palmerj3 authored and thymikee committed Mar 27, 2019
1 parent 0f43bdd commit e08be02
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
@@ -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
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

0 comments on commit e08be02

Please sign in to comment.