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

Added compact print option for default reporter #7845

Closed
wants to merge 41 commits into from
Closed
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
f9f6c1a
Added compact print option for default reporter
doniyor2109 Feb 10, 2019
56094dc
Merge remote-tracking branch 'jest/master' into tests_summary
doniyor2109 Feb 10, 2019
55ec7a5
Fix linter errors
doniyor2109 Feb 10, 2019
96cf325
Minor fixes
doniyor2109 Feb 10, 2019
c2d41c7
Added e2e tests
doniyor2109 Feb 10, 2019
87816d1
Fix bug when reporters is null
doniyor2109 Feb 10, 2019
88a464c
Changelog
doniyor2109 Feb 10, 2019
e64bc67
Minor tweaks
doniyor2109 Feb 10, 2019
d7158dd
Extends default reporter options for verbose reporter
doniyor2109 Feb 10, 2019
12cb996
Missing snapshot file
doniyor2109 Feb 10, 2019
6f594e2
Typo in test folder
doniyor2109 Feb 10, 2019
24ab5d3
Minor tweaks
doniyor2109 Feb 10, 2019
69c0226
Merge remote-tracking branch 'jest/master' into tests_summary
doniyor2109 Feb 17, 2019
1549e4e
Fix changelog
doniyor2109 Feb 17, 2019
cce75e3
Add peer dep
doniyor2109 Feb 17, 2019
f064e0f
Merge remote-tracking branch 'jest/master' into tests_summary
doniyor2109 Feb 17, 2019
a2ad80a
Merge remote-tracking branch 'jest/master' into tests_summary
doniyor2109 Mar 5, 2019
a2558dd
Add project refs
doniyor2109 Mar 5, 2019
d424a9a
Fix merge errors
doniyor2109 Mar 5, 2019
97772a9
Fix ts errors
doniyor2109 Mar 5, 2019
c7e9101
Fix ts errors
doniyor2109 Mar 6, 2019
23fb8df
Convert tests to ts
doniyor2109 Mar 6, 2019
ebd06d1
Update snapshot
doniyor2109 Mar 6, 2019
6540bbc
Update CHANGELOG.md
SimenB Mar 6, 2019
4fccead
Update CHANGELOG.md
SimenB Mar 6, 2019
9fb013f
Tweaks
doniyor2109 Mar 6, 2019
4d878b0
Merge remote-tracking branch 'jest/master' into tests_summary
doniyor2109 Mar 7, 2019
20df6db
Merge remote-tracking branch 'jest/master' into tests_summary
doniyor2109 Mar 8, 2019
d142e3e
Merge remote-tracking branch 'jest/master' into tests_summary
doniyor2109 Mar 8, 2019
6e2edcd
Tweaks
SimenB Mar 8, 2019
509fdf1
Tweaks
SimenB Mar 8, 2019
503e85e
Tweaks
doniyor2109 Mar 8, 2019
41fd5ad
Tweaks
doniyor2109 Mar 8, 2019
ddf293b
Tweak
SimenB Mar 8, 2019
00a9f1c
private method
SimenB Mar 8, 2019
eed3d51
Fix
doniyor2109 Mar 8, 2019
f4e1d47
Merge branch 'master' into tests_summary
doniyor2109 Mar 10, 2019
9c722a4
Tweaks
doniyor2109 Mar 11, 2019
5d5eddb
Merge branch 'master' into tests_summary
thymikee Mar 18, 2019
ed98e6c
Merge branch 'master' into tests_summary
SimenB Mar 20, 2019
9143bd4
move changelog entry
SimenB Mar 20, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features

