Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jest-core): support testResultsProcessor written in ESM #12006

Merged
merged 3 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 17 additions & 1 deletion e2e/__tests__/testResultsProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);
});
});
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 processor = await requireOrImportModule<TestResultsProcessor>(
testResultsProcessor,
);
processor(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