Skip to content

Commit

Permalink
Merge branch 'main' into feat/programmatic-api
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Oct 3, 2023
2 parents 0b6f93d + 46e6ed8 commit 6877134
Show file tree
Hide file tree
Showing 19 changed files with 55 additions and 43 deletions.
9 changes: 5 additions & 4 deletions packages/jest-console/src/CustomConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import {AssertionError, strict as assert} from 'assert';
import {Console} from 'console';
import type {WriteStream} from 'tty';
import {InspectOptions, format, formatWithOptions, inspect} from 'util';
import chalk = require('chalk');
import {clearLine, formatTime} from 'jest-util';
Expand All @@ -15,8 +16,8 @@ import type {LogCounters, LogMessage, LogTimers, LogType} from './types';
type Formatter = (type: LogType, message: LogMessage) => string;

export default class CustomConsole extends Console {
private readonly _stdout: NodeJS.WriteStream;
private readonly _stderr: NodeJS.WriteStream;
private readonly _stdout: WriteStream;
private readonly _stderr: WriteStream;
private readonly _formatBuffer: Formatter;
private _counters: LogCounters = {};
private _timers: LogTimers = {};
Expand All @@ -25,8 +26,8 @@ export default class CustomConsole extends Console {
override Console: typeof Console = Console;

constructor(
stdout: NodeJS.WriteStream,
stderr: NodeJS.WriteStream,
stdout: WriteStream,
stderr: WriteStream,
formatBuffer: Formatter = (_type, message) => message,
) {
super(stdout, stderr);
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-console/src/__tests__/CustomConsole.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import {Writable} from 'stream';
import type {WriteStream} from 'tty';
import chalk = require('chalk');
import CustomConsole from '../CustomConsole';

Expand All @@ -23,14 +24,14 @@ describe('CustomConsole', () => {
_stdout += chunk.toString();
callback();
},
}) as NodeJS.WriteStream;
}) as WriteStream;

const stderr = new Writable({
write(chunk: string, _encoding, callback) {
_stderr += chunk.toString();
callback();
},
}) as NodeJS.WriteStream;
}) as WriteStream;

