diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b9af485332e..9ccf8f3e6c99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/e2e/__tests__/testRetries.test.ts b/e2e/__tests__/testRetries.test.ts index 6ae9ff75b2d7..3516c01b024c 100644 --- a/e2e/__tests__/testRetries.test.ts +++ b/e2e/__tests__/testRetries.test.ts @@ -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: [ + ['/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); + }); }); diff --git a/e2e/test-retries/__tests__/beforeAllFailure.test.js b/e2e/test-retries/__tests__/beforeAllFailure.test.js new file mode 100644 index 000000000000..915ee923a29d --- /dev/null +++ b/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'); +}); diff --git a/packages/jest-circus/src/run.ts b/packages/jest-circus/src/run.ts index 04d5b8459b8d..fcff37cd826a 100644 --- a/packages/jest-circus/src/run.ts +++ b/packages/jest-circus/src/run.ts @@ -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); } }