Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jest-test-result): add duration property to JSON test output #12518

Merged
merged 5 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
3 changes: 2 additions & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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