From 817c9b8bcc071a41821cd84eb45c2eb3b1d0884b Mon Sep 17 00:00:00 2001 From: Dmitry Makhnev Date: Fri, 5 May 2023 14:20:53 +0200 Subject: [PATCH] ISSUE-13112: Refactor `onTestCaseStart` hook calling to more stable and transparent approach - clean previous approach - add `test_started` event to Circus runner - fix tests for `test_started` event --- .../customReportersOnCircus.test.ts.snap | 19 +++---------------- e2e/__tests__/customReportersOnCircus.test.ts | 19 ++----------------- e2e/__tests__/testEnvironmentCircus.test.ts | 2 ++ .../testEnvironmentCircusAsync.test.ts | 2 ++ .../reporters/TestCaseStartReporter.js | 6 ------ .../src/__mocks__/testEventHandler.ts | 1 + .../__snapshots__/afterAll.test.ts.snap | 7 +++++++ .../__snapshots__/baseTest.test.ts.snap | 5 +++++ .../__snapshots__/hooks.test.ts.snap | 10 ++++++++++ .../__snapshots__/randomizeTest.test.ts.snap | 5 +++++ packages/jest-circus/src/run.ts | 2 ++ .../jest-circus/src/testCaseReportHandler.ts | 3 +-- packages/jest-types/src/Circus.ts | 4 ++++ 13 files changed, 44 insertions(+), 41 deletions(-) diff --git a/e2e/__tests__/__snapshots__/customReportersOnCircus.test.ts.snap b/e2e/__tests__/__snapshots__/customReportersOnCircus.test.ts.snap index 6ac4b3b552fe..e52b96c3dbb6 100644 --- a/e2e/__tests__/__snapshots__/customReportersOnCircus.test.ts.snap +++ b/e2e/__tests__/__snapshots__/customReportersOnCircus.test.ts.snap @@ -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`] = `""`; diff --git a/e2e/__tests__/customReportersOnCircus.test.ts b/e2e/__tests__/customReportersOnCircus.test.ts index bdf1d5d18354..2dbe0b248cae 100644 --- a/e2e/__tests__/customReportersOnCircus.test.ts +++ b/e2e/__tests__/customReportersOnCircus.test.ts @@ -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', - '/reporters/AssertionCountsReporter.js', - ], - }), - 'skip.test.js', - ]); - - expect(stdout).toMatchSnapshot(); - }); - test('push test case start', () => { const {stdout} = runJest('custom-reporters', [ '--config', @@ -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({ @@ -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({ diff --git a/e2e/__tests__/testEnvironmentCircus.test.ts b/e2e/__tests__/testEnvironmentCircus.test.ts index aec50e7c2b10..1cfc2a8c8d49 100644 --- a/e2e/__tests__/testEnvironmentCircus.test.ts +++ b/e2e/__tests__/testEnvironmentCircus.test.ts @@ -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", @@ -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", diff --git a/e2e/__tests__/testEnvironmentCircusAsync.test.ts b/e2e/__tests__/testEnvironmentCircusAsync.test.ts index 56692be0c21f..90a4311e95ee 100644 --- a/e2e/__tests__/testEnvironmentCircusAsync.test.ts +++ b/e2e/__tests__/testEnvironmentCircusAsync.test.ts @@ -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", @@ -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", diff --git a/e2e/custom-reporters/reporters/TestCaseStartReporter.js b/e2e/custom-reporters/reporters/TestCaseStartReporter.js index 8afd1e7f6634..096957ddefae 100644 --- a/e2e/custom-reporters/reporters/TestCaseStartReporter.js +++ b/e2e/custom-reporters/reporters/TestCaseStartReporter.js @@ -21,12 +21,6 @@ class TestCaseStartReporter { `ancestorTitles: ${testCaseStartInfo.ancestorTitles.join('.')}`, ); } - onTestCaseResult(test, testCaseResult) { - console.log( - `onTestCaseResult: ${testCaseResult.title}, ` + - `status: ${testCaseResult.status}`, - ); - } } module.exports = TestCaseStartReporter; diff --git a/packages/jest-circus/src/__mocks__/testEventHandler.ts b/packages/jest-circus/src/__mocks__/testEventHandler.ts index cfdaef275c79..b3c20200ac64 100644 --- a/packages/jest-circus/src/__mocks__/testEventHandler.ts +++ b/packages/jest-circus/src/__mocks__/testEventHandler.ts @@ -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); diff --git a/packages/jest-circus/src/__tests__/__snapshots__/afterAll.test.ts.snap b/packages/jest-circus/src/__tests__/__snapshots__/afterAll.test.ts.snap index 8ae3e7150ae1..0f4a157cbb31 100644 --- a/packages/jest-circus/src/__tests__/__snapshots__/afterAll.test.ts.snap +++ b/packages/jest-circus/src/__tests__/__snapshots__/afterAll.test.ts.snap @@ -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 @@ -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 @@ -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 diff --git a/packages/jest-circus/src/__tests__/__snapshots__/baseTest.test.ts.snap b/packages/jest-circus/src/__tests__/__snapshots__/baseTest.test.ts.snap index f44411f9ac58..531ff0e13f1f 100644 --- a/packages/jest-circus/src/__tests__/__snapshots__/baseTest.test.ts.snap +++ b/packages/jest-circus/src/__tests__/__snapshots__/baseTest.test.ts.snap @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/packages/jest-circus/src/__tests__/__snapshots__/hooks.test.ts.snap b/packages/jest-circus/src/__tests__/__snapshots__/hooks.test.ts.snap index 465511f31286..277e3c17e2f7 100644 --- a/packages/jest-circus/src/__tests__/__snapshots__/hooks.test.ts.snap +++ b/packages/jest-circus/src/__tests__/__snapshots__/hooks.test.ts.snap @@ -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 @@ -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 @@ -65,6 +68,7 @@ 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 @@ -72,6 +76,7 @@ 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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/packages/jest-circus/src/__tests__/__snapshots__/randomizeTest.test.ts.snap b/packages/jest-circus/src/__tests__/__snapshots__/randomizeTest.test.ts.snap index f44411f9ac58..531ff0e13f1f 100644 --- a/packages/jest-circus/src/__tests__/__snapshots__/randomizeTest.test.ts.snap +++ b/packages/jest-circus/src/__tests__/__snapshots__/randomizeTest.test.ts.snap @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/packages/jest-circus/src/run.ts b/packages/jest-circus/src/run.ts index 7ccaf2d2e187..ec07b7d13dfd 100644 --- a/packages/jest-circus/src/run.ts +++ b/packages/jest-circus/src/run.ts @@ -166,6 +166,8 @@ const _runTest = async ( return; } + await dispatch({name: 'test_started', test}); + const {afterEach, beforeEach} = getEachHooksForTest(test); for (const hook of beforeEach) { diff --git a/packages/jest-circus/src/testCaseReportHandler.ts b/packages/jest-circus/src/testCaseReportHandler.ts index fd41c127a861..c1d987225518 100644 --- a/packages/jest-circus/src/testCaseReportHandler.ts +++ b/packages/jest-circus/src/testCaseReportHandler.ts @@ -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); diff --git a/packages/jest-types/src/Circus.ts b/packages/jest-types/src/Circus.ts index 9a9aa5089874..2b27c314f541 100644 --- a/packages/jest-types/src/Circus.ts +++ b/packages/jest-types/src/Circus.ts @@ -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,