- `[jest-cli]`: Add `compact` option to default reporter ([#7845](https://github.com/facebook/jest/pull/7845))
doniyor2109 marked this conversation as resolved.
Show resolved Hide resolved
- `[jest-resolve]` Now supports PnP environment without plugins ([#8094](https://github.com/facebook/jest/pull/8094))

### Fixes
Expand Down
10 changes: 10 additions & 0 deletions e2e/__tests__/__snapshots__/defaultReporter.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`default reporter prints compact message 1`] = `""`;

exports[`default reporter prints compact message 2`] = `
"FAIL __tests__/fail.test.js
● failing tests › fails __tests__/fail.test.js:13:11
● failing tests › fails 2 __tests__/fail.test.js:17:11
● failing tests › fails 3 __tests__/fail.test.js:23:11"
`;
21 changes: 21 additions & 0 deletions e2e/__tests__/defaultReporter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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.
*/

import {extractSummary} from '../Utils';
import runJest from '../runJest';

test('default reporter prints compact message', () => {
const {stdout, stderr} = runJest('default-reporter', [
'--verbose=false',
'--config',
JSON.stringify({
reporters: [['default', {compact: true}]],
}),
]);
expect(stdout).toMatchSnapshot();
expect(extractSummary(stderr).rest).toMatchSnapshot();
});
27 changes: 27 additions & 0 deletions e2e/default-reporter/__tests__/fail.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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.
*
*/

'use strict';

describe('failing tests', () => {
it('fails', () => {
throw new Error(`failing test`);
});

it('fails 2', () => {
throw new Error(`failing test`);
});

it('passes', () => {});

it('fails 3', () => {
throw new Error(`failing test`);
});

it('passes 2', () => {});
});
3 changes: 3 additions & 0 deletions e2e/default-reporter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
doniyor2109 marked this conversation as resolved.
Show resolved Hide resolved

}
19 changes: 17 additions & 2 deletions packages/jest-core/src/TestScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,14 @@ export default class TestScheduler {
private _setupDefaultReporters(collectCoverage: boolean) {
this.addReporter(
this._globalConfig.verbose
? new VerboseReporter(this._globalConfig)
: new DefaultReporter(this._globalConfig),
? new VerboseReporter(
this._globalConfig,
this._getReporterOption('verbose'),
)
: new DefaultReporter(
this._globalConfig,
this._getReporterOption('default'),
),
);

if (collectCoverage) {
Expand Down Expand Up @@ -365,6 +371,15 @@ export default class TestScheduler {
}
return Promise.resolve();
}

private _getReporterOption(reporterName: string): Record<string, any> {
const reporters = this._globalConfig.reporters || [];
const config = reporters.find(
(item): item is Config.ReporterConfig =>
Array.isArray(item) && item[0] === reporterName,
);
return (config && config[1]) || {};
}
}

const createAggregatedResults = (numTotalTestSuites: number) => {
Expand Down
18 changes: 9 additions & 9 deletions packages/jest-message-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ const ANONYMOUS_FN_IGNORE = /^\s+at <anonymous>.*$/;
const ANONYMOUS_PROMISE_IGNORE = /^\s+at (new )?Promise \(<anonymous>\).*$/;
const ANONYMOUS_GENERATOR_IGNORE = /^\s+at Generator.next \(<anonymous>\).*$/;
const NATIVE_NEXT_IGNORE = /^\s+at next \(native\).*$/;
const TITLE_INDENT = ' ';
export const TITLE_INDENT = ' ';
const MESSAGE_INDENT = ' ';
const STACK_INDENT = ' ';
const ANCESTRY_SEPARATOR = ' \u203A ';
const TITLE_BULLET = chalk.bold('\u25cf ');
export const TITLE_BULLET = chalk.bold('\u25cf ');
const STACK_TRACE_COLOR = chalk.dim;
const STACK_PATH_REGEXP = /\s*at.*\(?(\:\d*\:\d*|native)\)?/;
const EXEC_ERROR_MESSAGE = 'Test suite failed to run';
Expand Down Expand Up @@ -318,13 +318,8 @@ export const formatResultsErrors = (
message = indentAllLines(message, MESSAGE_INDENT);

const title =
chalk.bold.red(
TITLE_INDENT +
TITLE_BULLET +
result.ancestorTitles.join(ANCESTRY_SEPARATOR) +
(result.ancestorTitles.length ? ANCESTRY_SEPARATOR : '') +
result.title,
) + '\n';
chalk.bold.red(TITLE_INDENT + TITLE_BULLET + formatFullTitle(result)) +
'\n';

return title + '\n' + message + '\n' + stack;
})
Expand Down Expand Up @@ -354,3 +349,8 @@ export const separateMessageFromStack = (content: string) => {
const stack = messageMatch[2];
return {message, stack};
};

export const formatFullTitle = (result: AssertionResult) =>
result.ancestorTitles.join(ANCESTRY_SEPARATOR) +
(result.ancestorTitles.length ? ANCESTRY_SEPARATOR : '') +
result.title;
1 change: 1 addition & 0 deletions packages/jest-reporters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"istanbul-lib-instrument": "^3.0.1",
"istanbul-lib-source-maps": "^3.0.1",
"jest-haste-map": "^24.3.1",
"jest-message-util": "^24.3.0",
"jest-resolve": "^24.3.1",
"jest-runtime": "^24.3.1",
"jest-util": "^24.3.0",
Expand Down
56 changes: 46 additions & 10 deletions packages/jest-reporters/src/default_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@
*/

import {Config} from '@jest/types';
import {AggregatedResult, TestResult} from '@jest/test-result';
import {AggregatedResult, TestResult, AssertionResult} from '@jest/test-result';
import {clearLine, getConsoleOutput, isInteractive} from 'jest-util';
import {formatFullTitle, TITLE_INDENT, TITLE_BULLET} from 'jest-message-util';
import chalk from 'chalk';
import {Test, ReporterOnStartOptions} from './types';
import BaseReporter from './base_reporter';
import Status from './Status';
import getResultHeader from './get_result_header';
import getSnapshotStatus from './get_snapshot_status';
import {formatFullPath} from './utils';

type write = (chunk: string, enc?: any, cb?: () => void) => boolean;
type FlushBufferedOutput = () => void;

const TITLE_BULLET = chalk.bold('\u25cf ');
export type DefaultReporterOptions = {
compact?: boolean;
};

export default class DefaultReporter extends BaseReporter {
private _clear: string; // ANSI clear sequence for the last printed status
Expand All @@ -27,10 +30,15 @@ export default class DefaultReporter extends BaseReporter {
private _out: write;
private _status: Status;
private _bufferedOutput: Set<FlushBufferedOutput>;
private _options: DefaultReporterOptions;

constructor(globalConfig: Config.GlobalConfig) {
constructor(
globalConfig: Config.GlobalConfig,
options: DefaultReporterOptions = {},
) {
super();
this._globalConfig = globalConfig;
this._options = options;
this._clear = '';
this._out = process.stdout.write.bind(process.stdout);
this._err = process.stderr.write.bind(process.stderr);
Expand Down Expand Up @@ -149,11 +157,19 @@ export default class DefaultReporter extends BaseReporter {
test.context.config,
testResult,
);
this.printTestFileFailureMessage(
testResult.testFilePath,
test.context.config,
testResult,
);
if (this._options.compact) {
this.printCompactFailureMessage(
testResult.testFilePath,
test.context.config,
testResult,
);
} else {
this.printTestFileFailureMessage(
testResult.testFilePath,
test.context.config,
testResult,
);
}
}
this.forceFlushBufferedOutput();
}
Expand All @@ -175,7 +191,7 @@ export default class DefaultReporter extends BaseReporter {
const consoleBuffer = result.console;
if (consoleBuffer && consoleBuffer.length) {
this.log(
' ' +
TITLE_INDENT +
TITLE_BULLET +
'Console\n\n' +
getConsoleOutput(
Expand All @@ -199,4 +215,24 @@ export default class DefaultReporter extends BaseReporter {
const snapshotStatuses = getSnapshotStatus(result.snapshot, didUpdate);
snapshotStatuses.forEach(this.log);
}

printCompactFailureMessage(
_testPath: Config.Path,
config: Config.ProjectConfig,
result: TestResult,
) {
result.testResults.forEach((assertionResult: AssertionResult) => {
if (assertionResult.failureMessages.length) {
const fullPath = formatFullPath(
result.testFilePath,
config || this._globalConfig,
assertionResult,
);
const fullTitle = formatFullTitle(assertionResult);
this.log(
TITLE_INDENT + chalk.red(TITLE_BULLET + fullTitle) + ' ' + fullPath,
);
}
});
}
}
30 changes: 29 additions & 1 deletion packages/jest-reporters/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import path from 'path';
import {Config} from '@jest/types';
import {AggregatedResult} from '@jest/test-result';
import {AggregatedResult, AssertionResult, Callsite} from '@jest/test-result';
import chalk from 'chalk';
import slash from 'slash';
import {pluralize} from 'jest-util';
Expand Down Expand Up @@ -266,3 +266,31 @@ export const wrapAnsiString = (string: string, terminalWidth: number) => {
)
.join('\n');
};

export const getLocation = (
assertionResult: AssertionResult,
): Callsite | null => {
if (assertionResult.location) {
return assertionResult.location;
}
const matches = assertionResult.failureMessages[0].match(/(\d+):(\d+)/);
return (
matches && {
column: parseInt(matches[2], 10),
line: parseInt(matches[1], 10),
}
);
};

export const formatFullPath = (
testFilePath: string,
config: Config.GlobalConfig | Config.ProjectConfig,
assertionResult: AssertionResult,
) => {
let path = formatTestPath(config, testFilePath);
const location = getLocation(assertionResult);
if (location) {
path += `:${location.line}:${location.column}`;
}
return path;
};
9 changes: 6 additions & 3 deletions packages/jest-reporters/src/verbose_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ import {
import chalk from 'chalk';
import {specialChars} from 'jest-util';
import {Test} from './types';
import DefaultReporter from './default_reporter';
import DefaultReporter, {DefaultReporterOptions} from './default_reporter';

const {ICONS} = specialChars;

export default class VerboseReporter extends DefaultReporter {
protected _globalConfig: Config.GlobalConfig;

constructor(globalConfig: Config.GlobalConfig) {
super(globalConfig);
constructor(
globalConfig: Config.GlobalConfig,
options?: DefaultReporterOptions,
) {
super(globalConfig, options);
this._globalConfig = globalConfig;
}

Expand Down
1 change: 1 addition & 0 deletions packages/jest-reporters/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"references": [
{"path": "../jest-environment"},
{"path": "../jest-haste-map"},
{"path": "../jest-message-util"},
{"path": "../jest-resolve"},
{"path": "../jest-runtime"},
{"path": "../jest-test-result"},
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 @@ -22,4 +22,5 @@ export {
Status,
Suite,
TestResult,
Callsite,
doniyor2109 marked this conversation as resolved.
Show resolved Hide resolved
} from './types';
2 changes: 1 addition & 1 deletion packages/jest-test-result/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type Status =
export type Bytes = number;

export type Milliseconds = number;
type Callsite = {
export type Callsite = {
column: number;
line: number;
};
Expand Down