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

fix: make reporters respect useStderr option #6583

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -24,6 +24,7 @@
- `[jest-core]` Fix incorrect `passWithNoTests` warning ([#8595](https://github.com/facebook/jest/pull/8595))
- `[jest-snapshots]` Fix test retries that contain snapshots ([#8629](https://github.com/facebook/jest/pull/8629))
- `[jest-mock]` Fix incorrect assignments when restoring mocks in instances where they originally didn't exist ([#8631](https://github.com/facebook/jest/pull/8631))
- `[jest-reporters]` Make reporters respect `useStderr` option and write to `stdout` by default ([#6583](https://github.com/facebook/jest/pull/6583))

### Chore & Maintenance

Expand Down
14 changes: 12 additions & 2 deletions packages/jest-reporters/src/base_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,29 @@

import {AggregatedResult, TestResult} from '@jest/test-result';
import {preRunMessage} from 'jest-util';
import {Config} from '@jest/types';
import {ReporterOnStartOptions, Context, Test, Reporter} from './types';

const {remove: preRunMessageRemove} = preRunMessage;

export default class BaseReporter implements Reporter {
private _error?: Error;
private _stream: NodeJS.WriteStream;

constructor(globalConfig?: Config.GlobalConfig) {
if (globalConfig && globalConfig.useStderr) {
this._stream = process.stderr;
} else {
this._stream = process.stdout;
}
}

log(message: string) {
process.stderr.write(message + '\n');
this._stream.write(message + '\n');
}

onRunStart(_results: AggregatedResult, _options: ReporterOnStartOptions) {
preRunMessageRemove(process.stderr);
preRunMessageRemove(this._stream);
}

onTestResult(
Expand Down
11 changes: 8 additions & 3 deletions packages/jest-reporters/src/coverage_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class CoverageReporter extends BaseReporter {
globalConfig: Config.GlobalConfig,
options?: CoverageReporterOptions,
) {
super();
super(globalConfig);
this._coverageMap = istanbulCoverage.createCoverageMap({});
this._globalConfig = globalConfig;
this._sourceMapStore = libSourceMaps.createSourceMapStore();
Expand Down Expand Up @@ -143,9 +143,14 @@ export default class CoverageReporter extends BaseReporter {
}

if (isInteractive) {
process.stderr.write(
RUNNING_TEST_COLOR('Running coverage on untested files...'),
const message = RUNNING_TEST_COLOR(
'Running coverage on untested files...',
);
if (this._globalConfig.useStderr) {
process.stderr.write(message);
} else {
process.stdout.write(message);
}
}

let worker: CoverageWorker | Worker;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-reporters/src/default_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class DefaultReporter extends BaseReporter {
private _bufferedOutput: Set<FlushBufferedOutput>;

constructor(globalConfig: Config.GlobalConfig) {
super();
super(globalConfig);
this._globalConfig = globalConfig;
this._clear = '';
this._out = process.stdout.write.bind(process.stdout);
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-reporters/src/notify_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class NotifyReporter extends BaseReporter {
startRun: (globalConfig: Config.GlobalConfig) => any,
context: TestSchedulerContext,
) {
super();
super(globalConfig);
this._globalConfig = globalConfig;
this._startRun = startRun;
this._context = context;
Expand Down
9 changes: 7 additions & 2 deletions packages/jest-reporters/src/summary_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default class SummaryReporter extends BaseReporter {
private _globalConfig: Config.GlobalConfig;

constructor(globalConfig: Config.GlobalConfig) {
super();
super(globalConfig);
this._globalConfig = globalConfig;
this._estimatedTime = 0;
}
Expand All @@ -67,7 +67,12 @@ export default class SummaryReporter extends BaseReporter {
// when hundreds of tests are failing.
private _write(string: string) {
for (let i = 0; i < string.length; i++) {
process.stderr.write(string.charAt(i));
const char = string.charAt(i);
if (this._globalConfig.useStderr) {
process.stderr.write(char);
} else {
process.stdout.write(char);
}
}
}

Expand Down