Skip to content

Commit

Permalink
feat(jest-core): allow using Summary Reporter as stand-alone reporter (
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas authored Apr 18, 2022
1 parent 77b0f17 commit dd972da
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 126 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- `[jest-core]` Pass project config to `globalSetup`/`globalTeardown` function as second argument ([#12440](https://github.com/facebook/jest/pull/12440))
- `[jest-core]` Stabilize test runners with event emitters ([#12641](https://github.com/facebook/jest/pull/12641))
- `[jest-core, jest-watcher]` [**BREAKING**] Move `TestWatcher` class to `jest-watcher` package ([#12652](https://github.com/facebook/jest/pull/12652))
- `[jest-core]` Allow using Summary Reporter as stand-alone reporter ([#12687](https://github.com/facebook/jest/pull/12687))
- `[jest-environment-jsdom]` [**BREAKING**] Upgrade jsdom to 19.0.0 ([#12290](https://github.com/facebook/jest/pull/12290))
- `[jest-environment-jsdom]` [**BREAKING**] Add default `browser` condition to `exportConditions` for `jsdom` environment ([#11924](https://github.com/facebook/jest/pull/11924))
- `[jest-environment-jsdom]` [**BREAKING**] Pass global config to Jest environment constructor for `jsdom` environment ([#12461](https://github.com/facebook/jest/pull/12461))
Expand Down
10 changes: 10 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,16 @@ If included in the list, the built-in GitHub Actions Reporter will annotate chan
}
```

#### Summary Reporter

Summary reporter prints out summary of all tests. It is a part of default reporter, hence it will be enabled if `'default'` is included in the list. For instance, you might want to use it as stand-alone reporter instead of the default one, or together with [Silent Reporter](https://github.com/rickhanlonii/jest-silent-reporter):

```json
{
"reporters": ["jest-silent-reporter", "summary"]
}
```

#### Custom Reporters

:::tip
Expand Down
2 changes: 0 additions & 2 deletions e2e/__tests__/customReporters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ describe('Custom Reporters Integration', () => {
'package.json': JSON.stringify({
jest: {
reporters: ['default', '<rootDir>/reporter.js'],
testEnvironment: 'node',
},
}),
'reporter.js': `
Expand All @@ -167,7 +166,6 @@ describe('Custom Reporters Integration', () => {
'package.json': JSON.stringify({
jest: {
reporters: ['default', '<rootDir>/reporter.mjs'],
testEnvironment: 'node',
},
}),
'reporter.mjs': `
Expand Down
1 change: 1 addition & 0 deletions jest.config.ci.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ export default {
'jest-silent-reporter',
{showPaths: true, showWarnings: true, useDots: true},
],
'summary',
],
};
2 changes: 1 addition & 1 deletion packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ const normalizeReporters = (options: Config.InitialOptionsWithRootDir) => {
normalizedReporterConfig[0],
);

if (!['default', 'github-actions'].includes(reporterPath)) {
if (!['default', 'github-actions', 'summary'].includes(reporterPath)) {
const reporter = Resolver.findNodeModule(reporterPath, {
basedir: options.rootDir,
});
Expand Down
102 changes: 41 additions & 61 deletions packages/jest-core/src/TestScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ type TestRunnerConstructor = new (

export type TestSchedulerContext = ReporterContext & TestRunnerContext;

type ReporterMap = Record<string, Record<string, unknown>>;

export async function createTestScheduler(
globalConfig: Config.GlobalConfig,
context: TestSchedulerContext,
Expand Down Expand Up @@ -329,77 +327,59 @@ class TestScheduler {
}

async _setupReporters() {
const {collectCoverage, notify, reporters} = this._globalConfig;
const {collectCoverage: coverage, notify, verbose} = this._globalConfig;
const reporters = this._globalConfig.reporters || [['default', {}]];
let summary = false;

for (const [reporter, options] of reporters) {
switch (reporter) {
case 'default':
summary = true;
verbose
? this.addReporter(new VerboseReporter(this._globalConfig))
: this.addReporter(new DefaultReporter(this._globalConfig));
break;
case 'github-actions':
GITHUB_ACTIONS && this.addReporter(new GitHubActionsReporter());
break;
case 'summary':
summary = true;
break;
default:
await this._addCustomReporter(reporter, options);
}
}

if (notify) {
this.addReporter(new NotifyReporter(this._globalConfig, this._context));
}

if (!reporters) {
this._setupDefaultReporters(collectCoverage);
return;
}

let reporterMap: ReporterMap = {};

reporters.forEach(reporter => {
reporterMap = Object.assign(reporterMap, {
[reporter[0]]: reporter[1],
});
});

const reporterNames = Object.keys(reporterMap);

const isDefault = reporterNames.includes('default');
const isGitHubActions =
GITHUB_ACTIONS && reporterNames.includes('github-actions');

if (isDefault) {
this._setupDefaultReporters(collectCoverage);
}

if (isGitHubActions) {
this.addReporter(new GitHubActionsReporter());
}

if (!isDefault && collectCoverage) {
if (coverage) {
this.addReporter(new CoverageReporter(this._globalConfig, this._context));
}

if (reporterNames.length) {
await this._addCustomReporters(reporterMap);
}
}

private _setupDefaultReporters(collectCoverage: boolean) {
this.addReporter(
this._globalConfig.verbose
? new VerboseReporter(this._globalConfig)
: new DefaultReporter(this._globalConfig),
);

if (collectCoverage) {
this.addReporter(new CoverageReporter(this._globalConfig, this._context));
if (summary) {
this.addReporter(new SummaryReporter(this._globalConfig));
}

this.addReporter(new SummaryReporter(this._globalConfig));
}

private async _addCustomReporters(reporters: ReporterMap) {
for (const path in reporters) {
if (['default', 'github-actions'].includes(path)) continue;
private async _addCustomReporter(
reporter: string,
options: Record<string, unknown>,
) {
try {
const Reporter: ReporterConstructor = await requireOrImportModule(
reporter,
);

try {
const Reporter: ReporterConstructor = await requireOrImportModule(path);
this.addReporter(
new Reporter(this._globalConfig, reporters[path], this._context),
);
} catch (error: any) {
error.message = `An error occurred while adding the reporter at path "${chalk.bold(
path,
)}".\n${error.message}`;
throw error;
}
this.addReporter(
new Reporter(this._globalConfig, options, this._context),
);
} catch (error: any) {
error.message = `An error occurred while adding the reporter at path "${chalk.bold(
reporter,
)}".\n${error instanceof Error ? error.message : ''}`;
throw error;
}
}

Expand Down
Loading

0 comments on commit dd972da

Please sign in to comment.