From 1a66bb0319614cbf1e171cc887213e63f844daf7 Mon Sep 17 00:00:00 2001 From: feng Date: Tue, 1 Mar 2022 09:43:16 +0800 Subject: [PATCH 1/5] feat(jest-test-result): add duration property to JSON test output --- CHANGELOG.md | 1 + docs/Configuration.md | 3 ++- e2e/__tests__/jsonReporter.test.ts | 18 +++++++++++++----- e2e/json-reporter/__tests__/sum.test.js | 4 ++++ .../jest-test-result/src/formatTestResults.ts | 1 + packages/jest-test-result/src/types.ts | 2 +- 6 files changed, 22 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a8c3863378b..ea2b792e9b41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ - `[jest-runner]` Allow passing `testEnvironmentOptions` via docblocks ([#12470](https://github.com/facebook/jest/pull/12470)) - `[jest-runtime]` [**BREAKING**] `Runtime.createHasteMap` now returns a promise ([#12008](https://github.com/facebook/jest/pull/12008)) - `[@jest/schemas]` New module for JSON schemas for Jest's config ([#12384](https://github.com/facebook/jest/pull/12384)) +- `[jest-test-result]` Add duration property to JSON test output ([#12518](https://github.com/facebook/jest/pull/12518)) - `[jest-worker]` [**BREAKING**] Allow only absolute `workerPath` ([#12343](https://github.com/facebook/jest/pull/12343)) - `[pretty-format]` New `maxWidth` parameter ([#12402](https://github.com/facebook/jest/pull/12402)) diff --git a/docs/Configuration.md b/docs/Configuration.md index b32c04460805..bc56d26e8f27 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -1280,7 +1280,8 @@ This option allows the use of a custom results processor. This processor must be "location": { "column": number, "line": number - } + }, + "duration": number | null }, ... ], diff --git a/e2e/__tests__/jsonReporter.test.ts b/e2e/__tests__/jsonReporter.test.ts index 3e8a3b6c0f16..e3532b404a4f 100644 --- a/e2e/__tests__/jsonReporter.test.ts +++ b/e2e/__tests__/jsonReporter.test.ts @@ -36,37 +36,45 @@ describe('JSON Reporter', () => { ); } - expect(jsonResult.numTotalTests).toBe(3); + expect(jsonResult.numTotalTests).toBe(4); expect(jsonResult.numTotalTestSuites).toBe(1); expect(jsonResult.numRuntimeErrorTestSuites).toBe(0); expect(jsonResult.numPassedTests).toBe(2); expect(jsonResult.numFailedTests).toBe(1); - expect(jsonResult.numPendingTests).toBe(0); + expect(jsonResult.numPendingTests).toBe(1); const noAncestors = jsonResult.testResults[0].assertionResults.find( item => item.title == 'no ancestors', ); let expected = {ancestorTitles: [] as Array}; expect(noAncestors).toEqual(expect.objectContaining(expected)); + expect(noAncestors).toHaveProperty('duration', expect.any(Number)); const addsNumbers = jsonResult.testResults[0].assertionResults.find( item => item.title == 'adds numbers', ); expected = {ancestorTitles: ['sum']}; expect(addsNumbers).toEqual(expect.objectContaining(expected)); + expect(addsNumbers).toHaveProperty('duration', expect.any(Number)); const failsTheTest = jsonResult.testResults[0].assertionResults.find( item => item.title == 'fails the test', ); expected = {ancestorTitles: ['sum', 'failing tests']}; expect(failsTheTest).toEqual(expect.objectContaining(expected)); + expect(failsTheTest).toHaveProperty('duration', expect.any(Number)); + + const skipedTest = jsonResult.testResults[0].assertionResults.find( + item => item.title == 'skiped test', + ); + expect(skipedTest).toHaveProperty('duration', null); }); it('outputs coverage report', () => { const result = runJest('json-reporter', ['--json']); let jsonResult: FormattedTestResults; - expect(result.stderr).toMatch(/1 failed, 2 passed/); + expect(result.stderr).toMatch(/1 failed, 1 skipped, 2 passed/); expect(result.exitCode).toBe(1); try { @@ -77,12 +85,12 @@ describe('JSON Reporter', () => { ); } - expect(jsonResult.numTotalTests).toBe(3); + expect(jsonResult.numTotalTests).toBe(4); expect(jsonResult.numTotalTestSuites).toBe(1); expect(jsonResult.numRuntimeErrorTestSuites).toBe(0); expect(jsonResult.numPassedTests).toBe(2); expect(jsonResult.numFailedTests).toBe(1); - expect(jsonResult.numPendingTests).toBe(0); + expect(jsonResult.numPendingTests).toBe(1); const noAncestors = jsonResult.testResults[0].assertionResults.find( item => item.title == 'no ancestors', diff --git a/e2e/json-reporter/__tests__/sum.test.js b/e2e/json-reporter/__tests__/sum.test.js index ced52866d371..5cfb981c5a18 100644 --- a/e2e/json-reporter/__tests__/sum.test.js +++ b/e2e/json-reporter/__tests__/sum.test.js @@ -22,4 +22,8 @@ describe('sum', () => { expect(sum(1, 2)).toEqual(4); }); }); + + it.skip('skiped test', () => { + expect(sum(1, 2)).toEqual(3); + }); }); diff --git a/packages/jest-test-result/src/formatTestResults.ts b/packages/jest-test-result/src/formatTestResults.ts index 6453487d6f50..67ea02cf1b0a 100644 --- a/packages/jest-test-result/src/formatTestResults.ts +++ b/packages/jest-test-result/src/formatTestResults.ts @@ -58,6 +58,7 @@ function formatTestAssertion( ): FormattedAssertionResult { const result: FormattedAssertionResult = { ancestorTitles: assertion.ancestorTitles, + duration: assertion.duration, failureMessages: null, fullName: assertion.fullName, location: assertion.location, diff --git a/packages/jest-test-result/src/types.ts b/packages/jest-test-result/src/types.ts index fafefa4981e1..40abef5f541a 100644 --- a/packages/jest-test-result/src/types.ts +++ b/packages/jest-test-result/src/types.ts @@ -50,7 +50,7 @@ export type AssertionResult = TestResult.AssertionResult; export type FormattedAssertionResult = Pick< AssertionResult, - 'ancestorTitles' | 'fullName' | 'location' | 'status' | 'title' + 'ancestorTitles' | 'fullName' | 'location' | 'status' | 'title' | 'duration' > & { failureMessages: AssertionResult['failureMessages'] | null; }; From 9f6df67103a0c3d6de6165560e3832972ca16a8b Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 1 Mar 2022 08:09:00 +0100 Subject: [PATCH 2/5] Apply suggestions from code review --- e2e/__tests__/jsonReporter.test.ts | 2 +- e2e/json-reporter/__tests__/sum.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/e2e/__tests__/jsonReporter.test.ts b/e2e/__tests__/jsonReporter.test.ts index e3532b404a4f..a0f957dc74d1 100644 --- a/e2e/__tests__/jsonReporter.test.ts +++ b/e2e/__tests__/jsonReporter.test.ts @@ -65,7 +65,7 @@ describe('JSON Reporter', () => { expect(failsTheTest).toHaveProperty('duration', expect.any(Number)); const skipedTest = jsonResult.testResults[0].assertionResults.find( - item => item.title == 'skiped test', + item => item.title == 'skipped test', ); expect(skipedTest).toHaveProperty('duration', null); }); diff --git a/e2e/json-reporter/__tests__/sum.test.js b/e2e/json-reporter/__tests__/sum.test.js index 5cfb981c5a18..ceb50b500322 100644 --- a/e2e/json-reporter/__tests__/sum.test.js +++ b/e2e/json-reporter/__tests__/sum.test.js @@ -23,7 +23,7 @@ describe('sum', () => { }); }); - it.skip('skiped test', () => { + it.skip('skipped test', () => { expect(sum(1, 2)).toEqual(3); }); }); From 0cf4993aac3187c5cd6683a55d0ec60f0f41a1ed Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 1 Mar 2022 08:24:49 +0100 Subject: [PATCH 3/5] snap --- e2e/__tests__/__snapshots__/coverageReport.test.ts.snap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/__tests__/__snapshots__/coverageReport.test.ts.snap b/e2e/__tests__/__snapshots__/coverageReport.test.ts.snap index 517ac85832d8..467485a89f9b 100644 --- a/e2e/__tests__/__snapshots__/coverageReport.test.ts.snap +++ b/e2e/__tests__/__snapshots__/coverageReport.test.ts.snap @@ -62,7 +62,7 @@ All files | 60 | 0 | 50 | 60 | exports[`json reporter printing with --coverage 1`] = ` "Test Suites: 1 failed, 1 total -Tests: 1 failed, 2 passed, 3 total +Tests: 1 failed, 1 skipped, 2 passed, 4 total Snapshots: 0 total Time: <> Ran all test suites." From 6b555181c301869491deb570ad4f7528b7f00176 Mon Sep 17 00:00:00 2001 From: Feng Yu Date: Tue, 1 Mar 2022 08:37:16 +0000 Subject: [PATCH 4/5] fix jasmine2 --- packages/jest-jasmine2/src/reporter.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/jest-jasmine2/src/reporter.ts b/packages/jest-jasmine2/src/reporter.ts index 3f1353dfa0dd..ad94e6b3ac09 100644 --- a/packages/jest-jasmine2/src/reporter.ts +++ b/packages/jest-jasmine2/src/reporter.ts @@ -128,10 +128,13 @@ export default class Jasmine2Reporter implements Reporter { specResult: SpecResult, ancestorTitles: Array, ): AssertionResult { - const start = this._startTimes.get(specResult.id); - const duration = start ? Date.now() - start : undefined; const status = specResult.status === 'disabled' ? 'pending' : specResult.status; + const start = this._startTimes.get(specResult.id); + const duration = + start && !['pending', 'skipped'].includes(status) + ? Date.now() - start + : null; const location = specResult.__callsite ? { column: specResult.__callsite.getColumnNumber(), From 610e0915ba269c89d16e77ea5cc831c4076c3d2f Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 1 Mar 2022 10:12:50 +0100 Subject: [PATCH 5/5] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea2b792e9b41..9153a8f8da52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,7 @@ - `[@jest/expect-utils]` [**BREAKING**] Fix false positives when looking for `undefined` prop ([#8923](https://github.com/facebook/jest/pull/8923)) - `[jest-haste-map]` Don't use partial results if file crawl errors ([#12420](https://github.com/facebook/jest/pull/12420)) - `[jest-jasmine2, jest-types]` [**BREAKING**] Move all `jasmine` specific types from `@jest/types` to its own package ([#12125](https://github.com/facebook/jest/pull/12125)) +- `[jest-jasmine2]` Do not set `duration` to `0` for skipped tests ([#12518](https://github.com/facebook/jest/pull/12518)) - `[jest-matcher-utils]` Pass maxWidth to `pretty-format` to avoid printing every element in arrays by default ([#12402](https://github.com/facebook/jest/pull/12402)) - `[jest-mock]` Fix function overloads for `spyOn` to allow more correct type inference in complex object ([#12442](https://github.com/facebook/jest/pull/12442)) - `[jest-reporters]` Notifications generated by the `--notify` flag are no longer persistent in GNOME Shell. ([#11733](https://github.com/facebook/jest/pull/11733))