Skip to content

Commit fe5e91c

Browse files
authored
feat(jest-core): support testResultsProcessor written in ESM (#12006)
1 parent 3d04f33 commit fe5e91c

File tree

6 files changed

+40
-2
lines changed

6 files changed

+40
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Features
44

5+
- `[jest-core]` Add support for `testResultsProcessor` written in ESM ([#12006](https://github.com/facebook/jest/pull/12006))
6+
57
### Fixes
68

79
- `[expect]` Allow again `expect.Matchers` generic with single value ([#11986](https://github.com/facebook/jest/pull/11986))

e2e/__tests__/testResultsProcessor.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
*/
77

88
import * as path from 'path';
9+
import {onNodeVersions} from '@jest/test-utils';
910
import {json as runWithJson} from '../runJest';
1011

11-
test('testNamePattern', () => {
12+
test('testResultsProcessor', () => {
1213
const processorPath = path.resolve(
1314
__dirname,
1415
'../test-results-processor/processor.js',
@@ -19,3 +20,18 @@ test('testNamePattern', () => {
1920
]);
2021
expect(json.processed).toBe(true);
2122
});
23+
24+
// The versions where vm.Module exists and commonjs with "exports" is not broken
25+
onNodeVersions('>=12.16.0', () => {
26+
test('testResultsProcessor written in ESM', () => {
27+
const processorPath = path.resolve(
28+
__dirname,
29+
'../test-results-processor/processor.mjs',
30+
);
31+
const {json} = runWithJson('test-results-processor', [
32+
'--json',
33+
`--testResultsProcessor=${processorPath}`,
34+
]);
35+
expect(json.processed).toBe(true);
36+
});
37+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export default function (results) {
9+
results.processed = true;
10+
return results;
11+
}

packages/jest-core/src/runJest.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {CustomConsole} from '@jest/console';
1313
import {
1414
AggregatedResult,
1515
Test,
16+
TestResultsProcessor,
1617
formatTestResults,
1718
makeEmptyAggregatedTestResult,
1819
} from '@jest/test-result';
@@ -96,7 +97,10 @@ const processResults = async (
9697
}
9798

9899
if (testResultsProcessor) {
99-
runResults = require(testResultsProcessor)(runResults);
100+
const processor = await requireOrImportModule<TestResultsProcessor>(
101+
testResultsProcessor,
102+
);
103+
runResults = processor(runResults);
100104
}
101105
if (isJSON) {
102106
if (outputFile) {

packages/jest-test-result/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export type {
2828
TestEvents,
2929
TestFileEvent,
3030
TestResult,
31+
TestResultsProcessor,
3132
TestCaseResult,
3233
V8CoverageResult,
3334
} from './types';

packages/jest-test-result/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ export type AggregatedResult = AggregatedResultWithoutCoverage & {
7878
coverageMap?: CoverageMap | null;
7979
};
8080

81+
export type TestResultsProcessor = (
82+
results: AggregatedResult,
83+
) => AggregatedResult;
84+
8185
export type Suite = {
8286
title: string;
8387
suites: Array<Suite>;

0 commit comments

Comments
 (0)