From 5ea61a7c02519bd776765e556248388f8e808b47 Mon Sep 17 00:00:00 2001 From: chentsulin Date: Thu, 28 Oct 2021 13:20:51 +0800 Subject: [PATCH 1/3] feat(jest-core): support testResultsProcessor written in ESM --- CHANGELOG.md | 2 ++ e2e/__tests__/testResultsProcessor.test.ts | 18 +++++++++++++++++- e2e/test-results-processor/processor.mjs | 11 +++++++++++ packages/jest-core/src/runJest.ts | 5 ++++- packages/jest-test-result/src/index.ts | 1 + packages/jest-test-result/src/types.ts | 4 ++++ 6 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 e2e/test-results-processor/processor.mjs diff --git a/CHANGELOG.md b/CHANGELOG.md index d7cd6accaa41..01481ec5f85f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[jest-core]` Add support for `testResultsProcessor` written in ESM ([#12006](https://github.com/facebook/jest/pull/12006)) + ### Fixes - `[expect]` Allow again `expect.Matchers` generic with single value ([#11986](https://github.com/facebook/jest/pull/11986)) diff --git a/e2e/__tests__/testResultsProcessor.test.ts b/e2e/__tests__/testResultsProcessor.test.ts index cd7d89e6f592..c3724b6fe92e 100644 --- a/e2e/__tests__/testResultsProcessor.test.ts +++ b/e2e/__tests__/testResultsProcessor.test.ts @@ -6,9 +6,10 @@ */ import * as path from 'path'; +import {onNodeVersions} from '@jest/test-utils'; import {json as runWithJson} from '../runJest'; -test('testNamePattern', () => { +test('testResultsProcessor', () => { const processorPath = path.resolve( __dirname, '../test-results-processor/processor.js', @@ -19,3 +20,18 @@ test('testNamePattern', () => { ]); expect(json.processed).toBe(true); }); + +// The versions where vm.Module exists and commonjs with "exports" is not broken +onNodeVersions('>=12.16.0', () => { + test('testResultsProcessor written in ESM', () => { + const processorPath = path.resolve( + __dirname, + '../test-results-processor/processor.mjs', + ); + const {json} = runWithJson('test-results-processor', [ + '--json', + `--testResultsProcessor=${processorPath}`, + ]); + expect(json.processed).toBe(true); + }); +}); diff --git a/e2e/test-results-processor/processor.mjs b/e2e/test-results-processor/processor.mjs new file mode 100644 index 000000000000..be9679b3353d --- /dev/null +++ b/e2e/test-results-processor/processor.mjs @@ -0,0 +1,11 @@ +/** + * 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. + */ + +export default function (results) { + results.processed = true; + return results; +} diff --git a/packages/jest-core/src/runJest.ts b/packages/jest-core/src/runJest.ts index 636c28993385..a7c8f2fb238a 100644 --- a/packages/jest-core/src/runJest.ts +++ b/packages/jest-core/src/runJest.ts @@ -13,6 +13,7 @@ import {CustomConsole} from '@jest/console'; import { AggregatedResult, Test, + TestResultsProcessor, formatTestResults, makeEmptyAggregatedTestResult, } from '@jest/test-result'; @@ -96,7 +97,9 @@ const processResults = async ( } if (testResultsProcessor) { - runResults = require(testResultsProcessor)(runResults); + runResults = ( + await requireOrImportModule(testResultsProcessor) + )(runResults); } if (isJSON) { if (outputFile) { diff --git a/packages/jest-test-result/src/index.ts b/packages/jest-test-result/src/index.ts index 97c2def09908..26edbc397fa6 100644 --- a/packages/jest-test-result/src/index.ts +++ b/packages/jest-test-result/src/index.ts @@ -28,6 +28,7 @@ export type { TestEvents, TestFileEvent, TestResult, + TestResultsProcessor, TestCaseResult, V8CoverageResult, } from './types'; diff --git a/packages/jest-test-result/src/types.ts b/packages/jest-test-result/src/types.ts index 94c23cf72cd7..d911ac818fbf 100644 --- a/packages/jest-test-result/src/types.ts +++ b/packages/jest-test-result/src/types.ts @@ -78,6 +78,10 @@ export type AggregatedResult = AggregatedResultWithoutCoverage & { coverageMap?: CoverageMap | null; }; +export type TestResultsProcessor = ( + results: AggregatedResult, +) => AggregatedResult; + export type Suite = { title: string; suites: Array; From a9078316365dd4f8d965750cd7b7ec221747f2e2 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 28 Oct 2021 09:02:36 +0200 Subject: [PATCH 2/3] split up --- packages/jest-core/src/runJest.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/jest-core/src/runJest.ts b/packages/jest-core/src/runJest.ts index a7c8f2fb238a..93ca85b826cd 100644 --- a/packages/jest-core/src/runJest.ts +++ b/packages/jest-core/src/runJest.ts @@ -97,9 +97,10 @@ const processResults = async ( } if (testResultsProcessor) { - runResults = ( - await requireOrImportModule(testResultsProcessor) - )(runResults); + const processor = await requireOrImportModule( + testResultsProcessor, + ); + processor(runResults); } if (isJSON) { if (outputFile) { From 59b41366f0ecfb9b34341788f6dbac745c1cd528 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 28 Oct 2021 09:06:27 +0200 Subject: [PATCH 3/3] assign --- packages/jest-core/src/runJest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-core/src/runJest.ts b/packages/jest-core/src/runJest.ts index 93ca85b826cd..b407687fa89d 100644 --- a/packages/jest-core/src/runJest.ts +++ b/packages/jest-core/src/runJest.ts @@ -100,7 +100,7 @@ const processResults = async ( const processor = await requireOrImportModule( testResultsProcessor, ); - processor(runResults); + runResults = processor(runResults); } if (isJSON) { if (outputFile) {