Skip to content

Commit

Permalink
simplify to replace _log
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Jan 27, 2019
1 parent 17e6ace commit 2f2e35d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 53 deletions.
37 changes: 16 additions & 21 deletions packages/jest-runner/src/runTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
* @flow
*/

import type {ConsoleBuffer} from 'types/Console';
import type {EnvironmentClass} from 'types/Environment';
import type {GlobalConfig, Path, ProjectConfig} from 'types/Config';
import type {Resolver} from 'types/Resolve';
import type {TestFramework} from 'types/TestRunner';
import type {TestResult} from 'types/TestResult';
import type {SourceMapRegistry} from 'types/SourceMaps';
import type RuntimeClass from 'jest-runtime';

import fs from 'graceful-fs';
Expand All @@ -37,9 +35,12 @@ type RunTestInternalResult = {
result: TestResult,
};

function freezeConsole(buffer: ConsoleBuffer, config: ProjectConfig) {
function freezeConsole(
testConsole: BufferedConsole | Console | NullConsole,
config: ProjectConfig,
) {
// $FlowFixMe: overwrite it for pretty errors. `Object.freeze` works, but gives ugly errors
buffer.push = function fakeConsolePush({message}) {
testConsole._log = function fakeConsolePush({message}) {
const error = new ErrorWithStack(
chalk.red(
`${chalk.bold(
Expand Down Expand Up @@ -105,35 +106,29 @@ async function runTestInternal(

let runtime = undefined;

const getRuntimeSourceMaps: () => ?SourceMapRegistry = () =>
runtime && runtime.getSourceMaps();
const consoleOut = globalConfig.useStderr ? process.stderr : process.stdout;
const consoleFormatter = (type, message) =>
getConsoleOutput(
config.cwd,
!!globalConfig.verbose,
// 4 = the console call is buried 4 stack frames deep
BufferedConsole.write([], type, message, 4, getRuntimeSourceMaps()),
BufferedConsole.write(
[],
type,
message,
4,
runtime && runtime.getSourceMaps(),
),
);

let testConsole;

if (globalConfig.silent) {
testConsole = new NullConsole(
consoleOut,
process.stderr,
consoleFormatter,
getRuntimeSourceMaps,
);
testConsole = new NullConsole(consoleOut, process.stderr, consoleFormatter);
} else if (globalConfig.verbose) {
testConsole = new Console(
consoleOut,
process.stderr,
consoleFormatter,
getRuntimeSourceMaps,
);
testConsole = new Console(consoleOut, process.stderr, consoleFormatter);
} else {
testConsole = new BufferedConsole(getRuntimeSourceMaps);
testConsole = new BufferedConsole(() => runtime && runtime.getSourceMaps());
}

const environment = new TestEnvironment(config, {
Expand Down Expand Up @@ -232,7 +227,7 @@ async function runTestInternal(
throw err;
}

freezeConsole(testConsole.getBuffer(), config);
freezeConsole(testConsole, config);

const testCount =
result.numPassingTests +
Expand Down
35 changes: 3 additions & 32 deletions packages/jest-util/src/CustomConsole.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,29 @@
*/
/* global stream$Writable */

import type {
ConsoleBuffer,
LogCounters,
LogMessage,
LogTimers,
LogType,
} from 'types/Console';
import type {SourceMapRegistry} from 'types/SourceMaps';
import type {LogType, LogMessage, LogCounters, LogTimers} from 'types/Console';

import assert from 'assert';
import {format} from 'util';
import {Console} from 'console';
import chalk from 'chalk';
import clearLine from './clearLine';
import getCallsite from './getCallsite';

type Formatter = (type: LogType, message: LogMessage) => string;

export default class CustomConsole extends Console {
// This buffer exists so we can collect all logs for reporters
_buffer: ConsoleBuffer;
_stdout: stream$Writable;
_formatBuffer: Formatter;
_counters: LogCounters;
_timers: LogTimers;
_groupDepth: number;
_getSourceMaps: ?() => ?SourceMapRegistry;

constructor(
stdout: stream$Writable,
stderr: stream$Writable,
formatBuffer?: Formatter,
getSourceMaps?: () => ?SourceMapRegistry,
formatBuffer: ?Formatter,
) {
super(stdout, stderr);
this._buffer = [];
this._getSourceMaps = getSourceMaps;
this._formatBuffer = formatBuffer || ((type, message) => message);
this._counters = {};
this._timers = {};
Expand All @@ -55,23 +41,8 @@ export default class CustomConsole extends Console {
super.log(message);
}

_storeInBuffer(type: LogType, message: string) {
let origin = '';

if (this._getSourceMaps) {
const callsite = getCallsite(3, this._getSourceMaps());
origin = callsite.getFileName() + ':' + callsite.getLineNumber();
}

this._buffer.push({message, origin, type});
// we might want to keep this in the future for reporters (https://github.com/facebook/jest/issues/6441)
this._buffer.pop();
}

_log(type: LogType, message: string) {
clearLine(this._stdout);

this._storeInBuffer(type, message);
this._logToParentConsole(
this._formatBuffer(type, ' '.repeat(this._groupDepth) + message),
);
Expand Down Expand Up @@ -167,6 +138,6 @@ export default class CustomConsole extends Console {
}

getBuffer() {
return this._buffer;
return null;
}
}

0 comments on commit 2f2e35d

Please sign in to comment.