Skip to content

Commit

Permalink
ISSUE-13112: Refactor onTestCaseStart hook calling to more stable a…
Browse files Browse the repository at this point in the history
…nd transparent approach

- clean previous approach
- add `test_started` event to Circus runner
- fix tests for `test_started` event
  • Loading branch information
DmitryMakhnev committed May 5, 2023
1 parent 4f1cae7 commit 817c9b8
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 41 deletions.
19 changes: 3 additions & 16 deletions e2e/__tests__/__snapshots__/customReportersOnCircus.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,11 @@ exports[`Custom Reporters Integration on jest-circus push test case results for
onTestFileResult testCaseResult 0: sample, status: todo, numExpectations: 0"
`;

exports[`Custom Reporters Integration on jest-circus push test case results for skip tests 1`] = `
"onTestCaseResult: sample, status: pending, numExpectations: 0
onTestFileResult testCaseResult 0: sample, status: pending, numExpectations: 0"
`;

exports[`Custom Reporters Integration on jest-circus push test case start 1`] = `
"onTestCaseStart: test 1, mode: undefined, ancestorTitles: Custom Reporters
onTestCaseResult: test 1, status: passed
onTestCaseStart: test 2, mode: undefined, ancestorTitles: Custom Reporters
onTestCaseResult: test 2, status: passed"
onTestCaseStart: test 2, mode: undefined, ancestorTitles: Custom Reporters"
`;

exports[`Custom Reporters Integration on jest-circus push test case start for todo tests 1`] = `
"onTestCaseStart: sample, mode: todo, ancestorTitles: Custom Reporters
onTestCaseResult: sample, status: todo"
`;
exports[`Custom Reporters Integration on jest-circus doesn't push test case start for skip tests 1`] = `""`;

exports[`Custom Reporters Integration on jest-circus push test case start for skip tests 1`] = `
"onTestCaseStart: sample, mode: skip, ancestorTitles: Custom Reporters
onTestCaseResult: sample, status: pending"
`;
exports[`Custom Reporters Integration on jest-circus doesn't push test case start for todo tests 1`] = `""`;
19 changes: 2 additions & 17 deletions e2e/__tests__/customReportersOnCircus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,6 @@ describe('Custom Reporters Integration on jest-circus', () => {
expect(stdout).toMatchSnapshot();
});

test('push test case results for skip tests', () => {
const {stdout} = runJest('custom-reporters', [
'--config',
JSON.stringify({
reporters: [
'default',
'<rootDir>/reporters/AssertionCountsReporter.js',
],
}),
'skip.test.js',
]);

expect(stdout).toMatchSnapshot();
});

test('push test case start', () => {
const {stdout} = runJest('custom-reporters', [
'--config',
Expand All @@ -82,7 +67,7 @@ describe('Custom Reporters Integration on jest-circus', () => {
expect(stdout).toMatchSnapshot();
});

test('push test case start for todo tests', () => {
test("doesn't push test case start for todo tests", () => {
const {stdout} = runJest('custom-reporters', [
'--config',
JSON.stringify({
Expand All @@ -94,7 +79,7 @@ describe('Custom Reporters Integration on jest-circus', () => {
expect(stdout).toMatchSnapshot();
});

test('push test case start for skip tests', () => {
test("doesn't push test case start for skip tests", () => {
const {stdout} = runJest('custom-reporters', [
'--config',
JSON.stringify({
Expand Down
2 changes: 2 additions & 0 deletions e2e/__tests__/testEnvironmentCircus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ it('calls testEnvironment handleTestEvent', () => {
"run_start",
"run_describe_start",
"test_start: test name here",
"test_started: test name here",
"hook_start",
"hook_success: test name here",
"hook_start",
Expand All @@ -31,6 +32,7 @@ it('calls testEnvironment handleTestEvent', () => {
"test_fn_success: test name here",
"test_done: test name here",
"test_start: second test name here",
"test_started: second test name here",
"hook_start",
"hook_success: second test name here",
"hook_start",
Expand Down
2 changes: 2 additions & 0 deletions e2e/__tests__/testEnvironmentCircusAsync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ it('calls asynchronous handleTestEvent in testEnvironment', () => {
"run_describe_start",
"run_describe_start",
"test_start: passing test",
"test_started: passing test",
"hook_start: beforeEach",
"hook_success: beforeEach",
"hook_start: beforeEach",
Expand All @@ -46,6 +47,7 @@ it('calls asynchronous handleTestEvent in testEnvironment', () => {
"hook_failure: afterEach",
"test_done: passing test",
"test_start: failing test",
"test_started: failing test",
"hook_start: beforeEach",
"hook_success: beforeEach",
"hook_start: beforeEach",
Expand Down
6 changes: 0 additions & 6 deletions e2e/custom-reporters/reporters/TestCaseStartReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ class TestCaseStartReporter {
`ancestorTitles: ${testCaseStartInfo.ancestorTitles.join('.')}`,
);
}
onTestCaseResult(test, testCaseResult) {
console.log(
`onTestCaseResult: ${testCaseResult.title}, ` +
`status: ${testCaseResult.status}`,
);
}
}

module.exports = TestCaseStartReporter;
1 change: 1 addition & 0 deletions packages/jest-circus/src/__mocks__/testEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const testEventHandler: Circus.EventHandler = (event, state) => {
break;
}
case 'test_start':
case 'test_started':
case 'test_retry':
case 'test_done': {
console.log(`${event.name}:`, event.test.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ hook_start: beforeAll
hook_success: beforeAll
run_describe_start: child describe
test_start: my test
test_started: my test
hook_start: beforeEach
> beforeEach
hook_success: beforeEach
Expand Down Expand Up @@ -132,24 +133,29 @@ run_start
run_describe_start: ROOT_DESCRIBE_BLOCK
run_describe_start: describe
test_start: one
test_started: one
test_fn_start: one
test_fn_success: one
test_done: one
test_start: two
test_started: two
test_fn_start: two
test_fn_success: two
test_done: two
run_describe_start: 2nd level describe
test_start: 2nd level test
test_started: 2nd level test
test_fn_start: 2nd level test
test_fn_success: 2nd level test
test_done: 2nd level test
run_describe_start: 3rd level describe
test_start: 3rd level test
test_started: 3rd level test
test_fn_start: 3rd level test
test_fn_success: 3rd level test
test_done: 3rd level test
test_start: 3rd level test#2
test_started: 3rd level test#2
test_fn_start: 3rd level test#2
test_fn_success: 3rd level test#2
test_done: 3rd level test#2
Expand All @@ -162,6 +168,7 @@ hook_success: afterAll
run_describe_finish: describe
run_describe_start: 2nd describe
test_start: 2nd describe test
test_started: 2nd describe test
test_fn_start: 2nd describe test
test_fn_success: 2nd describe test
test_done: 2nd describe test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ run_start
run_describe_start: ROOT_DESCRIBE_BLOCK
run_describe_start: describe
test_start: one
test_started: one
hook_start: beforeEach
hook_success: beforeEach
test_fn_start: one
Expand All @@ -19,6 +20,7 @@ hook_start: afterEach
hook_failure: afterEach
test_done: one
test_start: two
test_started: two
hook_start: beforeEach
hook_success: beforeEach
test_fn_start: two
Expand All @@ -41,6 +43,7 @@ run_start
run_describe_start: ROOT_DESCRIBE_BLOCK
run_describe_start: describer
test_start: One
test_started: One
test_fn_start: One
test_fn_success: One
test_done: One
Expand All @@ -62,6 +65,7 @@ run_start
run_describe_start: ROOT_DESCRIBE_BLOCK
run_describe_start: describe
test_start: one
test_started: one
hook_start: beforeEach
hook_success: beforeEach
test_fn_start: one
Expand All @@ -70,6 +74,7 @@ hook_start: afterEach
hook_success: afterEach
test_done: one
test_start: two
test_started: two
hook_start: beforeEach
hook_success: beforeEach
test_fn_start: two
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ hook_start: beforeAll
> beforeAll 1
hook_success: beforeAll
test_start: test 1
test_started: test 1
test_fn_start: test 1
> test 1
test_fn_success: test 1
Expand All @@ -26,11 +27,13 @@ hook_start: beforeAll
> beforeAll 2
hook_success: beforeAll
test_start: test 2
test_started: test 2
test_fn_start: test 2
> test 2
test_fn_success: test 2
test_done: test 2
test_start: test 3
test_started: test 3
test_fn_start: test 3
> test 3
test_fn_success: test 3
Expand Down Expand Up @@ -65,13 +68,15 @@ run_start
run_describe_start: ROOT_DESCRIBE_BLOCK
run_describe_start: describe
test_start: one
test_started: one
hook_start: beforeEach
> describe beforeEach
hook_success: beforeEach
test_fn_start: one
test_fn_success: one
test_done: one
test_start: two
test_started: two
hook_start: beforeEach
> describe beforeEach
hook_success: beforeEach
Expand All @@ -80,6 +85,7 @@ test_fn_success: two
test_done: two
run_describe_start: 2nd level describe
test_start: 2nd level test
test_started: 2nd level test
hook_start: beforeEach
> describe beforeEach
hook_success: beforeEach
Expand All @@ -91,6 +97,7 @@ test_fn_success: 2nd level test
test_done: 2nd level test
run_describe_start: 3rd level describe
test_start: 3rd level test
test_started: 3rd level test
hook_start: beforeEach
> describe beforeEach
hook_success: beforeEach
Expand All @@ -101,6 +108,7 @@ test_fn_start: 3rd level test
test_fn_success: 3rd level test
test_done: 3rd level test
test_start: 3rd level test#2
test_started: 3rd level test#2
hook_start: beforeEach
> describe beforeEach
hook_success: beforeEach
Expand All @@ -115,6 +123,7 @@ run_describe_finish: 2nd level describe
run_describe_finish: describe
run_describe_start: 2nd describe
test_start: 2nd describe test
test_started: 2nd describe test
hook_start: beforeEach
> 2nd describe beforeEach that throws
hook_failure: beforeEach
Expand All @@ -140,6 +149,7 @@ run_describe_start: ROOT_DESCRIBE_BLOCK
run_describe_start: describe 1
run_describe_start: 2nd level describe
test_start: test
test_started: test
hook_start: beforeEach
before each 1
hook_success: beforeEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ run_start
run_describe_start: ROOT_DESCRIBE_BLOCK
run_describe_start: describe
test_start: one
test_started: one
hook_start: beforeEach
hook_success: beforeEach
test_fn_start: one
Expand All @@ -19,6 +20,7 @@ hook_start: afterEach
hook_failure: afterEach
test_done: one
test_start: two
test_started: two
hook_start: beforeEach
hook_success: beforeEach
test_fn_start: two
Expand All @@ -41,6 +43,7 @@ run_start
run_describe_start: ROOT_DESCRIBE_BLOCK
run_describe_start: describer
test_start: One
test_started: One
test_fn_start: One
test_fn_success: One
test_done: One
Expand All @@ -62,6 +65,7 @@ run_start
run_describe_start: ROOT_DESCRIBE_BLOCK
run_describe_start: describe
test_start: one
test_started: one
hook_start: beforeEach
hook_success: beforeEach
test_fn_start: one
Expand All @@ -70,6 +74,7 @@ hook_start: afterEach
hook_success: afterEach
test_done: one
test_start: two
test_started: two
hook_start: beforeEach
hook_success: beforeEach
test_fn_start: two
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-circus/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ const _runTest = async (
return;
}

await dispatch({name: 'test_started', test});

const {afterEach, beforeEach} = getEachHooksForTest(test);

for (const hook of beforeEach) {
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-circus/src/testCaseReportHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ const testCaseReportHandler =
(testPath: string, sendMessageToJest: TestFileEvent) =>
(event: Circus.Event): void => {
switch (event.name) {
case 'test_start': {
case 'test_started': {
const testCaseStartInfo = createTestCaseStartInfo(event.test);
sendMessageToJest('test-case-start', [testPath, testCaseStartInfo]);
break;
}
case 'test_skip':
case 'test_todo':
case 'test_done': {
const testResult = makeSingleTestResult(event.test);
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-types/src/Circus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ export type AsyncEvent =
name: 'test_todo';
test: TestEntry;
}
| {
name: 'test_started';
test: TestEntry;
}
| {
// test failure is defined by presence of errors in `test.errors`,
// `test_done` indicates that the test and all its hooks were run,
Expand Down

0 comments on commit 817c9b8

Please sign in to comment.