Skip to content

Commit 41f842a

Browse files
authored
feat: pass startedAt to onTestCaseResult (#15145)
1 parent fb207dd commit 41f842a

File tree

5 files changed

+13
-7
lines changed

5 files changed

+13
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- `[jest-circus]` Add a `waitBeforeRetry` option to `jest.retryTimes` ([#14738](https://github.com/jestjs/jest/pull/14738))
88
- `[jest-circus]` Add a `retryImmediately` option to `jest.retryTimes` ([#14696](https://github.com/jestjs/jest/pull/14696))
99
- `[jest-circus, jest-jasmine2]` Allow `setupFilesAfterEnv` to export an async function ([#10962](https://github.com/jestjs/jest/issues/10962))
10+
- `[jest-circus, jest-test-result]` Add `startedAt` timestamp in `TestCaseResultObject` within `onTestCaseResult` ([#15145](https://github.com/jestjs/jest/pull/15145))
1011
- `[jest-config]` [**BREAKING**] Add `mts` and `cts` to default `moduleFileExtensions` config ([#14369](https://github.com/facebook/jest/pull/14369))
1112
- `[jest-config]` [**BREAKING**] Update `testMatch` and `testRegex` default option for supporting `mjs`, `cjs`, `mts`, and `cts` ([#14584](https://github.com/jestjs/jest/pull/14584))
1213
- `[jest-config]` Loads config file from provided path in `package.json` ([#14044](https://github.com/facebook/jest/pull/14044))

e2e/__tests__/__snapshots__/customReportersOnCircus.test.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`Custom Reporters Integration on jest-circus valid failing assertion counts for adding reporters 1`] = `
4-
"onTestCaseResult: adds fail, status: failed, numExpectations: 0
4+
"onTestCaseResult: adds fail, started: today, status: failed, numExpectations: 0
55
onTestFileResult testCaseResult 0: adds fail, status: failed, numExpectations: 0"
66
`;
77

88
exports[`Custom Reporters Integration on jest-circus valid passing assertion counts for adding reporters 1`] = `
9-
"onTestCaseResult: adds ok, status: passed, numExpectations: 3
9+
"onTestCaseResult: adds ok, started: today, status: passed, numExpectations: 3
1010
onTestFileResult testCaseResult 0: adds ok, status: passed, numExpectations: 3"
1111
`;
1212

1313
exports[`Custom Reporters Integration on jest-circus push test case results for todo tests 1`] = `
14-
"onTestCaseResult: sample, status: todo, numExpectations: 0
14+
"onTestCaseResult: sample, started: today, status: todo, numExpectations: 0
1515
onTestFileResult testCaseResult 0: sample, status: todo, numExpectations: 0"
1616
`;
1717

e2e/custom-reporters/reporters/AssertionCountsReporter.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ class AssertionCountsReporter {
1818
}
1919
}
2020
onTestCaseResult(test, testCaseResult) {
21+
const difference = new Date(
22+
Date.now() - testCaseResult.startedAt,
23+
).getDate();
2124
console.log(
2225
`onTestCaseResult: ${testCaseResult.title}, ` +
26+
`started: ${difference === 1 ? 'today' : 'invalid'}, ` +
2327
`status: ${testCaseResult.status}, ` +
2428
`numExpectations: ${testCaseResult.numPassingAsserts}`,
2529
);

packages/jest-circus/src/utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import dedent from 'dedent';
1111
import isGeneratorFn from 'is-generator-fn';
1212
import slash = require('slash');
1313
import StackUtils = require('stack-utils');
14-
import type {AssertionResult, Status} from '@jest/test-result';
14+
import type {Status, TestCaseResult} from '@jest/test-result';
1515
import type {Circus, Global} from '@jest/types';
1616
import {
1717
ErrorWithStack,
@@ -490,7 +490,7 @@ const resolveTestCaseStartInfo = (
490490

491491
export const parseSingleTestResult = (
492492
testResult: Circus.TestResult,
493-
): AssertionResult => {
493+
): TestCaseResult => {
494494
let status: Status;
495495
if (testResult.status === 'skip') {
496496
status = 'pending';
@@ -517,6 +517,7 @@ export const parseSingleTestResult = (
517517
location: testResult.location,
518518
numPassingAsserts: testResult.numPassingAsserts,
519519
retryReasons: [...testResult.retryReasons],
520+
startedAt: testResult.startedAt,
520521
status,
521522
title,
522523
};

packages/jest-test-result/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export type Suite = {
8585
tests: Array<AssertionResult>;
8686
};
8787

88-
export type TestCaseResult = AssertionResult;
88+
export type TestCaseResult = AssertionResult & {startedAt?: number | null};
8989

9090
export type TestResult = {
9191
console?: ConsoleBuffer;
@@ -209,7 +209,7 @@ export type TestEvents = {
209209
'test-file-success': [Test, TestResult];
210210
'test-file-failure': [Test, SerializableError];
211211
'test-case-start': [string, Circus.TestCaseStartInfo];
212-
'test-case-result': [string, AssertionResult];
212+
'test-case-result': [string, TestCaseResult];
213213
};
214214

215215
export type TestFileEvent<T extends keyof TestEvents = keyof TestEvents> = (

0 commit comments

Comments
 (0)