_console = new CustomConsole(stdout, stderr);
});
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-core/src/__tests__/watchFileChanges.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import {tmpdir} from 'os';
import * as path from 'path';
import type {WriteStream} from 'tty';
import * as fs from 'graceful-fs';
import type {AggregatedResult} from '@jest/test-result';
import {normalize} from 'jest-config';
Expand All @@ -20,7 +21,7 @@ describe('Watch mode flows with changed files', () => {
jest.resetModules();

let watch: typeof import('../watch').default;
let pipe: NodeJS.WriteStream;
let pipe: WriteStream;
let stdin: MockStdin;
const testDirectory = path.resolve(tmpdir(), 'jest-tmp');
const fileTargetPath = path.resolve(testDirectory, 'lost-file.js');
Expand Down
9 changes: 5 additions & 4 deletions packages/jest-core/src/jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import {performance} from 'perf_hooks';
import type {WriteStream} from 'tty';
import exit = require('exit');
import {CustomConsole} from '@jest/console';
import type {AggregatedResult, TestContext} from '@jest/test-result';
Expand Down Expand Up @@ -78,7 +79,7 @@ export const createJest = Jest.createJest;
const buildContextsAndHasteMaps = async (
configs: Array<Config.ProjectConfig>,
globalConfig: Config.GlobalConfig,
outputStream: NodeJS.WriteStream,
outputStream: WriteStream,
) => {
const hasteMapInstances = Array(configs.length);
const contexts = await Promise.all(
Expand Down Expand Up @@ -107,7 +108,7 @@ export const _run = async (
globalConfig: Config.GlobalConfig,
configs: Array<Config.ProjectConfig>,
hasDeprecationWarnings: boolean,
outputStream: NodeJS.WriteStream,
outputStream: WriteStream,
): Promise<AggregatedResult> => {
// Queries to hg/git can take a while, so we need to start the process
// as soon as possible, so by the time we need the result it's already there.
Expand Down Expand Up @@ -193,7 +194,7 @@ const runWatch = async (
_configs: Array<Config.ProjectConfig>,
hasDeprecationWarnings: boolean,
globalConfig: Config.GlobalConfig,
outputStream: NodeJS.WriteStream,
outputStream: WriteStream,
hasteMapInstances: Array<IHasteMap>,
filter?: Filter,
) => {
Expand Down Expand Up @@ -228,7 +229,7 @@ const runWatch = async (
const runWithoutWatch = async (
globalConfig: Config.GlobalConfig,
contexts: Array<TestContext>,
outputStream: NodeJS.WriteStream,
outputStream: WriteStream,
onComplete: OnCompleteCallback,
changedFilesPromise?: ChangedFilesPromise,
filter?: Filter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
*/

import type {WriteStream} from 'tty';
import {makeGlobalConfig, makeProjectConfig} from '@jest/test-utils';
import logDebugMessages from '../logDebugMessages';

Expand All @@ -18,7 +19,7 @@ const getOutputStream = (resolve: (message: string) => void) =>
write(message: string) {
resolve(message);
},
}) as NodeJS.WriteStream;
}) as WriteStream;

it('prints the jest version', async () => {
expect.assertions(1);
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-core/src/lib/handleDeprecationWarnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/

import type {ReadStream, WriteStream} from 'tty';
import chalk = require('chalk');
import {KEYS} from 'jest-watcher';

export default function handleDeprecationWarnings(
pipe: NodeJS.WriteStream,
stdin: NodeJS.ReadStream = process.stdin,
pipe: WriteStream,
stdin: ReadStream = process.stdin,
): Promise<void> {
return new Promise((resolve, reject) => {
if (typeof stdin.setRawMode === 'function') {
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-core/src/lib/logDebugMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
* LICENSE file in the root directory of this source tree.
*/

import type {WriteStream} from 'tty';
import type {Config} from '@jest/types';
const VERSION = require('../../package.json').version;

// if the output here changes, update `getConfig` in e2e/runJest.ts
export default function logDebugMessages(
globalConfig: Config.GlobalConfig,
configs: Array<Config.ProjectConfig> | Config.ProjectConfig,
outputStream: NodeJS.WriteStream,
outputStream: WriteStream,
): void {
const output = {
configs,
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-core/src/plugins/Quit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/

import type {ReadStream, WriteStream} from 'tty';
import {BaseWatchPlugin, UsageData} from 'jest-watcher';

class QuitPlugin extends BaseWatchPlugin {
isInternal: true;

constructor(options: {stdin: NodeJS.ReadStream; stdout: NodeJS.WriteStream}) {
constructor(options: {stdin: ReadStream; stdout: WriteStream}) {
super(options);
this.isInternal = true;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-core/src/plugins/TestNamePattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import type {ReadStream, WriteStream} from 'tty';
import type {Config} from '@jest/types';
import {
BaseWatchPlugin,
Expand All @@ -19,7 +20,7 @@ class TestNamePatternPlugin extends BaseWatchPlugin {
_prompt: Prompt;
isInternal: true;

constructor(options: {stdin: NodeJS.ReadStream; stdout: NodeJS.WriteStream}) {
constructor(options: {stdin: ReadStream; stdout: WriteStream}) {
super(options);
this._prompt = new Prompt();
this.isInternal = true;
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-core/src/plugins/TestPathPattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import type {ReadStream, WriteStream} from 'tty';
import type {Config} from '@jest/types';
import {
BaseWatchPlugin,
Expand All @@ -19,7 +20,7 @@ class TestPathPatternPlugin extends BaseWatchPlugin {
private readonly _prompt: Prompt;
isInternal: true;

constructor(options: {stdin: NodeJS.ReadStream; stdout: NodeJS.WriteStream}) {
constructor(options: {stdin: ReadStream; stdout: WriteStream}) {
super(options);
this._prompt = new Prompt();
this.isInternal = true;
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-core/src/plugins/UpdateSnapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import type {ReadStream, WriteStream} from 'tty';
import type {Config} from '@jest/types';
import {
BaseWatchPlugin,
Expand All @@ -17,7 +18,7 @@ class UpdateSnapshotsPlugin extends BaseWatchPlugin {
private _hasSnapshotFailure: boolean;
isInternal: true;

constructor(options: {stdin: NodeJS.ReadStream; stdout: NodeJS.WriteStream}) {
constructor(options: {stdin: ReadStream; stdout: WriteStream}) {
super(options);
this.isInternal = true;
this._hasSnapshotFailure = false;
Expand Down
7 changes: 4 additions & 3 deletions packages/jest-core/src/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import * as path from 'path';
import {performance} from 'perf_hooks';
import type {WriteStream} from 'tty';
import chalk = require('chalk');
import exit = require('exit');
import * as fs from 'graceful-fs';
Expand Down Expand Up @@ -36,7 +37,7 @@ import type {Filter, TestRunData} from './types';
const getTestPaths = async (
globalConfig: Config.GlobalConfig,
source: SearchSource,
outputStream: NodeJS.WriteStream,
outputStream: WriteStream,
changedFiles: ChangedFiles | undefined,
jestHooks: JestHookEmitter,
filter?: Filter,
Expand Down Expand Up @@ -74,7 +75,7 @@ type ProcessResultOptions = Pick<
> & {
collectHandles?: HandleCollectionResult;
onComplete?: (result: AggregatedResult) => void;
outputStream: NodeJS.WriteStream;
outputStream: WriteStream;
};

const processResults = async (
Expand Down Expand Up @@ -142,7 +143,7 @@ export default async function runJest({
}: {
globalConfig: Config.GlobalConfig;
contexts: Array<TestContext>;
outputStream: NodeJS.WriteStream;
outputStream: WriteStream;
testWatcher: TestWatcher;
jestHooks?: JestHookEmitter;
startRun: (globalConfig: Config.GlobalConfig) => void;
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-core/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import * as path from 'path';
import type {WriteStream} from 'tty';
import ansiEscapes = require('ansi-escapes');
import chalk = require('chalk');
import exit = require('exit');
Expand Down Expand Up @@ -90,7 +91,7 @@ const RESERVED_KEY_PLUGINS = new Map<
export default async function watch(
initialGlobalConfig: Config.GlobalConfig,
contexts: Array<TestContext>,
outputStream: NodeJS.WriteStream,
outputStream: WriteStream,
hasteMapInstances: Array<HasteMap>,
stdin: NodeJS.ReadStream = process.stdin,
hooks: JestHook = new JestHook(),
Expand Down
7 changes: 3 additions & 4 deletions packages/jest-reporters/src/DefaultReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import type {WriteStream} from 'tty';
import chalk = require('chalk');
import {getConsoleOutput} from '@jest/console';
import type {
Expand All @@ -26,7 +27,7 @@ import getResultHeader from './getResultHeader';
import getSnapshotStatus from './getSnapshotStatus';
import type {ReporterOnStartOptions} from './types';

type write = NodeJS.WriteStream['write'];
type write = WriteStream['write'];
type FlushBufferedOutput = () => void;

const TITLE_BULLET = chalk.bold('\u25cf ');
Expand Down Expand Up @@ -57,9 +58,7 @@ export default class DefaultReporter extends BaseReporter {
});
}

protected __wrapStdio(
stream: NodeJS.WritableStream | NodeJS.WriteStream,
): void {
protected __wrapStdio(stream: NodeJS.WritableStream | WriteStream): void {
const write = stream.write.bind(stream);

let buffer: Array<string> = [];
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-reporters/src/VerboseReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import type {WriteStream} from 'tty';
import chalk = require('chalk');
import type {
AggregatedResult,
Expand Down Expand Up @@ -32,7 +33,7 @@ export default class VerboseReporter extends DefaultReporter {
// Verbose mode is for debugging. Buffering of output is undesirable.
// See https://github.com/jestjs/jest/issues/8208
protected override __wrapStdio(
stream: NodeJS.WritableStream | NodeJS.WriteStream,
stream: NodeJS.WritableStream | WriteStream,
): void {
const write = stream.write.bind(stream);

Expand Down
4 changes: 3 additions & 1 deletion packages/jest-util/src/clearLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/

export default function clearLine(stream: NodeJS.WriteStream): void {
import type {WriteStream} from 'tty';

export default function clearLine(stream: WriteStream): void {
if (stream.isTTY) {
stream.write('\x1b[999D\x1b[K');
}
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-util/src/preRunMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
* LICENSE file in the root directory of this source tree.
*/

import type {WriteStream} from 'tty';
import chalk = require('chalk');
import clearLine from './clearLine';
import isInteractive from './isInteractive';

export function print(stream: NodeJS.WriteStream): void {
export function print(stream: WriteStream): void {
if (isInteractive) {
stream.write(chalk.bold.dim('Determining test suites to run...'));
}
}

export function remove(stream: NodeJS.WriteStream): void {
export function remove(stream: WriteStream): void {
if (isInteractive) {
clearLine(stream);
}
Expand Down
15 changes: 5 additions & 10 deletions packages/jest-watcher/src/BaseWatchPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import type {ReadStream, WriteStream} from 'tty';
import type {Config} from '@jest/types';
import type {
JestHookSubscriber,
Expand All @@ -14,16 +15,10 @@ import type {
} from './types';

abstract class BaseWatchPlugin implements WatchPlugin {
protected _stdin: NodeJS.ReadStream;
protected _stdout: NodeJS.WriteStream;

constructor({
stdin,
stdout,
}: {
stdin: NodeJS.ReadStream;
stdout: NodeJS.WriteStream;
}) {
protected _stdin: ReadStream;
protected _stdout: WriteStream;

constructor({stdin, stdout}: {stdin: ReadStream; stdout: WriteStream}) {
this._stdin = stdin;
this._stdout = stdout;
}
Expand Down
Loading

0 comments on commit 6877134

Please sign in to comment.