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: allow reporters to be default exports #9161

Merged
merged 1 commit into from Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
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
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
@@ -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
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
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