Skip to content

Commit 77c17bc

Browse files
authored
feat: allow reporters to be default exports (#9161)
1 parent 314afd4 commit 77c17bc

File tree

7 files changed

+67
-7
lines changed

7 files changed

+67
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- `[jest-config]` Throw the full error message and stack when a Jest preset is missing a dependency ([#8924](https://github.com/facebook/jest/pull/8924))
1111
- `[jest-config]` [**BREAKING**] Set default display name color based on runner ([#8689](https://github.com/facebook/jest/pull/8689))
1212
- `[jest-config]` Merge preset globals with project globals ([#9027](https://github.com/facebook/jest/pull/9027))
13+
- `[jest-core]` Support reporters as default exports ([#9161](https://github.com/facebook/jest/pull/9161))
1314
- `[jest-diff]` Add options for colors and symbols ([#8841](https://github.com/facebook/jest/pull/8841))
1415
- `[jest-diff]` [**BREAKING**] Export as ECMAScript module ([#8873](https://github.com/facebook/jest/pull/8873))
1516
- `[jest-diff]` Add `includeChangeCounts` and rename `Indicator` options ([#8881](https://github.com/facebook/jest/pull/8881))

docs/Configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ class MyCustomReporter {
627627
}
628628

629629
module.exports = MyCustomReporter;
630+
// or export default MyCustomReporter;
630631
```
631632

632633
Custom reporters can also force Jest to exit with non-0 code by returning an Error from `getLastError()` methods

e2e/__tests__/__snapshots__/customReporters.test.ts.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ exports[`Custom Reporters Integration invalid format for adding reporters 1`] =
119119
120120
`;
121121
122+
exports[`Custom Reporters Integration reporters can be default exports 1`] = `
123+
run start
124+
test start
125+
test complete
126+
run complete
127+
`;
128+
122129
exports[`Custom Reporters Integration valid array format for adding reporters 1`] = `
123130
{
124131
"onRunComplete": {

e2e/__tests__/customReporters.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ describe('Custom Reporters Integration', () => {
121121
expect(wrap(stdout)).toMatchSnapshot();
122122
});
123123

124+
test('reporters can be default exports', () => {
125+
const {stderr, stdout, exitCode} = runJest('custom-reporters', [
126+
'--no-cache',
127+
'--config',
128+
JSON.stringify({
129+
reporters: ['<rootDir>/reporters/DefaultExportReporter.js'],
130+
}),
131+
'add.test.js',
132+
]);
133+
134+
expect(stderr).toBe('');
135+
expect(exitCode).toBe(0);
136+
expect(wrap(stdout)).toMatchSnapshot();
137+
});
138+
124139
test('prints reporter errors', () => {
125140
writeFiles(DIR, {
126141
'__tests__/test.test.js': `test('test', () => {});`,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
// TODO: use babel to transpile actual import/export in Jest 26
9+
Object.defineProperty(exports, '__esModule', {
10+
value: true,
11+
});
12+
exports.default = void 0;
13+
14+
const _reporters = require('@jest/reporters');
15+
16+
class TestReporter extends _reporters.BaseReporter {
17+
onTestStart() {
18+
console.log('test start');
19+
}
20+
21+
onTestResult() {
22+
console.log('test complete');
23+
}
24+
25+
onRunStart() {
26+
console.log('run start');
27+
}
28+
29+
onRunComplete() {
30+
console.log('run complete');
31+
}
32+
}
33+
34+
exports.default = TestReporter;

packages/jest-config/src/normalize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ const normalizeReporters = (options: Config.InitialOptionsWithRootDir) => {
393393
basedir: options.rootDir,
394394
});
395395
if (!reporter) {
396-
throw new Error(
396+
throw new Resolver.ModuleNotFoundError(
397397
`Could not resolve a module for a custom reporter.\n` +
398398
` Module name: ${reporterPath}`,
399399
);

packages/jest-core/src/TestScheduler.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
buildFailureTestResult,
2929
makeEmptyAggregatedTestResult,
3030
} from '@jest/test-result';
31+
import {interopRequireDefault} from 'jest-util';
3132
import ReporterDispatcher from './ReporterDispatcher';
3233
import TestWatcher from './TestWatcher';
3334
import {shouldRunInBand} from './testSchedulerHelper';
@@ -315,15 +316,16 @@ export default class TestScheduler {
315316
if (path === 'default') return;
316317

317318
try {
318-
const Reporter = require(path);
319+
// TODO: Use `requireAndTranspileModule` for Jest 26
320+
const Reporter = interopRequireDefault(require(path)).default;
319321
this.addReporter(new Reporter(this._globalConfig, options));
320322
} catch (error) {
321-
throw new Error(
323+
error.message =
322324
'An error occurred while adding the reporter at path "' +
323-
path +
324-
'".' +
325-
error.message,
326-
);
325+
chalk.bold(path) +
326+
'".' +
327+
error.message;
328+
throw error;
327329
}
328330
});
329331
}

0 commit comments

Comments
 (0)