From 3fdcb0a76152e9907c5538bbeb9845e49aadaa19 Mon Sep 17 00:00:00 2001 From: Feng Yu Date: Tue, 1 Mar 2022 17:13:07 +0800 Subject: [PATCH] feat(jest-test-result): add duration property to JSON test output (#12518) --- CHANGELOG.md | 2 ++ docs/Configuration.md | 3 ++- .../__snapshots__/coverageReport.test.ts.snap | 2 +- e2e/__tests__/jsonReporter.test.ts | 18 +++++++++++++----- e2e/json-reporter/__tests__/sum.test.js | 4 ++++ packages/jest-jasmine2/src/reporter.ts | 7 +++++-- .../jest-test-result/src/formatTestResults.ts | 1 + packages/jest-test-result/src/types.ts | 2 +- 8 files changed, 29 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0f132ad803a..2eea5ff29d01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,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)) @@ -54,6 +55,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)) diff --git a/docs/Configuration.md b/docs/Configuration.md index 0e5b3b92f71c..5cb7a173e093 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__/__snapshots__/coverageReport.test.ts.snap b/e2e/__tests__/__snapshots__/coverageReport.test.ts.snap index a4fd058cf688..bf48e0520423 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." diff --git a/e2e/__tests__/jsonReporter.test.ts b/e2e/__tests__/jsonReporter.test.ts index 929c05ac6f0a..93132ac5e8a7 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 == 'skipped 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..ceb50b500322 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('skipped test', () => { + expect(sum(1, 2)).toEqual(3); + }); }); 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(), 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; };