From f442c153532cedfd4f43d3b57704cac9035f7a98 Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Sat, 21 Jan 2023 15:42:20 +0800 Subject: [PATCH 01/27] chore: numPassingAsserts type --- packages/expect/__typetests__/expect.test.ts | 1 + packages/expect/src/types.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/expect/__typetests__/expect.test.ts b/packages/expect/__typetests__/expect.test.ts index a34f1c28f4ee..b9e32400f54f 100644 --- a/packages/expect/__typetests__/expect.test.ts +++ b/packages/expect/__typetests__/expect.test.ts @@ -77,6 +77,7 @@ expectType( expectType(this.isExpectingAssertions); expectType(this.isExpectingAssertionsError); expectType(this.isNot); + expectType(this.numPassingAsserts); expectType(this.promise); expectType>(this.suppressedErrors); expectType(this.testPath); diff --git a/packages/expect/src/types.ts b/packages/expect/src/types.ts index 2f449b124d94..f3687b5597cb 100644 --- a/packages/expect/src/types.ts +++ b/packages/expect/src/types.ts @@ -65,6 +65,7 @@ export interface MatcherState { isExpectingAssertions: boolean; isExpectingAssertionsError?: Error; isNot?: boolean; + numPassingAsserts: number; promise?: string; suppressedErrors: Array; testPath?: string; From edefeabf849621ccee24ec3bc764ac69690d8d54 Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Sat, 21 Jan 2023 15:42:42 +0800 Subject: [PATCH 02/27] chore: numPassingAsserts default value --- packages/expect/src/extractExpectedAssertionsErrors.ts | 1 + packages/expect/src/jestMatchersObject.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/expect/src/extractExpectedAssertionsErrors.ts b/packages/expect/src/extractExpectedAssertionsErrors.ts index d56fb2417f26..9af24602d50a 100644 --- a/packages/expect/src/extractExpectedAssertionsErrors.ts +++ b/packages/expect/src/extractExpectedAssertionsErrors.ts @@ -20,6 +20,7 @@ const resetAssertionsLocalState = () => { assertionCalls: 0, expectedAssertionsNumber: null, isExpectingAssertions: false, + numPassingAsserts: 0, }); }; diff --git a/packages/expect/src/jestMatchersObject.ts b/packages/expect/src/jestMatchersObject.ts index 2cb600be6772..9272e4feed9a 100644 --- a/packages/expect/src/jestMatchersObject.ts +++ b/packages/expect/src/jestMatchersObject.ts @@ -29,6 +29,7 @@ if (!Object.prototype.hasOwnProperty.call(globalThis, JEST_MATCHERS_OBJECT)) { assertionCalls: 0, expectedAssertionsNumber: null, isExpectingAssertions: false, + numPassingAsserts: 0, suppressedErrors: [], // errors that are not thrown immediately. }; Object.defineProperty(globalThis, JEST_MATCHERS_OBJECT, { From 4173948013e5b200fc8144b950914ffb2cc69d8a Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Sat, 21 Jan 2023 15:42:54 +0800 Subject: [PATCH 03/27] chore: increment numPassingAsserts --- packages/expect/src/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/expect/src/index.ts b/packages/expect/src/index.ts index 88b913e8f181..7c526815d23b 100644 --- a/packages/expect/src/index.ts +++ b/packages/expect/src/index.ts @@ -337,6 +337,8 @@ const makeThrowingMatcher = ( } else { getState().suppressedErrors.push(error); } + } else { + getState().numPassingAsserts++; } }; From 590003b3f91293725c831bbe52e589ac1c89c112 Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Sat, 21 Jan 2023 15:43:00 +0800 Subject: [PATCH 04/27] chore: numPassingAsserts type --- packages/jest-types/__typetests__/expect.test.ts | 1 + packages/jest-types/src/Circus.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/jest-types/__typetests__/expect.test.ts b/packages/jest-types/__typetests__/expect.test.ts index 79b68993379b..e1178fc54b74 100644 --- a/packages/jest-types/__typetests__/expect.test.ts +++ b/packages/jest-types/__typetests__/expect.test.ts @@ -446,6 +446,7 @@ expectType( expectType(this.isExpectingAssertions); expectType(this.isExpectingAssertionsError); expectType(this.isNot); + expectType(this.numPassingAsserts); expectType(this.promise); expectType>(this.suppressedErrors); expectType(this.testPath); diff --git a/packages/jest-types/src/Circus.ts b/packages/jest-types/src/Circus.ts index 7ed5023d6caf..42e10b30d9d6 100644 --- a/packages/jest-types/src/Circus.ts +++ b/packages/jest-types/src/Circus.ts @@ -185,6 +185,7 @@ export type TestResult = { invocations: number; status: TestStatus; location?: {column: number; line: number} | null; + numPassingAsserts: number; retryReasons: Array; testPath: Array; }; From 559c28ffbb250c259cf558cc8bc5fa921ce6f0b6 Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Sat, 21 Jan 2023 15:43:12 +0800 Subject: [PATCH 05/27] chore: add numPassingAsserts to testResult --- .../src/legacy-code-todo-rewrite/jestAdapterInit.ts | 3 ++- packages/jest-circus/src/utils.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts index 4556c55185ae..4108fe133547 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts @@ -178,7 +178,7 @@ export const runAndTransformResultsToJestFormat = async ({ : ancestorTitles.join(' '), invocations: testResult.invocations, location: testResult.location, - numPassingAsserts: 0, + numPassingAsserts: testResult.numPassingAsserts, retryReasons: testResult.retryReasons, status, title: testResult.testPath[testResult.testPath.length - 1], @@ -238,6 +238,7 @@ const eventHandler = async (event: Circus.Event) => { break; } case 'test_done': { + event.test.numPassingAsserts = jestExpect.getState().numPassingAsserts; _addSuppressedErrors(event.test); _addExpectedAssertionErrors(event.test); break; diff --git a/packages/jest-circus/src/utils.ts b/packages/jest-circus/src/utils.ts index 7eeb2dfb2f3a..deda0f85ee0b 100644 --- a/packages/jest-circus/src/utils.ts +++ b/packages/jest-circus/src/utils.ts @@ -364,6 +364,7 @@ export const makeSingleTestResult = ( invocations: test.invocations, location, retryReasons: test.retryReasons.map(_getError).map(getErrorStack), + numPassingAsserts: test.numPassingAsserts, status, testPath: Array.from(testPath), }; @@ -484,7 +485,7 @@ export const parseSingleTestResult = ( : ancestorTitles.join(' '), invocations: testResult.invocations, location: testResult.location, - numPassingAsserts: 0, + numPassingAsserts: testResult.numPassingAsserts, retryReasons: Array.from(testResult.retryReasons), status, title: testResult.testPath[testResult.testPath.length - 1], From a8e05843704e6456100ea7145c4d3040289a3b0b Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Sat, 21 Jan 2023 20:17:23 +0800 Subject: [PATCH 06/27] chore: changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f85d3f887a47..a6fee03325e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - `[jest-runtime]` Add `jest.isEnvironmentTornDown` function ([#13698](https://github.com/facebook/jest/pull/13698)) - `[jest-test-result]` Added `skipped` and `focused` status to `FormattedTestResult` ([#13700](https://github.com/facebook/jest/pull/13700)) - `[jest-transform]` Support for asynchronous `createTransformer` ([#13762](https://github.com/facebook/jest/pull/13762)) +- `[expect, jest-circus, @jest/types]` Implement `numPassingAsserts` to track the number of passing asserts in a test ([#13795](https://github.com/facebook/jest/pull/13795)) ### Fixes From 9ecbb59c7bc3e2323d0da2717bb841d1e49d6460 Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Sat, 21 Jan 2023 21:06:26 +0800 Subject: [PATCH 07/27] chore: update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6fee03325e4..6ec7cbc63d1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ - `[jest-runtime]` Add `jest.isEnvironmentTornDown` function ([#13698](https://github.com/facebook/jest/pull/13698)) - `[jest-test-result]` Added `skipped` and `focused` status to `FormattedTestResult` ([#13700](https://github.com/facebook/jest/pull/13700)) - `[jest-transform]` Support for asynchronous `createTransformer` ([#13762](https://github.com/facebook/jest/pull/13762)) -- `[expect, jest-circus, @jest/types]` Implement `numPassingAsserts` to track the number of passing asserts in a test ([#13795](https://github.com/facebook/jest/pull/13795)) +- `[expect, jest-circus, @jest/types]` Implement `numPassingAsserts` of testResults to track the number of passing asserts in a test ([#13795](https://github.com/facebook/jest/pull/13795)) ### Fixes From d4f62c083ee25577d2f2730269b55d2af9b092b4 Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Mon, 23 Jan 2023 19:48:04 +0800 Subject: [PATCH 08/27] chore: fix ci --- packages/jest-circus/src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-circus/src/utils.ts b/packages/jest-circus/src/utils.ts index deda0f85ee0b..c57a2af36462 100644 --- a/packages/jest-circus/src/utils.ts +++ b/packages/jest-circus/src/utils.ts @@ -363,8 +363,8 @@ export const makeSingleTestResult = ( errorsDetailed, invocations: test.invocations, location, - retryReasons: test.retryReasons.map(_getError).map(getErrorStack), numPassingAsserts: test.numPassingAsserts, + retryReasons: test.retryReasons.map(_getError).map(getErrorStack), status, testPath: Array.from(testPath), }; From a423029758fd5e6e7059de2a31e365dced885972 Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Mon, 23 Jan 2023 19:48:26 +0800 Subject: [PATCH 09/27] chore: TestEntry numPassingAsserts --- packages/jest-types/src/Circus.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jest-types/src/Circus.ts b/packages/jest-types/src/Circus.ts index 42e10b30d9d6..c94c74a20e70 100644 --- a/packages/jest-types/src/Circus.ts +++ b/packages/jest-types/src/Circus.ts @@ -246,6 +246,7 @@ export type TestEntry = { mode: TestMode; concurrent: boolean; name: TestName; + numPassingAsserts: number; parent: DescribeBlock; startedAt?: number | null; duration?: number | null; From cdfd96f6ea4fb1b1a6f2f154740bdea5b520b60e Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Mon, 23 Jan 2023 22:26:02 +0800 Subject: [PATCH 10/27] fix: missing numPassingAsserts --- packages/jest-circus/src/utils.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jest-circus/src/utils.ts b/packages/jest-circus/src/utils.ts index c57a2af36462..d45bf21e91f5 100644 --- a/packages/jest-circus/src/utils.ts +++ b/packages/jest-circus/src/utils.ts @@ -78,6 +78,7 @@ export const makeTest = ( invocations: 0, mode, name: convertDescriptorToString(name), + numPassingAsserts: 0, parent, retryReasons: [], seenDone: false, From ba550ce56751c875daedb7b259f1487106cb8bbf Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Tue, 24 Jan 2023 22:44:14 +0800 Subject: [PATCH 11/27] chore: numPassingAsserts unit test --- .../src/__tests__/assertionCounts.test.ts | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/packages/expect/src/__tests__/assertionCounts.test.ts b/packages/expect/src/__tests__/assertionCounts.test.ts index 0e45883363b3..f3cdfeb1bf18 100644 --- a/packages/expect/src/__tests__/assertionCounts.test.ts +++ b/packages/expect/src/__tests__/assertionCounts.test.ts @@ -44,3 +44,35 @@ describe('.hasAssertions()', () => { it('hasAssertions not leaking to global state', () => {}); }); + +describe('numPassingAsserts', () => { + it('verify the default value of numPassingAsserts', () => { + const {numPassingAsserts} = jestExpect.getState(); + expect(numPassingAsserts).toBe(0); + }); + + it('verify the resetting of numPassingAsserts after a test', () => { + expect('a').toBe('a'); + expect('a').toBe('a'); + // reset state + jestExpect.extractExpectedAssertionsErrors(); + const {numPassingAsserts} = jestExpect.getState(); + expect(numPassingAsserts).toBe(0); + }); + + it('verify the correctness of numPassingAsserts count for passing test', () => { + expect('a').toBe('a'); + expect('a').toBe('a'); + const {numPassingAsserts} = jestExpect.getState(); + expect(numPassingAsserts).toBe(2); + }); + + it('verify the correctness of numPassingAsserts count for failing test', () => { + expect('a').toBe('a'); + try { + expect('a').toBe('b'); + } catch (error) {} + const {numPassingAsserts} = jestExpect.getState(); + expect(numPassingAsserts).toBe(1); + }); +}); From 82e450f0edddb0675df0a5b06ab9c705bb4e62d7 Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Tue, 24 Jan 2023 23:50:16 +0800 Subject: [PATCH 12/27] chore: AssertionCountsReporter e2e test --- .../customReporters.test.ts.snap | 7 +++++ e2e/__tests__/customReporters.test.ts | 16 ++++++++++ .../reporters/AssertionCountsReporter.js | 29 +++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 e2e/custom-reporters/reporters/AssertionCountsReporter.js diff --git a/e2e/__tests__/__snapshots__/customReporters.test.ts.snap b/e2e/__tests__/__snapshots__/customReporters.test.ts.snap index 7506630b07d2..5085ff453ab6 100644 --- a/e2e/__tests__/__snapshots__/customReporters.test.ts.snap +++ b/e2e/__tests__/__snapshots__/customReporters.test.ts.snap @@ -167,3 +167,10 @@ exports[`Custom Reporters Integration valid array format for adding reporters 1` } }" `; + +exports[`Custom Reporters Integration valid assertion counts for adding reporters 1`] = ` +"onTestCaseResult: adds fail, status: failed, numExpectations: 0 +onTestFileResult onTestCaseResult 0: adds fail, status: failed, numExpectations: 0 +onTestCaseResult: adds ok, status: passed, numExpectations: 3 +onTestFileResult onTestCaseResult 0: adds ok, status: passed, numExpectations: 3" +`; diff --git a/e2e/__tests__/customReporters.test.ts b/e2e/__tests__/customReporters.test.ts index f76647fd5758..5e468a88039d 100644 --- a/e2e/__tests__/customReporters.test.ts +++ b/e2e/__tests__/customReporters.test.ts @@ -179,4 +179,20 @@ describe('Custom Reporters Integration', () => { expect(stderr).toMatch(/ON_RUN_START_ERROR/); expect(exitCode).toBe(1); }); + + test('valid assertion counts for adding reporters', () => { + const {stdout} = runJest('custom-reporters', [ + '--config', + JSON.stringify({ + reporters: [ + 'default', + '/reporters/AssertionCountsReporter.js', + ], + }), + 'add.test.js', + 'addFail.test.js', + ]); + + expect(stdout).toMatchSnapshot(); + }); }); diff --git a/e2e/custom-reporters/reporters/AssertionCountsReporter.js b/e2e/custom-reporters/reporters/AssertionCountsReporter.js new file mode 100644 index 000000000000..c25d5f14be44 --- /dev/null +++ b/e2e/custom-reporters/reporters/AssertionCountsReporter.js @@ -0,0 +1,29 @@ +/*! + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +'use strict'; + +class AssertionCountsReporter { + onTestFileResult(test, testResult, aggregatedResult) { + testResult.testResults.forEach((testCaseResult, index) => { + console.log( + `onTestFileResult onTestCaseResult ${index}: ${testCaseResult.title}, ` + + `status: ${testCaseResult.status}, ` + + `numExpectations: ${testCaseResult.numPassingAsserts}`, + ); + }); + } + onTestCaseResult(test, testCaseResult) { + console.log( + `onTestCaseResult: ${testCaseResult.title}, ` + + `status: ${testCaseResult.status}, ` + + `numExpectations: ${testCaseResult.numPassingAsserts}`, + ); + } +} + +module.exports = AssertionCountsReporter; From 0418a302e3cad8526547c0a7cc6e3b5773a76247 Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Wed, 25 Jan 2023 00:18:49 +0800 Subject: [PATCH 13/27] chore: only test in jest-circus --- e2e/__tests__/customReporters.test.ts | 31 +++++++++++++++++---------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/e2e/__tests__/customReporters.test.ts b/e2e/__tests__/customReporters.test.ts index 5e468a88039d..b77478800000 100644 --- a/e2e/__tests__/customReporters.test.ts +++ b/e2e/__tests__/customReporters.test.ts @@ -181,17 +181,26 @@ describe('Custom Reporters Integration', () => { }); test('valid assertion counts for adding reporters', () => { - const {stdout} = runJest('custom-reporters', [ - '--config', - JSON.stringify({ - reporters: [ - 'default', - '/reporters/AssertionCountsReporter.js', - ], - }), - 'add.test.js', - 'addFail.test.js', - ]); + // just implement for jest-circus + const {stdout} = runJest( + 'custom-reporters', + [ + '--config', + JSON.stringify({ + reporters: [ + 'default', + '/reporters/AssertionCountsReporter.js', + ], + }), + 'add.test.js', + 'addFail.test.js', + ], + { + env: { + JEST_JASMINE: '0', + }, + }, + ); expect(stdout).toMatchSnapshot(); }); From 94386e10057fff7feaabd584a1a58619db7fba40 Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Wed, 25 Jan 2023 00:22:48 +0800 Subject: [PATCH 14/27] chore: update numPassingAsserts snapshot --- e2e/__tests__/__snapshots__/customReporters.test.ts.snap | 4 ++-- e2e/custom-reporters/reporters/AssertionCountsReporter.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/e2e/__tests__/__snapshots__/customReporters.test.ts.snap b/e2e/__tests__/__snapshots__/customReporters.test.ts.snap index 5085ff453ab6..45b5b09e1bc0 100644 --- a/e2e/__tests__/__snapshots__/customReporters.test.ts.snap +++ b/e2e/__tests__/__snapshots__/customReporters.test.ts.snap @@ -170,7 +170,7 @@ exports[`Custom Reporters Integration valid array format for adding reporters 1` exports[`Custom Reporters Integration valid assertion counts for adding reporters 1`] = ` "onTestCaseResult: adds fail, status: failed, numExpectations: 0 -onTestFileResult onTestCaseResult 0: adds fail, status: failed, numExpectations: 0 +onTestFileResult testCaseResult 0: adds fail, status: failed, numExpectations: 0 onTestCaseResult: adds ok, status: passed, numExpectations: 3 -onTestFileResult onTestCaseResult 0: adds ok, status: passed, numExpectations: 3" +onTestFileResult testCaseResult 0: adds ok, status: passed, numExpectations: 3" `; diff --git a/e2e/custom-reporters/reporters/AssertionCountsReporter.js b/e2e/custom-reporters/reporters/AssertionCountsReporter.js index c25d5f14be44..15988a300972 100644 --- a/e2e/custom-reporters/reporters/AssertionCountsReporter.js +++ b/e2e/custom-reporters/reporters/AssertionCountsReporter.js @@ -11,7 +11,7 @@ class AssertionCountsReporter { onTestFileResult(test, testResult, aggregatedResult) { testResult.testResults.forEach((testCaseResult, index) => { console.log( - `onTestFileResult onTestCaseResult ${index}: ${testCaseResult.title}, ` + + `onTestFileResult testCaseResult ${index}: ${testCaseResult.title}, ` + `status: ${testCaseResult.status}, ` + `numExpectations: ${testCaseResult.numPassingAsserts}`, ); From 249388cb6884a81d902fefe657c7ffc5ba18537b Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Wed, 25 Jan 2023 02:27:30 +0800 Subject: [PATCH 15/27] chore: onNotJestJasmine helper --- packages/test-utils/src/ConditionalTest.ts | 6 ++++++ packages/test-utils/src/index.ts | 1 + 2 files changed, 7 insertions(+) diff --git a/packages/test-utils/src/ConditionalTest.ts b/packages/test-utils/src/ConditionalTest.ts index badcb0c10ada..68321dc06d30 100644 --- a/packages/test-utils/src/ConditionalTest.ts +++ b/packages/test-utils/src/ConditionalTest.ts @@ -46,3 +46,9 @@ export function onNodeVersions( }); } } + +export function onNotJestJasmine(testBody: () => void): void { + if (!isJestJasmineRun()) { + testBody(); + } +} diff --git a/packages/test-utils/src/index.ts b/packages/test-utils/src/index.ts index a35e355d297b..ce1ff41fe3aa 100644 --- a/packages/test-utils/src/index.ts +++ b/packages/test-utils/src/index.ts @@ -12,6 +12,7 @@ export { skipSuiteOnJasmine, skipSuiteOnJestCircus, onNodeVersions, + onNotJestJasmine, } from './ConditionalTest'; export {makeGlobalConfig, makeProjectConfig} from './config'; From 8e13c11e94d7329951cf8c6732bbb341afd4c9f8 Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Wed, 25 Jan 2023 02:27:53 +0800 Subject: [PATCH 16/27] chore: remove hard code --- e2e/__tests__/customReporters.test.ts | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/e2e/__tests__/customReporters.test.ts b/e2e/__tests__/customReporters.test.ts index b77478800000..dfc7dc84f788 100644 --- a/e2e/__tests__/customReporters.test.ts +++ b/e2e/__tests__/customReporters.test.ts @@ -7,6 +7,7 @@ import {tmpdir} from 'os'; import * as path from 'path'; +import {onNotJestJasmine} from '@jest/test-utils'; import {cleanup, extractSummary, writeFiles} from '../Utils'; import runJest from '../runJest'; @@ -180,11 +181,9 @@ describe('Custom Reporters Integration', () => { expect(exitCode).toBe(1); }); - test('valid assertion counts for adding reporters', () => { - // just implement for jest-circus - const {stdout} = runJest( - 'custom-reporters', - [ + onNotJestJasmine(() => { + test('valid assertion counts for adding reporters', () => { + const {stdout} = runJest('custom-reporters', [ '--config', JSON.stringify({ reporters: [ @@ -194,14 +193,9 @@ describe('Custom Reporters Integration', () => { }), 'add.test.js', 'addFail.test.js', - ], - { - env: { - JEST_JASMINE: '0', - }, - }, - ); + ]); - expect(stdout).toMatchSnapshot(); + expect(stdout).toMatchSnapshot(); + }); }); }); From e5408ae3e35ffdfd7b6d177d0d8e49d053953206 Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Wed, 25 Jan 2023 02:37:24 +0800 Subject: [PATCH 17/27] chore: rename --- packages/test-utils/src/ConditionalTest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/test-utils/src/ConditionalTest.ts b/packages/test-utils/src/ConditionalTest.ts index 68321dc06d30..82b129960a29 100644 --- a/packages/test-utils/src/ConditionalTest.ts +++ b/packages/test-utils/src/ConditionalTest.ts @@ -47,7 +47,7 @@ export function onNodeVersions( } } -export function onNotJestJasmine(testBody: () => void): void { +export function skipTestOnJasmine(testBody: () => void): void { if (!isJestJasmineRun()) { testBody(); } From 2aa739fb71edc5438d889f3150652b0cc4a3b480 Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Wed, 25 Jan 2023 02:38:40 +0800 Subject: [PATCH 18/27] chore: update fn name --- e2e/__tests__/customReporters.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/e2e/__tests__/customReporters.test.ts b/e2e/__tests__/customReporters.test.ts index dfc7dc84f788..bc1ea1b002f8 100644 --- a/e2e/__tests__/customReporters.test.ts +++ b/e2e/__tests__/customReporters.test.ts @@ -7,7 +7,7 @@ import {tmpdir} from 'os'; import * as path from 'path'; -import {onNotJestJasmine} from '@jest/test-utils'; +import {skipTestOnJasmine} from '@jest/test-utils'; import {cleanup, extractSummary, writeFiles} from '../Utils'; import runJest from '../runJest'; @@ -181,7 +181,7 @@ describe('Custom Reporters Integration', () => { expect(exitCode).toBe(1); }); - onNotJestJasmine(() => { + skipTestOnJasmine(() => { test('valid assertion counts for adding reporters', () => { const {stdout} = runJest('custom-reporters', [ '--config', From 886a709c9b49e342d76aec4bda84f75d53c3181f Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Wed, 25 Jan 2023 02:42:21 +0800 Subject: [PATCH 19/27] chore: update export --- packages/test-utils/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/test-utils/src/index.ts b/packages/test-utils/src/index.ts index ce1ff41fe3aa..9936fddf98cf 100644 --- a/packages/test-utils/src/index.ts +++ b/packages/test-utils/src/index.ts @@ -12,7 +12,7 @@ export { skipSuiteOnJasmine, skipSuiteOnJestCircus, onNodeVersions, - onNotJestJasmine, + skipTestOnJasmine, } from './ConditionalTest'; export {makeGlobalConfig, makeProjectConfig} from './config'; From bae470a91fc236a62a8b7a09c3fb3dee1078d445 Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Wed, 25 Jan 2023 03:15:18 +0800 Subject: [PATCH 20/27] chore: update copyright --- e2e/custom-reporters/reporters/AssertionCountsReporter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/e2e/custom-reporters/reporters/AssertionCountsReporter.js b/e2e/custom-reporters/reporters/AssertionCountsReporter.js index 15988a300972..f5c5ac42d2c1 100644 --- a/e2e/custom-reporters/reporters/AssertionCountsReporter.js +++ b/e2e/custom-reporters/reporters/AssertionCountsReporter.js @@ -1,5 +1,5 @@ -/*! - * Copyright (c) Meta Platforms, Inc. and affiliates. +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. From 607bdb309b9114f8d88555d556ed6fc2507b2d0c Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Wed, 25 Jan 2023 03:15:44 +0800 Subject: [PATCH 21/27] chore: use inline snapshot --- e2e/__tests__/__snapshots__/customReporters.test.ts.snap | 7 ------- e2e/__tests__/customReporters.test.ts | 7 ++++++- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/e2e/__tests__/__snapshots__/customReporters.test.ts.snap b/e2e/__tests__/__snapshots__/customReporters.test.ts.snap index 45b5b09e1bc0..7506630b07d2 100644 --- a/e2e/__tests__/__snapshots__/customReporters.test.ts.snap +++ b/e2e/__tests__/__snapshots__/customReporters.test.ts.snap @@ -167,10 +167,3 @@ exports[`Custom Reporters Integration valid array format for adding reporters 1` } }" `; - -exports[`Custom Reporters Integration valid assertion counts for adding reporters 1`] = ` -"onTestCaseResult: adds fail, status: failed, numExpectations: 0 -onTestFileResult testCaseResult 0: adds fail, status: failed, numExpectations: 0 -onTestCaseResult: adds ok, status: passed, numExpectations: 3 -onTestFileResult testCaseResult 0: adds ok, status: passed, numExpectations: 3" -`; diff --git a/e2e/__tests__/customReporters.test.ts b/e2e/__tests__/customReporters.test.ts index bc1ea1b002f8..0a46ca76e4dd 100644 --- a/e2e/__tests__/customReporters.test.ts +++ b/e2e/__tests__/customReporters.test.ts @@ -195,7 +195,12 @@ describe('Custom Reporters Integration', () => { 'addFail.test.js', ]); - expect(stdout).toMatchSnapshot(); + expect(stdout).toMatchInlineSnapshot(` + "onTestCaseResult: adds fail, status: failed, numExpectations: 0 + onTestFileResult testCaseResult 0: adds fail, status: failed, numExpectations: 0 + onTestCaseResult: adds ok, status: passed, numExpectations: 3 + onTestFileResult testCaseResult 0: adds ok, status: passed, numExpectations: 3" + `); }); }); }); From d683aafde24f2cf6f6cb5a0a71069fd6a0a55c36 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 25 Jan 2023 09:59:50 +0100 Subject: [PATCH 22/27] move changelog entry --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0734acefc10b..5d484c61413a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[expect, jest-circus, @jest/types]` Implement `numPassingAsserts` of testResults to track the number of passing asserts in a test ([#13795](https://github.com/facebook/jest/pull/13795)) + ### Fixes - `[@jest/expect-utils]` `toMatchObject` diffs should include `Symbol` properties ([#13810](https://github.com/facebook/jest/pull/13810)) @@ -23,7 +25,6 @@ - `[jest-runtime]` Add `jest.isEnvironmentTornDown` function ([#13741](https://github.com/facebook/jest/pull/13741)) - `[jest-test-result]` Added `skipped` and `focused` status to `FormattedTestResult` ([#13700](https://github.com/facebook/jest/pull/13700)) - `[jest-transform]` Support for asynchronous `createTransformer` ([#13762](https://github.com/facebook/jest/pull/13762)) -- `[expect, jest-circus, @jest/types]` Implement `numPassingAsserts` of testResults to track the number of passing asserts in a test ([#13795](https://github.com/facebook/jest/pull/13795)) ### Fixes From e9201619a05998dc04535ad807f5b6ae46fe74ce Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Wed, 25 Jan 2023 20:51:11 +0800 Subject: [PATCH 23/27] chore: remove skipTestOnJasmine --- packages/test-utils/src/ConditionalTest.ts | 6 ------ packages/test-utils/src/index.ts | 1 - 2 files changed, 7 deletions(-) diff --git a/packages/test-utils/src/ConditionalTest.ts b/packages/test-utils/src/ConditionalTest.ts index 82b129960a29..badcb0c10ada 100644 --- a/packages/test-utils/src/ConditionalTest.ts +++ b/packages/test-utils/src/ConditionalTest.ts @@ -46,9 +46,3 @@ export function onNodeVersions( }); } } - -export function skipTestOnJasmine(testBody: () => void): void { - if (!isJestJasmineRun()) { - testBody(); - } -} diff --git a/packages/test-utils/src/index.ts b/packages/test-utils/src/index.ts index 9936fddf98cf..a35e355d297b 100644 --- a/packages/test-utils/src/index.ts +++ b/packages/test-utils/src/index.ts @@ -12,7 +12,6 @@ export { skipSuiteOnJasmine, skipSuiteOnJestCircus, onNodeVersions, - skipTestOnJasmine, } from './ConditionalTest'; export {makeGlobalConfig, makeProjectConfig} from './config'; From 7f2f2dfcdcad8ed40d0811c378acff3b28065fcc Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Wed, 25 Jan 2023 20:51:48 +0800 Subject: [PATCH 24/27] chore: customReportersOnCircus e2e test --- .../customReportersOnCircus.test.ts.snap | 8 ++++++ e2e/__tests__/customReporters.test.ts | 24 ---------------- e2e/__tests__/customReportersOnCircus.test.ts | 28 +++++++++++++++++++ 3 files changed, 36 insertions(+), 24 deletions(-) create mode 100644 e2e/__tests__/__snapshots__/customReportersOnCircus.test.ts.snap create mode 100644 e2e/__tests__/customReportersOnCircus.test.ts diff --git a/e2e/__tests__/__snapshots__/customReportersOnCircus.test.ts.snap b/e2e/__tests__/__snapshots__/customReportersOnCircus.test.ts.snap new file mode 100644 index 000000000000..6251bce52f87 --- /dev/null +++ b/e2e/__tests__/__snapshots__/customReportersOnCircus.test.ts.snap @@ -0,0 +1,8 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Custom Reporters Integration on jest-circus valid assertion counts for adding reporters 1`] = ` +"onTestCaseResult: adds fail, status: failed, numExpectations: 0 +onTestFileResult testCaseResult 0: adds fail, status: failed, numExpectations: 0 +onTestCaseResult: adds ok, status: passed, numExpectations: 3 +onTestFileResult testCaseResult 0: adds ok, status: passed, numExpectations: 3" +`; diff --git a/e2e/__tests__/customReporters.test.ts b/e2e/__tests__/customReporters.test.ts index 0a46ca76e4dd..f76647fd5758 100644 --- a/e2e/__tests__/customReporters.test.ts +++ b/e2e/__tests__/customReporters.test.ts @@ -7,7 +7,6 @@ import {tmpdir} from 'os'; import * as path from 'path'; -import {skipTestOnJasmine} from '@jest/test-utils'; import {cleanup, extractSummary, writeFiles} from '../Utils'; import runJest from '../runJest'; @@ -180,27 +179,4 @@ describe('Custom Reporters Integration', () => { expect(stderr).toMatch(/ON_RUN_START_ERROR/); expect(exitCode).toBe(1); }); - - skipTestOnJasmine(() => { - test('valid assertion counts for adding reporters', () => { - const {stdout} = runJest('custom-reporters', [ - '--config', - JSON.stringify({ - reporters: [ - 'default', - '/reporters/AssertionCountsReporter.js', - ], - }), - 'add.test.js', - 'addFail.test.js', - ]); - - expect(stdout).toMatchInlineSnapshot(` - "onTestCaseResult: adds fail, status: failed, numExpectations: 0 - onTestFileResult testCaseResult 0: adds fail, status: failed, numExpectations: 0 - onTestCaseResult: adds ok, status: passed, numExpectations: 3 - onTestFileResult testCaseResult 0: adds ok, status: passed, numExpectations: 3" - `); - }); - }); }); diff --git a/e2e/__tests__/customReportersOnCircus.test.ts b/e2e/__tests__/customReportersOnCircus.test.ts new file mode 100644 index 000000000000..463d7bc46847 --- /dev/null +++ b/e2e/__tests__/customReportersOnCircus.test.ts @@ -0,0 +1,28 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +import {skipSuiteOnJasmine} from '@jest/test-utils'; +import runJest from '../runJest'; + +skipSuiteOnJasmine(); + +describe('Custom Reporters Integration on jest-circus', () => { + test('valid assertion counts for adding reporters', () => { + const {stdout} = runJest('custom-reporters', [ + '--config', + JSON.stringify({ + reporters: [ + 'default', + '/reporters/AssertionCountsReporter.js', + ], + }), + 'add.test.js', + 'addFail.test.js', + ]); + + expect(stdout).toMatchSnapshot(); + }); +}); From 9d0c78a6e91cb1fbfc7ba743984e41e92261a68a Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Wed, 25 Jan 2023 22:05:14 +0800 Subject: [PATCH 25/27] fix: ci error --- .../customReportersOnCircus.test.ts.snap | 9 ++++++--- e2e/__tests__/customReportersOnCircus.test.ts | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/e2e/__tests__/__snapshots__/customReportersOnCircus.test.ts.snap b/e2e/__tests__/__snapshots__/customReportersOnCircus.test.ts.snap index 6251bce52f87..0867a8829991 100644 --- a/e2e/__tests__/__snapshots__/customReportersOnCircus.test.ts.snap +++ b/e2e/__tests__/__snapshots__/customReportersOnCircus.test.ts.snap @@ -1,8 +1,11 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Custom Reporters Integration on jest-circus valid assertion counts for adding reporters 1`] = ` +exports[`Custom Reporters Integration on jest-circus valid failing assertion counts for adding reporters 1`] = ` "onTestCaseResult: adds fail, status: failed, numExpectations: 0 -onTestFileResult testCaseResult 0: adds fail, status: failed, numExpectations: 0 -onTestCaseResult: adds ok, status: passed, numExpectations: 3 +onTestFileResult testCaseResult 0: adds fail, status: failed, numExpectations: 0" +`; + +exports[`Custom Reporters Integration on jest-circus valid passing assertion counts for adding reporters 1`] = ` +"onTestCaseResult: adds ok, status: passed, numExpectations: 3 onTestFileResult testCaseResult 0: adds ok, status: passed, numExpectations: 3" `; diff --git a/e2e/__tests__/customReportersOnCircus.test.ts b/e2e/__tests__/customReportersOnCircus.test.ts index 463d7bc46847..c30483085a14 100644 --- a/e2e/__tests__/customReportersOnCircus.test.ts +++ b/e2e/__tests__/customReportersOnCircus.test.ts @@ -10,7 +10,7 @@ import runJest from '../runJest'; skipSuiteOnJasmine(); describe('Custom Reporters Integration on jest-circus', () => { - test('valid assertion counts for adding reporters', () => { + test('valid passing assertion counts for adding reporters', () => { const {stdout} = runJest('custom-reporters', [ '--config', JSON.stringify({ @@ -20,6 +20,20 @@ describe('Custom Reporters Integration on jest-circus', () => { ], }), 'add.test.js', + ]); + + expect(stdout).toMatchSnapshot(); + }); + + test('valid failing assertion counts for adding reporters', () => { + const {stdout} = runJest('custom-reporters', [ + '--config', + JSON.stringify({ + reporters: [ + 'default', + '/reporters/AssertionCountsReporter.js', + ], + }), 'addFail.test.js', ]); From e5cac799c710d3eb3c9f433a1a4feffeabef122a Mon Sep 17 00:00:00 2001 From: Ming Ye Date: Thu, 26 Jan 2023 12:08:12 +0800 Subject: [PATCH 26/27] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ce442653ac9..d9d4934e2547 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## main ### Features + - `[jest-core]` Add newlines to JSON output ([#13817](https://github.com/facebook/jest/pull/13817)) - `[expect, jest-circus, @jest/types]` Implement `numPassingAsserts` of testResults to track the number of passing asserts in a test ([#13795](https://github.com/facebook/jest/pull/13795)) From 565fef83d20f09d3281a2a617a9227f7fd15e662 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 26 Jan 2023 15:29:20 +0100 Subject: [PATCH 27/27] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9d4934e2547..fc857b12fcd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,8 @@ ### Features -- `[jest-core]` Add newlines to JSON output ([#13817](https://github.com/facebook/jest/pull/13817)) - `[expect, jest-circus, @jest/types]` Implement `numPassingAsserts` of testResults to track the number of passing asserts in a test ([#13795](https://github.com/facebook/jest/pull/13795)) +- `[jest-core]` Add newlines to JSON output ([#13817](https://github.com/facebook/jest/pull/13817)) ### Fixes