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;