From a89be24c4defc0e9d8b80a5836f8e48d1afd3ba1 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 24 Dec 2020 13:48:07 +0100 Subject: [PATCH 1/3] chore: improve git version detection in tests (#10978) Co-authored-by: Marouane --- e2e/__tests__/jestChangedFiles.test.ts | 4 ++-- e2e/__tests__/onlyChanged.test.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/e2e/__tests__/jestChangedFiles.test.ts b/e2e/__tests__/jestChangedFiles.test.ts index 5fc0746cdb51..46cf856616f2 100644 --- a/e2e/__tests__/jestChangedFiles.test.ts +++ b/e2e/__tests__/jestChangedFiles.test.ts @@ -21,9 +21,9 @@ const HG = 'hg --config ui.username=jest_test'; const gitVersionSupportsInitialBranch = (() => { const {stdout} = run(`${GIT} --version`); - const gitVersion = stdout.split(' ').slice(-1)[0]; + const gitVersion = stdout.trim(); - const match = gitVersion.match(/(?\d+\.\d+\.\d+)/); + const match = gitVersion.match(/^git version (?\d+\.\d+\.\d+)/); if (match?.groups?.version == null) { throw new Error(`Unable to parse git version from string "${gitVersion}"`); diff --git a/e2e/__tests__/onlyChanged.test.ts b/e2e/__tests__/onlyChanged.test.ts index cc988ed57b57..4b4a72fedcbd 100644 --- a/e2e/__tests__/onlyChanged.test.ts +++ b/e2e/__tests__/onlyChanged.test.ts @@ -17,9 +17,9 @@ const HG = 'hg --config ui.username=jest_test'; const gitVersionSupportsInitialBranch = (() => { const {stdout} = run(`${GIT} --version`); - const gitVersion = stdout.split(' ').slice(-1)[0]; + const gitVersion = stdout.trim(); - const match = gitVersion.match(/(?\d+\.\d+\.\d+)/); + const match = gitVersion.match(/^git version (?\d+\.\d+\.\d+)/); if (match?.groups?.version == null) { throw new Error(`Unable to parse git version from string "${gitVersion}"`); From f171d381ba43e978b6f2bc340c463b7e966f158f Mon Sep 17 00:00:00 2001 From: Marouane Fazouane Date: Thu, 24 Dec 2020 14:08:42 +0100 Subject: [PATCH 2/3] 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); } From 4102243a785305f0b466526569978145f6ebc832 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 24 Dec 2020 15:43:39 +0100 Subject: [PATCH 3/3] chore: make node-notifier a peer dep (#10977) --- CHANGELOG.md | 1 + package.json | 1 + packages/jest-cli/package.json | 8 +++++ packages/jest-core/package.json | 8 +++++ packages/jest-reporters/package.json | 9 ++++-- packages/jest-reporters/src/NotifyReporter.ts | 8 ++--- .../src/__tests__/NotifyReporter.test.ts | 5 ++-- packages/jest/package.json | 8 +++++ yarn.lock | 29 +++++++++++++++---- 9 files changed, 62 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9624f6ba6f75..ce9b4567a0a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,7 @@ - `[jest-resolve]` [**BREAKING**] Migrate to ESM ([#10688](https://github.com/facebook/jest/pull/10688)) - `[jest-resolve-dependencies]` [**BREAKING**] Migrate to ESM ([#10876](https://github.com/facebook/jest/pull/10876)) - `[jest-mock]` [**BREAKING**] Migrate to ESM ([#10887](https://github.com/facebook/jest/pull/10887)) +- `[jest-reporters]` [**BREAKING**] Make `node-notifier` a peer dependency ([#10977](https://github.com/facebook/jest/pull/10977)) - `[jest-resolve, jest-runtime]` [**BREAKING**] Use `Map`s instead of objects for all cached resources ([#10968](https://github.com/facebook/jest/pull/10968)) - `[jest-runner]` [**BREAKING**] Migrate to ESM ([#10900](https://github.com/facebook/jest/pull/10900)) - `[jest-runtime]` [**BREAKING**] Remove deprecated and unnused `getSourceMapInfo` from Runtime ([#9969](https://github.com/facebook/jest/pull/9969)) diff --git a/package.json b/package.json index 4282de5d4ccc..e5201f422d56 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,7 @@ "micromatch": "^4.0.2", "mlh-tsd": "^0.14.1", "mock-fs": "^4.4.1", + "node-notifier": "^9.0.0", "prettier": "^2.1.1", "progress": "^2.0.0", "promise": "^8.0.2", diff --git a/packages/jest-cli/package.json b/packages/jest-cli/package.json index cae92340097f..abe2bd0cb69b 100644 --- a/packages/jest-cli/package.json +++ b/packages/jest-cli/package.json @@ -32,6 +32,14 @@ "@types/prompts": "^2.0.1", "@types/yargs": "^15.0.0" }, + "peerDependencies": { + "node-notifier": "^8.0.1 || ^9.0.0" + }, + "peerDependenciesMeta": { + "node-notifier": { + "optional": true + } + }, "bin": { "jest": "./bin/jest.js" }, diff --git a/packages/jest-core/package.json b/packages/jest-core/package.json index 36cf849c4a14..87384a38e47e 100644 --- a/packages/jest-core/package.json +++ b/packages/jest-core/package.json @@ -48,6 +48,14 @@ "@types/rimraf": "^3.0.0", "jest-snapshot-serializer-raw": "^1.1.0" }, + "peerDependencies": { + "node-notifier": "^8.0.1 || ^9.0.0" + }, + "peerDependenciesMeta": { + "node-notifier": { + "optional": true + } + }, "engines": { "node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0" }, diff --git a/packages/jest-reporters/package.json b/packages/jest-reporters/package.json index 8dc5e88b3351..5651ce381006 100644 --- a/packages/jest-reporters/package.json +++ b/packages/jest-reporters/package.json @@ -48,8 +48,13 @@ "mock-fs": "^4.4.1", "strip-ansi": "^6.0.0" }, - "optionalDependencies": { - "node-notifier": "^8.0.0" + "peerDependencies": { + "node-notifier": "^8.0.1 || ^9.0.0" + }, + "peerDependenciesMeta": { + "node-notifier": { + "optional": true + } }, "engines": { "node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0" diff --git a/packages/jest-reporters/src/NotifyReporter.ts b/packages/jest-reporters/src/NotifyReporter.ts index 54579e1f5b50..16ba761c34a5 100644 --- a/packages/jest-reporters/src/NotifyReporter.ts +++ b/packages/jest-reporters/src/NotifyReporter.ts @@ -145,10 +145,10 @@ function loadNotifier(): typeof import('node-notifier') { } catch (err) { if (err.code !== 'MODULE_NOT_FOUND') { throw err; - } else { - throw Error( - 'notify reporter requires optional dependeny node-notifier but it was not found', - ); } + + throw Error( + 'notify reporter requires optional peer dependency "node-notifier" but it was not found', + ); } } diff --git a/packages/jest-reporters/src/__tests__/NotifyReporter.test.ts b/packages/jest-reporters/src/__tests__/NotifyReporter.test.ts index e6a694d42634..3e9a22ea03fa 100644 --- a/packages/jest-reporters/src/__tests__/NotifyReporter.test.ts +++ b/packages/jest-reporters/src/__tests__/NotifyReporter.test.ts @@ -224,14 +224,13 @@ describe('node-notifier is an optional dependency', () => { test('without node-notifier uses mock function that throws an error', () => { jest.doMock('node-notifier', () => { - const error: unknown = new Resolver.ModuleNotFoundError( + throw new Resolver.ModuleNotFoundError( "Cannot find module 'node-notifier'", ); - throw error; }); expect(ctor).toThrow( - 'notify reporter requires optional dependeny node-notifier but it was not found', + 'notify reporter requires optional peer dependency "node-notifier" but it was not found', ); }); diff --git a/packages/jest/package.json b/packages/jest/package.json index d12716f0f1b7..7ca72aa5d923 100644 --- a/packages/jest/package.json +++ b/packages/jest/package.json @@ -14,6 +14,14 @@ "import-local": "^3.0.2", "jest-cli": "^27.0.0-next.2" }, + "peerDependencies": { + "node-notifier": "^8.0.1 || ^9.0.0" + }, + "peerDependenciesMeta": { + "node-notifier": { + "optional": true + } + }, "bin": "./bin/jest.js", "engines": { "node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0" diff --git a/yarn.lock b/yarn.lock index ff658db33ebe..7d6be98b256b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1820,6 +1820,11 @@ __metadata: rimraf: ^3.0.0 slash: ^3.0.0 strip-ansi: ^6.0.0 + peerDependencies: + node-notifier: ^8.0.1 || ^9.0.0 + peerDependenciesMeta: + node-notifier: + optional: true languageName: unknown linkType: soft @@ -1945,6 +1950,7 @@ __metadata: micromatch: ^4.0.2 mlh-tsd: ^0.14.1 mock-fs: ^4.4.1 + node-notifier: ^9.0.0 prettier: ^2.1.1 progress: ^2.0.0 promise: ^8.0.2 @@ -2000,14 +2006,15 @@ __metadata: jest-util: ^27.0.0-next.1 jest-worker: ^27.0.0-next.2 mock-fs: ^4.4.1 - node-notifier: ^8.0.0 slash: ^3.0.0 source-map: ^0.6.0 string-length: ^4.0.1 strip-ansi: ^6.0.0 terminal-link: ^2.0.0 v8-to-istanbul: ^7.0.0 - dependenciesMeta: + peerDependencies: + node-notifier: ^8.0.1 || ^9.0.0 + peerDependenciesMeta: node-notifier: optional: true languageName: unknown @@ -11591,6 +11598,11 @@ fsevents@^1.2.7: jest-validate: ^27.0.0-next.1 prompts: ^2.0.1 yargs: ^16.0.3 + peerDependencies: + node-notifier: ^8.0.1 || ^9.0.0 + peerDependenciesMeta: + node-notifier: + optional: true bin: jest: ./bin/jest.js languageName: unknown @@ -12362,6 +12374,11 @@ fsevents@^1.2.7: "@jest/core": ^27.0.0-next.2 import-local: ^3.0.2 jest-cli: ^27.0.0-next.2 + peerDependencies: + node-notifier: ^8.0.1 || ^9.0.0 + peerDependenciesMeta: + node-notifier: + optional: true bin: jest: ./bin/jest.js languageName: unknown @@ -14559,9 +14576,9 @@ fsevents@^1.2.7: languageName: node linkType: hard -"node-notifier@npm:^8.0.0": - version: 8.0.0 - resolution: "node-notifier@npm:8.0.0" +"node-notifier@npm:^9.0.0": + version: 9.0.0 + resolution: "node-notifier@npm:9.0.0" dependencies: growly: ^1.3.0 is-wsl: ^2.2.0 @@ -14569,7 +14586,7 @@ fsevents@^1.2.7: shellwords: ^0.1.1 uuid: ^8.3.0 which: ^2.0.2 - checksum: 3016eccb32cbfc0ec26129500570a0d875c32e28c43aef9c32d4cea24617cdd870eaf39247faffed5b89f78ef69ca4506270d2f8f76f027222597b700cc8aec9 + checksum: 10e6ba45afb246ea8bd0189960b883c4c0cb04de82a7c091b536f7652327910a4929fd3862cc32bdea20e53bb52a4ddae2ff1eb3aee751aba6e27242602193b3 languageName: node linkType: hard