Skip to content

Commit

Permalink
feat: allow reporters to be default exports
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Nov 11, 2019
1 parent 17c70f6 commit 7f08098
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- `[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))
- `[jest-config]` [**BREAKING**] Set default display name color based on runner ([#8689](https://github.com/facebook/jest/pull/8689))
- `[jest-config]` Merge preset globals with project globals ([#9027](https://github.com/facebook/jest/pull/9027))
- `[jest-core]` Support reporters as default exports ([#9161](https://github.com/facebook/jest/pull/9161))
- `[jest-diff]` Add options for colors and symbols ([#8841](https://github.com/facebook/jest/pull/8841))
- `[jest-diff]` [**BREAKING**] Export as ECMAScript module ([#8873](https://github.com/facebook/jest/pull/8873))
- `[jest-diff]` Add `includeChangeCounts` and rename `Indicator` options ([#8881](https://github.com/facebook/jest/pull/8881))
Expand Down
1 change: 1 addition & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ class MyCustomReporter {
}

module.exports = MyCustomReporter;
// or export default MyCustomReporter;
```

Custom reporters can also force Jest to exit with non-0 code by returning an Error from `getLastError()` methods
Expand Down
7 changes: 7 additions & 0 deletions e2e/__tests__/__snapshots__/customReporters.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ exports[`Custom Reporters Integration invalid format for adding reporters 1`] =
`;
exports[`Custom Reporters Integration reporters can be default exports 1`] = `
run start
test start
test complete
run complete
`;
exports[`Custom Reporters Integration valid array format for adding reporters 1`] = `
{
"onRunComplete": {
Expand Down
15 changes: 15 additions & 0 deletions e2e/__tests__/customReporters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,21 @@ describe('Custom Reporters Integration', () => {
expect(wrap(stdout)).toMatchSnapshot();
});

test('reporters can be default exports', () => {
const {stderr, stdout, exitCode} = runJest('custom-reporters', [
'--no-cache',
'--config',
JSON.stringify({
reporters: ['<rootDir>/reporters/DefaultExportReporter.js'],
}),
'add.test.js',
]);

expect(stderr).toBe('');
expect(exitCode).toBe(0);
expect(wrap(stdout)).toMatchSnapshot();
});

test('prints reporter errors', () => {
writeFiles(DIR, {
'__tests__/test.test.js': `test('test', () => {});`,
Expand Down
34 changes: 34 additions & 0 deletions e2e/custom-reporters/reporters/DefaultExportReporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 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.
*/

// TODO: use babel to transpile actual import/export in Jest 26
Object.defineProperty(exports, '__esModule', {
value: true,
});
exports.default = void 0;

const _reporters = require('@jest/reporters');

class TestReporter extends _reporters.BaseReporter {
onTestStart() {
console.log('test start');
}

onTestResult() {
console.log('test complete');
}

onRunStart() {
console.log('run start');
}

onRunComplete() {
console.log('run complete');
}
}

exports.default = TestReporter;
2 changes: 1 addition & 1 deletion packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ const normalizeReporters = (options: Config.InitialOptionsWithRootDir) => {
basedir: options.rootDir,
});
if (!reporter) {
throw new Error(
throw new Resolver.ModuleNotFoundError(
`Could not resolve a module for a custom reporter.\n` +
` Module name: ${reporterPath}`,
);
Expand Down
14 changes: 8 additions & 6 deletions packages/jest-core/src/TestScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
buildFailureTestResult,
makeEmptyAggregatedTestResult,
} from '@jest/test-result';
import {interopRequireDefault} from 'jest-util';
import ReporterDispatcher from './ReporterDispatcher';
import TestWatcher from './TestWatcher';
import {shouldRunInBand} from './testSchedulerHelper';
Expand Down Expand Up @@ -315,15 +316,16 @@ export default class TestScheduler {
if (path === 'default') return;

try {
const Reporter = require(path);
// TODO: Use `requireAndTranspileModule` for Jest 26
const Reporter = interopRequireDefault(require(path)).default;
this.addReporter(new Reporter(this._globalConfig, options));
} catch (error) {
throw new Error(
error.message =
'An error occurred while adding the reporter at path "' +
path +
'".' +
error.message,
);
chalk.bold(path) +
'".' +
error.message;
throw error;
}
});
}
Expand Down

0 comments on commit 7f08098

Please sign in to comment.