From f171d381ba43e978b6f2bc340c463b7e966f158f Mon Sep 17 00:00:00 2001 From: Marouane Fazouane Date: Thu, 24 Dec 2020 14:08:42 +0100 Subject: [PATCH] Make testFailureExitCode compatible with bail option (#10958) --- CHANGELOG.md | 1 + e2e/__tests__/testFailureExitCode.test.ts | 26 +++++++++++++++++++ packages/jest-core/src/TestScheduler.ts | 3 ++- .../src/__tests__/ReactElement.test.ts | 2 +- .../src/__tests__/react.test.tsx | 2 +- 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89a2b36721dc..9624f6ba6f75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ - `[expect]` [**BREAKING**] Make `toContain` more strict with the received type ([#10119](https://github.com/facebook/jest/pull/10119) & [#10929](https://github.com/facebook/jest/pull/10929)) - `[jest-circus]` Fixed the issue of beforeAll & afterAll hooks getting executed even if it is inside a skipped `describe` block [#10451](https://github.com/facebook/jest/issues/10451) - `[jest-circus]` Fix `testLocation` on Windows when using `test.each` ([#10871](https://github.com/facebook/jest/pull/10871)) +- `[jest-cli]` Use testFailureExitCode when bailing from a failed test ([#10958](https://github.com/facebook/jest/pull/10958)) - `[jest-config]` [**BREAKING**] Change default file extension order by moving json behind ts and tsx ([10572](https://github.com/facebook/jest/pull/10572)) - `[jest-console]` `console.dir` now respects the second argument correctly ([#10638](https://github.com/facebook/jest/pull/10638)) - `[jest-each]` [**BREAKING**] Ignore excess words in headings ([#8766](https://github.com/facebook/jest/pull/8766)) diff --git a/e2e/__tests__/testFailureExitCode.test.ts b/e2e/__tests__/testFailureExitCode.test.ts index a7c8506721fb..cffd1bcf20f9 100644 --- a/e2e/__tests__/testFailureExitCode.test.ts +++ b/e2e/__tests__/testFailureExitCode.test.ts @@ -38,3 +38,29 @@ test('exits with a specified code when test fail', () => { ({exitCode} = runJest(DIR)); expect(exitCode).toBe(1); }); + +test('exits with a specified code when bailing from a failed test', () => { + writeFiles(DIR, { + '__tests__/test.test.js': `test('test', () => { expect(1).toBe(2); });`, + '__tests__/test2.test.js': `test('test2', () => { expect(1).toBe(2); });`, + 'package.json': JSON.stringify({ + jest: {testEnvironment: 'node', testFailureExitCode: 99}, + }), + }); + + let {exitCode} = runJest(DIR, ['--bail']); + expect(exitCode).toBe(99); + + ({exitCode} = runJest(DIR, ['--bail', '--testFailureExitCode', '77'])); + expect(exitCode).toBe(77); + + writeFiles(DIR, { + '__tests__/test.test.js': `test('test', () => { expect(1).toBe(2); });`, + '__tests__/test2.test.js': `test('test2', () => { expect(1).toBe(2); });`, + 'package.json': JSON.stringify({ + jest: {testEnvironment: 'node'}, + }), + }); + ({exitCode} = runJest(DIR)); + expect(exitCode).toBe(1); +}); diff --git a/packages/jest-core/src/TestScheduler.ts b/packages/jest-core/src/TestScheduler.ts index fa75861835af..8afb21af452d 100644 --- a/packages/jest-core/src/TestScheduler.ts +++ b/packages/jest-core/src/TestScheduler.ts @@ -436,7 +436,8 @@ export default class TestScheduler { try { await this._dispatcher.onRunComplete(contexts, aggregatedResults); } finally { - exit(1); + const exitCode = this._globalConfig.testFailureExitCode; + exit(exitCode); } } } diff --git a/packages/pretty-format/src/__tests__/ReactElement.test.ts b/packages/pretty-format/src/__tests__/ReactElement.test.ts index 911048067c38..1f8aa1fdb9b0 100644 --- a/packages/pretty-format/src/__tests__/ReactElement.test.ts +++ b/packages/pretty-format/src/__tests__/ReactElement.test.ts @@ -15,7 +15,7 @@ setPrettyPrint([ReactElement]); describe('ReactElement Plugin', () => { let forwardRefComponent: { - (_props: unknown, _ref: unknown): unknown; + (_props: unknown, _ref: unknown): React.ReactElement | null; displayName?: string; }; diff --git a/packages/pretty-format/src/__tests__/react.test.tsx b/packages/pretty-format/src/__tests__/react.test.tsx index 0e636b0eb648..5e8ba13f9c87 100644 --- a/packages/pretty-format/src/__tests__/react.test.tsx +++ b/packages/pretty-format/src/__tests__/react.test.tsx @@ -706,7 +706,7 @@ test('ReactTestComponent plugin highlights syntax with color from theme option', }); test('supports forwardRef with a child', () => { - function Cat(props: any) { + function Cat(props: any, _ref: any) { return React.createElement('div', props, props.children); }