Skip to content

Commit

Permalink
feat(jest-core): support testResultsProcessor written in ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
chentsulin committed Oct 28, 2021
1 parent 3d04f33 commit b9a3048
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 14 additions & 0 deletions e2e/__tests__/testResultsProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

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

test('testNamePattern', () => {
Expand All @@ -19,3 +20,16 @@ test('testNamePattern', () => {
]);
expect(json.processed).toBe(true);
});

// The versions where vm.Module exists and commonjs with "exports" is not broken
onNodeVersions('>=12.16.0', () => {
const processorPath = path.resolve(
__dirname,
'../test-results-processor/processor.mjs',
);
const {json} = runWithJson('test-results-processor', [
'--json',
`--testResultsProcessor=${processorPath}`,
]);
expect(json.processed).toBe(true);
});
11 changes: 11 additions & 0 deletions e2e/test-results-processor/processor.mjs
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 5 additions & 1 deletion packages/jest-core/src/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {CustomConsole} from '@jest/console';
import {
AggregatedResult,
Test,
TestResultsProcessor,
formatTestResults,
makeEmptyAggregatedTestResult,
} from '@jest/test-result';
Expand Down Expand Up @@ -96,7 +97,10 @@ const processResults = async (
}

if (testResultsProcessor) {
runResults = require(testResultsProcessor)(runResults);
const resultsProcessor: TestResultsProcessor = await requireOrImportModule(
testResultsProcessor,
);
runResults = resultsProcessor(runResults);
}
if (isJSON) {
if (outputFile) {
Expand Down
1 change: 1 addition & 0 deletions packages/jest-test-result/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type {
TestEvents,
TestFileEvent,
TestResult,
TestResultsProcessor,
TestCaseResult,
V8CoverageResult,
} from './types';
4 changes: 4 additions & 0 deletions packages/jest-test-result/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export type AggregatedResult = AggregatedResultWithoutCoverage & {
coverageMap?: CoverageMap | null;
};

export type TestResultsProcessor = (
results: AggregatedResult,
) => AggregatedResult;

export type Suite = {
title: string;
suites: Array<Suite>;
Expand Down

0 comments on commit b9a3048

Please sign in to comment.