From fe5e91c1b86323ab02b3de9d322b77e65a64e75a Mon Sep 17 00:00:00 2001 From: "C. T. Lin" Date: Thu, 28 Oct 2021 15:38:14 +0800 Subject: [PATCH] feat(jest-core): support `testResultsProcessor` written in ESM (#12006) --- CHANGELOG.md | 2 ++ e2e/__tests__/testResultsProcessor.test.ts | 18 +++++++++++++++++- e2e/test-results-processor/processor.mjs | 11 +++++++++++ packages/jest-core/src/runJest.ts | 6 +++++- packages/jest-test-result/src/index.ts | 1 + packages/jest-test-result/src/types.ts | 4 ++++ 6 files changed, 40 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..b407687fa89d 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,10 @@ const processResults = async ( } if (testResultsProcessor) { - runResults = require(testResultsProcessor)(runResults); + const processor = await requireOrImportModule( + testResultsProcessor, + ); + runResults = processor(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;