Skip to content

Commit

Permalink
chore: create new @jest/console package (#8030)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Mar 3, 2019
1 parent b3e80ef commit bcc2b10
Show file tree
Hide file tree
Showing 31 changed files with 135 additions and 61 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -79,6 +79,7 @@
- `[docs]`: Update CONTRIBUTING.md to add information about running jest with `jest-circus` locally ([#8013](https://github.com/facebook/jest/pull/8013)).
- `[@jest/core]`: Migrate to TypeScript ([#7998](https://github.com/facebook/jest/pull/7998))
- `[@jest/source-map]`: Extract `getCallsite` function from `jest-util` into a new separate package ([#8029](https://github.com/facebook/jest/pull/8029))
- `[@jest/console]`: Extract custom `console` implementations from `jest-util` into a new separate package ([#8030](https://github.com/facebook/jest/pull/8030))

### Performance

Expand Down
Expand Up @@ -15,6 +15,6 @@ PASS __tests__/console.test.js
15 | });
16 |
at BufferedConsole.log (../../packages/jest-util/build/BufferedConsole.js:180:10)
at BufferedConsole.log (../../packages/jest-console/build/BufferedConsole.js:180:10)
at log (__tests__/console.test.js:13:13)
`;
3 changes: 3 additions & 0 deletions packages/jest-console/.npmignore
@@ -0,0 +1,3 @@
**/__mocks__/**
**/__tests__/**
src
25 changes: 25 additions & 0 deletions packages/jest-console/package.json
@@ -0,0 +1,25 @@
{
"name": "@jest/console",
"version": "24.1.0",
"repository": {
"type": "git",
"url": "https://github.com/facebook/jest.git",
"directory": "packages/jest-console"
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@jest/source-map": "^24.1.0",
"@types/node": "*",
"chalk": "^2.0.1",
"slash": "^2.0.0"
},
"devDependencies": {
"@types/slash": "^2.0.0"
},
"engines": {
"node": ">= 6"
},
"gitHead": "634e5a54f46b2a62d1dc81a170562e6f4e55ad60"
}
Expand Up @@ -9,18 +9,24 @@ import assert from 'assert';
import {Console} from 'console';
import {format} from 'util';
import chalk from 'chalk';
import {Console as ConsoleType} from '@jest/types';
import {getCallsite, SourceMapRegistry} from '@jest/source-map';
import {
ConsoleBuffer,
LogCounters,
LogMessage,
LogTimers,
LogType,
} from './types';

export default class BufferedConsole extends Console {
private _buffer: ConsoleType.ConsoleBuffer;
private _counters: ConsoleType.LogCounters;
private _timers: ConsoleType.LogTimers;
private _buffer: ConsoleBuffer;
private _counters: LogCounters;
private _timers: LogTimers;
private _groupDepth: number;
private _getSourceMaps: () => SourceMapRegistry | null | undefined;

constructor(getSourceMaps: () => SourceMapRegistry | null | undefined) {
const buffer: ConsoleType.ConsoleBuffer = [];
const buffer: ConsoleBuffer = [];
super({
write: (message: string) => {
BufferedConsole.write(buffer, 'log', message, null, getSourceMaps());
Expand All @@ -36,9 +42,9 @@ export default class BufferedConsole extends Console {
}

static write(
buffer: ConsoleType.ConsoleBuffer,
type: ConsoleType.LogType,
message: ConsoleType.LogMessage,
buffer: ConsoleBuffer,
type: LogType,
message: LogMessage,
level?: number | null,
sourceMaps?: SourceMapRegistry | null,
) {
Expand All @@ -54,7 +60,7 @@ export default class BufferedConsole extends Console {
return buffer;
}

private _log(type: ConsoleType.LogType, message: ConsoleType.LogMessage) {
private _log(type: LogType, message: LogMessage) {
BufferedConsole.write(
this._buffer,
type,
Expand Down Expand Up @@ -153,7 +159,7 @@ export default class BufferedConsole extends Console {
this._log('warn', format(firstArg, ...rest));
}

getBuffer(): ConsoleType.ConsoleBuffer {
getBuffer(): ConsoleBuffer {
return this._buffer;
}
}
Expand Up @@ -9,19 +9,22 @@ import assert from 'assert';
import {format} from 'util';
import {Console} from 'console';
import chalk from 'chalk';
import {Console as ConsoleType} from '@jest/types';
import clearLine from './clearLine';
import {LogCounters, LogMessage, LogTimers, LogType} from './types';

type Formatter = (
type: ConsoleType.LogType,
message: ConsoleType.LogMessage,
) => string;
// TODO: Copied from `jest-util`. Import from it in Jest 25
function clearLine(stream: NodeJS.WritableStream) {
if (process.stdout.isTTY) {
stream.write('\x1b[999D\x1b[K');
}
}

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

export default class CustomConsole extends Console {
private _stdout: NodeJS.WritableStream;
private _formatBuffer: Formatter;
private _counters: ConsoleType.LogCounters;
private _timers: ConsoleType.LogTimers;
private _counters: LogCounters;
private _timers: LogTimers;
private _groupDepth: number;

constructor(
Expand All @@ -41,7 +44,7 @@ export default class CustomConsole extends Console {
super.log(message);
}

private _log(type: ConsoleType.LogType, message: string) {
private _log(type: LogType, message: string) {
clearLine(this._stdout);
this._logToParentConsole(
this._formatBuffer(type, ' '.repeat(this._groupDepth) + message),
Expand Down
File renamed without changes.
Expand Up @@ -8,13 +8,9 @@
import path from 'path';
import chalk from 'chalk';
import slash from 'slash';
import {Console} from '@jest/types';
import {ConsoleBuffer} from './types';

export default (
root: string,
verbose: boolean,
buffer: Console.ConsoleBuffer,
) => {
export default (root: string, verbose: boolean, buffer: ConsoleBuffer) => {
const TITLE_INDENT = verbose ? ' ' : ' ';
const CONSOLE_INDENT = TITLE_INDENT + ' ';

Expand Down
12 changes: 12 additions & 0 deletions packages/jest-console/src/index.ts
@@ -0,0 +1,12 @@
/**
* 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.
*/

export {default as BufferedConsole} from './BufferedConsole';
export {default as CustomConsole} from './CustomConsole';
export {default as NullConsole} from './NullConsole';
export {default as getConsoleOutput} from './getConsoleOutput';
export {ConsoleBuffer, LogMessage, LogType} from './types';
File renamed without changes.
10 changes: 10 additions & 0 deletions packages/jest-console/tsconfig.json
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
},
"references": [
{"path": "../jest-source-map"}
]
}
1 change: 1 addition & 0 deletions packages/jest-core/package.json
Expand Up @@ -5,6 +5,7 @@
"main": "build/jest.js",
"types": "build/jest.d.ts",
"dependencies": {
"@jest/console": "^24.1.0",
"@jest/types": "^24.1.0",
"@jest/reporters": "^24.1.0",
"@jest/transform": "^24.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/__tests__/run_jest.test.js
Expand Up @@ -2,7 +2,7 @@

import runJest from '../runJest';

jest.mock('jest-util');
jest.mock('@jest/console');

const processErrWriteFn = process.stderr.write;
describe('runJest', () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-core/src/cli/index.ts
Expand Up @@ -6,7 +6,8 @@
*/

import {Config, TestResult} from '@jest/types';
import {Console, createDirectory, preRunMessage} from 'jest-util';
import {CustomConsole} from '@jest/console';
import {createDirectory, preRunMessage} from 'jest-util';
import {readConfigs} from 'jest-config';
import Runtime, {Context} from 'jest-runtime';
import {ChangedFilesPromise} from 'jest-changed-files';
Expand Down Expand Up @@ -118,7 +119,7 @@ const buildContextsAndHasteMaps = async (
configs.map(async (config, index) => {
createDirectory(config.cacheDirectory);
const hasteMapInstance = Runtime.createHasteMap(config, {
console: new Console(outputStream, outputStream),
console: new CustomConsole(outputStream, outputStream),
maxWorkers: globalConfig.maxWorkers,
resetCache: !config.cache,
watch: globalConfig.watch || globalConfig.watchAll,
Expand Down
9 changes: 5 additions & 4 deletions packages/jest-core/src/runJest.ts
Expand Up @@ -8,7 +8,8 @@
import path from 'path';
import chalk from 'chalk';
import {sync as realpath} from 'realpath-native';
import {Console, formatTestResults} from 'jest-util';
import {CustomConsole} from '@jest/console';
import {formatTestResults} from 'jest-util';
import exit from 'exit';
import fs from 'graceful-fs';
import {JestHook, JestHookEmitter} from 'jest-watcher';
Expand Down Expand Up @@ -38,7 +39,7 @@ const getTestPaths = async (
const data = await source.getTestPaths(globalConfig, changedFiles);

if (!data.tests.length && globalConfig.onlyChanged && data.noSCM) {
new Console(outputStream, outputStream).log(
new CustomConsole(outputStream, outputStream).log(
'Jest can only find uncommitted changed files in a git or hg ' +
'repository. If you make your project a git or hg ' +
'repository (`git init` or `hg init`), Jest will be able ' +
Expand Down Expand Up @@ -204,9 +205,9 @@ export default (async function runJest({
globalConfig.lastCommit ||
globalConfig.onlyChanged
) {
new Console(outputStream, outputStream).log(noTestsFoundMessage);
new CustomConsole(outputStream, outputStream).log(noTestsFoundMessage);
} else {
new Console(outputStream, outputStream).error(noTestsFoundMessage);
new CustomConsole(outputStream, outputStream).error(noTestsFoundMessage);

exit(1);
}
Expand Down
1 change: 1 addition & 0 deletions packages/jest-core/tsconfig.json
Expand Up @@ -8,6 +8,7 @@
"references": [
{"path": "../jest-changed-files"},
{"path": "../jest-config"},
{"path": "../jest-console"},
{"path": "../jest-haste-map"},
{"path": "../jest-message-util"},
{"path": "../jest-regex-util"},
Expand Down
1 change: 1 addition & 0 deletions packages/jest-runner/package.json
Expand Up @@ -10,6 +10,7 @@
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@jest/console": "^24.1.0",
"@jest/environment": "^24.1.0",
"@jest/types": "^24.1.0",
"chalk": "^2.4.2",
Expand Down
36 changes: 19 additions & 17 deletions packages/jest-runner/src/runTest.ts
Expand Up @@ -6,18 +6,19 @@
*
*/

import {Config, TestResult, Console as ConsoleType} from '@jest/types';
import {JestEnvironment} from '@jest/environment';
import RuntimeClass from 'jest-runtime';
import fs from 'graceful-fs';
import {Config, TestResult} from '@jest/types';
import {
BufferedConsole,
Console,
ErrorWithStack,
CustomConsole,
NullConsole,
LogType,
LogMessage,
getConsoleOutput,
setGlobal,
} from 'jest-util';
} from '@jest/console';
import {JestEnvironment} from '@jest/environment';
import RuntimeClass from 'jest-runtime';
import fs from 'graceful-fs';
import {ErrorWithStack, setGlobal} from 'jest-util';
import LeakDetector from 'jest-leak-detector';
import Resolver from 'jest-resolve';
import {getTestEnvironment} from 'jest-config';
Expand All @@ -35,13 +36,13 @@ type RunTestInternalResult = {
};

function freezeConsole(
// @ts-ignore: Correct types when `jest-util` is ESM
testConsole: BufferedConsole | Console | NullConsole,
testConsole: BufferedConsole | CustomConsole | NullConsole,
config: Config.ProjectConfig,
) {
// @ts-ignore: `_log` is `private` - we should figure out some proper API here
testConsole._log = function fakeConsolePush(
_type: ConsoleType.LogType,
message: ConsoleType.LogMessage,
_type: LogType,
message: LogMessage,
) {
const error = new ErrorWithStack(
`${chalk.red(
Expand Down Expand Up @@ -114,10 +115,7 @@ async function runTestInternal(
let runtime: RuntimeClass | undefined = undefined;

const consoleOut = globalConfig.useStderr ? process.stderr : process.stdout;
const consoleFormatter = (
type: ConsoleType.LogType,
message: ConsoleType.LogMessage,
) =>
const consoleFormatter = (type: LogType, message: LogMessage) =>
getConsoleOutput(
config.cwd,
!!globalConfig.verbose,
Expand All @@ -136,7 +134,11 @@ async function runTestInternal(
if (globalConfig.silent) {
testConsole = new NullConsole(consoleOut, process.stderr, consoleFormatter);
} else if (globalConfig.verbose) {
testConsole = new Console(consoleOut, process.stderr, consoleFormatter);
testConsole = new CustomConsole(
consoleOut,
process.stderr,
consoleFormatter,
);
} else {
testConsole = new BufferedConsole(() => runtime && runtime.getSourceMaps());
}
Expand Down
1 change: 1 addition & 0 deletions packages/jest-runner/tsconfig.json
Expand Up @@ -6,6 +6,7 @@
},
"references": [
{"path": "../jest-config"},
{"path": "../jest-console"},
{"path": "../jest-docblock"},
{"path": "../jest-environment"},
{"path": "../jest-haste-map"},
Expand Down
1 change: 1 addition & 0 deletions packages/jest-runtime/package.json
Expand Up @@ -10,6 +10,7 @@
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@jest/console": "^24.1.0",
"@jest/environment": "^24.1.0",
"@jest/source-map": "^24.1.0",
"@jest/transform": "^24.1.0",
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-runtime/src/cli/index.ts
Expand Up @@ -12,7 +12,8 @@ import {sync as realpath} from 'realpath-native';
import yargs from 'yargs';
import {Config} from '@jest/types';
import {JestEnvironment} from '@jest/environment';
import {Console, setGlobal} from 'jest-util';
import {CustomConsole} from '@jest/console';
import {setGlobal} from 'jest-util';
import {validateCLIOptions} from 'jest-validate';
import {readConfig, deprecationEntries} from 'jest-config';
import {VERSION} from '../version';
Expand Down Expand Up @@ -86,7 +87,7 @@ export function run(cliArgv?: Config.Argv, cliInfo?: Array<string>) {
setGlobal(
environment.global,
'console',
new Console(process.stdout, process.stderr),
new CustomConsole(process.stdout, process.stderr),
);
setGlobal(environment.global, 'jestProjectConfig', config);
setGlobal(environment.global, 'jestGlobalConfig', globalConfig);
Expand Down
1 change: 1 addition & 0 deletions packages/jest-runtime/tsconfig.json
Expand Up @@ -6,6 +6,7 @@
},
"references": [
{"path": "../jest-config"},
{"path": "../jest-console"},
{"path": "../jest-environment"},
{"path": "../jest-environment-node"},
{"path": "../jest-haste-map"},
Expand Down
1 change: 1 addition & 0 deletions packages/jest-types/package.json
Expand Up @@ -13,6 +13,7 @@
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@jest/console": "^24.1.0",
"@types/istanbul-lib-coverage": "^1.1.0"
}
}

0 comments on commit bcc2b10

Please sign in to comment.