Skip to content

Commit

Permalink
feat(jest-test-result): add duration property to JSON test output
Browse files Browse the repository at this point in the history
  • Loading branch information
F3n67u committed Mar 1, 2022
1 parent 7826a8f commit 1a66bb0
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 7 deletions.
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
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 == '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 {
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('skiped test', () => {
expect(sum(1, 2)).toEqual(3);
});
});
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

0 comments on commit 1a66bb0

Please sign in to comment.