Skip to content

Commit 45daa05

Browse files
feat(jest-circus): add onTestCaseStart hook Reporter (#14174)
1 parent 6460335 commit 45daa05

File tree

21 files changed

+250
-24
lines changed

21 files changed

+250
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- `[jest-cli]` Include type definitions to generated config files ([#14078](https://github.com/facebook/jest/pull/14078))
66
- `[jest-snapshot]` Support arrays as property matchers ([#14025](https://github.com/facebook/jest/pull/14025))
7+
- `[jest-core, jest-circus, jest-reporter, jest-runner]` Added support for reporting about start individual test cases using jest-circus ([#14174](https://github.com/jestjs/jest/pull/14174))
78

89
### Fixes
910

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@ exports[`Custom Reporters Integration on jest-circus push test case results for
1414
"onTestCaseResult: sample, status: todo, numExpectations: 0
1515
onTestFileResult testCaseResult 0: sample, status: todo, numExpectations: 0"
1616
`;
17+
18+
exports[`Custom Reporters Integration on jest-circus push test case start 1`] = `
19+
"onTestCaseStart: test 1, mode: undefined, ancestorTitles: Custom Reporters
20+
onTestCaseStart: test 2, mode: undefined, ancestorTitles: Custom Reporters"
21+
`;
22+
23+
exports[`Custom Reporters Integration on jest-circus doesn't push test case start for skip tests 1`] = `""`;
24+
25+
exports[`Custom Reporters Integration on jest-circus doesn't push test case start for todo tests 1`] = `""`;

e2e/__tests__/customReportersOnCircus.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,40 @@ describe('Custom Reporters Integration on jest-circus', () => {
5454

5555
expect(stdout).toMatchSnapshot();
5656
});
57+
58+
test('push test case start', () => {
59+
const {stdout} = runJest('custom-reporters', [
60+
'--config',
61+
JSON.stringify({
62+
reporters: ['default', '<rootDir>/reporters/TestCaseStartReporter.js'],
63+
}),
64+
'just2Tests.test.js',
65+
]);
66+
67+
expect(stdout).toMatchSnapshot();
68+
});
69+
70+
test("doesn't push test case start for todo tests", () => {
71+
const {stdout} = runJest('custom-reporters', [
72+
'--config',
73+
JSON.stringify({
74+
reporters: ['default', '<rootDir>/reporters/TestCaseStartReporter.js'],
75+
}),
76+
'todo.test.js',
77+
]);
78+
79+
expect(stdout).toMatchSnapshot();
80+
});
81+
82+
test("doesn't push test case start for skip tests", () => {
83+
const {stdout} = runJest('custom-reporters', [
84+
'--config',
85+
JSON.stringify({
86+
reporters: ['default', '<rootDir>/reporters/TestCaseStartReporter.js'],
87+
}),
88+
'skip.test.js',
89+
]);
90+
91+
expect(stdout).toMatchSnapshot();
92+
});
5793
});

e2e/__tests__/testEnvironmentCircus.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ it('calls testEnvironment handleTestEvent', () => {
2323
"run_start",
2424
"run_describe_start",
2525
"test_start: test name here",
26+
"test_started: test name here",
2627
"hook_start",
2728
"hook_success: test name here",
2829
"hook_start",
@@ -31,6 +32,7 @@ it('calls testEnvironment handleTestEvent', () => {
3132
"test_fn_success: test name here",
3233
"test_done: test name here",
3334
"test_start: second test name here",
35+
"test_started: second test name here",
3436
"hook_start",
3537
"hook_success: second test name here",
3638
"hook_start",

e2e/__tests__/testEnvironmentCircusAsync.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ it('calls asynchronous handleTestEvent in testEnvironment', () => {
3636
"run_describe_start",
3737
"run_describe_start",
3838
"test_start: passing test",
39+
"test_started: passing test",
3940
"hook_start: beforeEach",
4041
"hook_success: beforeEach",
4142
"hook_start: beforeEach",
@@ -46,6 +47,7 @@ it('calls asynchronous handleTestEvent in testEnvironment', () => {
4647
"hook_failure: afterEach",
4748
"test_done: passing test",
4849
"test_start: failing test",
50+
"test_started: failing test",
4951
"hook_start: beforeEach",
5052
"hook_success: beforeEach",
5153
"hook_start: beforeEach",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
9+
'use strict';
10+
11+
describe('Custom Reporters', () => {
12+
it('test 1', () => {
13+
expect(true).toBeTruthy();
14+
});
15+
16+
it('test 2', () => {
17+
expect(true).toBeTruthy();
18+
});
19+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
9+
'use strict';
10+
11+
describe('Custom Reporters', () => {
12+
it.skip('sample', () => {});
13+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
/**
11+
* @class
12+
* @implements {import('@jest/reporters').Reporter}
13+
*/
14+
class TestCaseStartReporter {
15+
onTestCaseStart(test, testCaseStartInfo) {
16+
const mode =
17+
testCaseStartInfo.mode != null ? testCaseStartInfo.mode : 'undefined';
18+
console.log(
19+
`onTestCaseStart: ${testCaseStartInfo.title}, ` +
20+
`mode: ${mode}, ` +
21+
`ancestorTitles: ${testCaseStartInfo.ancestorTitles.join('.')}`,
22+
);
23+
}
24+
}
25+
26+
module.exports = TestCaseStartReporter;

packages/jest-circus/src/__mocks__/testEventHandler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const testEventHandler: Circus.EventHandler = (event, state) => {
2020
break;
2121
}
2222
case 'test_start':
23+
case 'test_started':
2324
case 'test_retry':
2425
case 'test_done': {
2526
console.log(`${event.name}:`, event.test.name);

packages/jest-circus/src/__tests__/__snapshots__/afterAll.test.ts.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ hook_start: beforeAll
5454
hook_success: beforeAll
5555
run_describe_start: child describe
5656
test_start: my test
57+
test_started: my test
5758
hook_start: beforeEach
5859
> beforeEach
5960
hook_success: beforeEach
@@ -132,24 +133,29 @@ run_start
132133
run_describe_start: ROOT_DESCRIBE_BLOCK
133134
run_describe_start: describe
134135
test_start: one
136+
test_started: one
135137
test_fn_start: one
136138
test_fn_success: one
137139
test_done: one
138140
test_start: two
141+
test_started: two
139142
test_fn_start: two
140143
test_fn_success: two
141144
test_done: two
142145
run_describe_start: 2nd level describe
143146
test_start: 2nd level test
147+
test_started: 2nd level test
144148
test_fn_start: 2nd level test
145149
test_fn_success: 2nd level test
146150
test_done: 2nd level test
147151
run_describe_start: 3rd level describe
148152
test_start: 3rd level test
153+
test_started: 3rd level test
149154
test_fn_start: 3rd level test
150155
test_fn_success: 3rd level test
151156
test_done: 3rd level test
152157
test_start: 3rd level test#2
158+
test_started: 3rd level test#2
153159
test_fn_start: 3rd level test#2
154160
test_fn_success: 3rd level test#2
155161
test_done: 3rd level test#2
@@ -162,6 +168,7 @@ hook_success: afterAll
162168
run_describe_finish: describe
163169
run_describe_start: 2nd describe
164170
test_start: 2nd describe test
171+
test_started: 2nd describe test
165172
test_fn_start: 2nd describe test
166173
test_fn_success: 2nd describe test
167174
test_done: 2nd describe test

0 commit comments

Comments
 (0)