Skip to content

Commit

Permalink
feat(jest-test-result): add duration property to JSON test output (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
F3n67u committed Mar 1, 2022
1 parent 070307e commit 3fdcb0a
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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))

Expand All @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion docs/Configuration.md
Expand Up @@ -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
},
...
],
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/coverageReport.test.ts.snap
Expand Up @@ -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: <<REPLACED>>
Ran all test suites."
Expand Down
18 changes: 13 additions & 5 deletions e2e/__tests__/jsonReporter.test.ts
Expand Up @@ -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<string>};
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 {
Expand All @@ -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',
Expand Down
4 changes: 4 additions & 0 deletions e2e/json-reporter/__tests__/sum.test.js
Expand Up @@ -22,4 +22,8 @@ describe('sum', () => {
expect(sum(1, 2)).toEqual(4);
});
});

it.skip('skipped test', () => {
expect(sum(1, 2)).toEqual(3);
});
});
7 changes: 5 additions & 2 deletions packages/jest-jasmine2/src/reporter.ts
Expand Up @@ -128,10 +128,13 @@ export default class Jasmine2Reporter implements Reporter {
specResult: SpecResult,
ancestorTitles: Array<string>,
): 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(),
Expand Down
1 change: 1 addition & 0 deletions packages/jest-test-result/src/formatTestResults.ts
Expand Up @@ -58,6 +58,7 @@ function formatTestAssertion(
): FormattedAssertionResult {
const result: FormattedAssertionResult = {
ancestorTitles: assertion.ancestorTitles,
duration: assertion.duration,
failureMessages: null,
fullName: assertion.fullName,
location: assertion.location,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-test-result/src/types.ts
Expand Up @@ -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;
};
Expand Down

0 comments on commit 3fdcb0a

Please sign in to comment.