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

chore: remove ansi-escapes #13471

Closed
wants to merge 3 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

### Chore & Maintenance

- `[*]` Remove `ansi-escapes` dependency [#13471](https://github.com/facebook/jest/pull/13471)

### Performance

- `[*]` Use sha1 instead of sha256 for hashing [#13421](https://github.com/facebook/jest/pull/13421)
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@
},
"resolutions": {
"@types/node": "~14.14.45",
"ansi-escapes/type-fest": "^2.0.0",
"babel-jest": "workspace:^",
"enzyme/cheerio": "=1.0.0-rc.3",
"jest": "workspace:^",
Expand Down
1 change: 0 additions & 1 deletion packages/jest-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"@jest/transform": "workspace:^",
"@jest/types": "workspace:^",
"@types/node": "*",
"ansi-escapes": "^4.2.1",
"chalk": "^4.0.0",
"ci-info": "^3.2.0",
"exit": "^0.1.2",
Expand Down
11 changes: 5 additions & 6 deletions packages/jest-core/src/FailedTestsInteractiveMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
* LICENSE file in the root directory of this source tree.
*/

import ansiEscapes = require('ansi-escapes');
import chalk = require('chalk');
import type {AggregatedResult, AssertionLocation} from '@jest/test-result';
import {pluralize, specialChars} from 'jest-util';
import {ansiEscapes, pluralize, specialChars} from 'jest-util';
import {KEYS} from 'jest-watcher';

type RunnerUpdateFunction = (failure?: AssertionLocation) => void;

const {ARROW, CLEAR} = specialChars;
const {ARROW} = specialChars;

function describeKey(key: string, description: string) {
return `${chalk.dim(`${ARROW}Press`)} ${key} ${chalk.dim(description)}`;
Expand Down Expand Up @@ -98,11 +97,11 @@ export default class FailedTestsInteractiveMode {

private _clearTestSummary() {
this._pipe.write(ansiEscapes.cursorUp(6));
this._pipe.write(ansiEscapes.eraseDown);
this._pipe.write(ansiEscapes.eraseScreenDown);
}

private _drawUIDone() {
this._pipe.write(CLEAR);
this._pipe.write(ansiEscapes.clearTerminal);

const messages: Array<string> = [
chalk.bold('Watch Usage'),
Expand All @@ -113,7 +112,7 @@ export default class FailedTestsInteractiveMode {
}

private _drawUIDoneWithSkipped() {
this._pipe.write(CLEAR);
this._pipe.write(ansiEscapes.clearTerminal);

let stats = `${pluralize('test', this._countPaths)} reviewed`;

Expand Down
11 changes: 5 additions & 6 deletions packages/jest-core/src/SnapshotInteractiveMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
* LICENSE file in the root directory of this source tree.
*/

import ansiEscapes = require('ansi-escapes');
import chalk = require('chalk');
import type {AggregatedResult, AssertionLocation} from '@jest/test-result';
import {pluralize, specialChars} from 'jest-util';
import {ansiEscapes, pluralize, specialChars} from 'jest-util';
import {KEYS} from 'jest-watcher';

const {ARROW, CLEAR} = specialChars;
const {ARROW} = specialChars;

export default class SnapshotInteractiveMode {
private readonly _pipe: NodeJS.WritableStream;
Expand Down Expand Up @@ -40,7 +39,7 @@ export default class SnapshotInteractiveMode {

private _clearTestSummary() {
this._pipe.write(ansiEscapes.cursorUp(6));
this._pipe.write(ansiEscapes.eraseDown);
this._pipe.write(ansiEscapes.eraseScreenDown);
}

private _drawUIProgress() {
Expand Down Expand Up @@ -87,7 +86,7 @@ export default class SnapshotInteractiveMode {
}

private _drawUIDoneWithSkipped() {
this._pipe.write(CLEAR);
this._pipe.write(ansiEscapes.clearTerminal);
const numPass = this._countPaths - this._testAssertions.length;

let stats = chalk.bold.dim(
Expand Down Expand Up @@ -121,7 +120,7 @@ export default class SnapshotInteractiveMode {
}

private _drawUIDone() {
this._pipe.write(CLEAR);
this._pipe.write(ansiEscapes.clearTerminal);
const numPass = this._countPaths - this._testAssertions.length;

let stats = chalk.bold.dim(
Expand Down
15 changes: 7 additions & 8 deletions packages/jest-core/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import * as path from 'path';
import ansiEscapes = require('ansi-escapes');
import chalk = require('chalk');
import exit = require('exit');
import slash = require('slash');
Expand All @@ -15,10 +14,10 @@ import type {Config} from '@jest/types';
import type {IHasteMap as HasteMap} from 'jest-haste-map';
import {formatExecError} from 'jest-message-util';
import {
ansiEscapes,
isInteractive,
preRunMessage,
requireOrImportModule,
specialChars,
} from 'jest-util';
import {ValidationError} from 'jest-validate';
import {
Expand Down Expand Up @@ -267,7 +266,7 @@ export default async function watch(
process.on('exit', () => {
if (activePlugin) {
outputStream.write(ansiEscapes.cursorDown());
outputStream.write(ansiEscapes.eraseDown);
outputStream.write(ansiEscapes.eraseScreenDown);
}
});
}
Expand All @@ -280,7 +279,7 @@ export default async function watch(
}

testWatcher = new TestWatcher({isWatchMode: true});
isInteractive && outputStream.write(specialChars.CLEAR);
isInteractive && outputStream.write(ansiEscapes.clearTerminal);
preRunMessagePrint(outputStream);
isRunning = true;
const configs = contexts.map(context => context.config);
Expand Down Expand Up @@ -434,7 +433,7 @@ export default async function watch(
case 'w':
if (!shouldDisplayWatchUsage && !isWatchUsageDisplayed) {
outputStream.write(ansiEscapes.cursorUp());
outputStream.write(ansiEscapes.eraseDown);
outputStream.write(ansiEscapes.eraseScreenDown);
outputStream.write(usage(globalConfig, watchPlugins));
isWatchUsageDisplayed = true;
shouldDisplayWatchUsage = false;
Expand All @@ -444,10 +443,10 @@ export default async function watch(
};

const onCancelPatternPrompt = () => {
outputStream.write(ansiEscapes.cursorHide);
outputStream.write(specialChars.CLEAR);
outputStream.write(ansiEscapes.hideCursor);
outputStream.write(ansiEscapes.clearTerminal);
outputStream.write(usage(globalConfig, watchPlugins));
outputStream.write(ansiEscapes.cursorShow);
outputStream.write(ansiEscapes.showCursor);
};

if (typeof stdin.setRawMode === 'function') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ansiEscapes allows passing an argument to cursorDown function 1`] = `
"<moveCursorDownBy2Rows>
"
`;

exports[`ansiEscapes allows passing an argument to cursorUp function 1`] = `
"<moveCursorUpBy2Rows>
"
`;

exports[`ansiEscapes allows passing arguments to cursorTo function 1`] = `
"<moveCursorToRow3Column12>
"
`;

exports[`ansiEscapes clearTerminal sequence 1`] = `
"<clearTerminal>
"
`;

exports[`ansiEscapes cursorToFirstColumn sequence 1`] = `
"<moveCursorToColumn1>
"
`;

exports[`ansiEscapes eraseLine sequence 1`] = `
"<eraseLine>
"
`;

exports[`ansiEscapes eraseScreenDown sequence 1`] = `
"<eraseScreenDown>
"
`;

exports[`ansiEscapes hideCursor sequence 1`] = `
"<hideCursor>
"
`;

exports[`ansiEscapes restoreCursorPosition sequence 1`] = `
"<restoreCursorPosition>
"
`;

exports[`ansiEscapes restoreCursorPosition sequence on Terminal App 1`] = `
"<restoreCursorPosition>
"
`;

exports[`ansiEscapes returns cursorDown sequence 1`] = `
"<moveCursorDownBy1Row>
"
`;

exports[`ansiEscapes returns cursorUp sequence 1`] = `
"<moveCursorUpBy1Row>
"
`;

exports[`ansiEscapes saveCursorPosition sequence 1`] = `
"<saveCursorPosition>
"
`;

exports[`ansiEscapes saveCursorPosition sequence on Terminal App 1`] = `
"<saveCursorPosition>
"
`;

exports[`ansiEscapes showCursor sequence 1`] = `
"<showCursor>
"
`;
80 changes: 80 additions & 0 deletions packages/jest-util/src/__tests__/ansiEscapes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* 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 * as ansiEscapes from '../ansiEscapes';

const oldTermProgram = process.env.TERM_PROGRAM;

afterEach(() => {
process.env.TERM_PROGRAM = oldTermProgram;
});

describe('ansiEscapes', () => {
test('clearTerminal sequence', () => {
expect(ansiEscapes.clearTerminal).toMatchSnapshot();
});

test('returns cursorUp sequence', () => {
expect(ansiEscapes.cursorUp()).toMatchSnapshot();
});

test('allows passing an argument to cursorUp function', () => {
expect(ansiEscapes.cursorUp(2)).toMatchSnapshot();
});

test('returns cursorDown sequence', () => {
expect(ansiEscapes.cursorDown()).toMatchSnapshot();
});

test('allows passing an argument to cursorDown function', () => {
expect(ansiEscapes.cursorDown(2)).toMatchSnapshot();
});

test('cursorToFirstColumn sequence', () => {
expect(ansiEscapes.cursorToFirstColumn).toMatchSnapshot();
});

test('allows passing arguments to cursorTo function', () => {
expect(ansiEscapes.cursorTo(3, 12)).toMatchSnapshot();
});

test('eraseScreenDown sequence', () => {
expect(ansiEscapes.eraseScreenDown).toMatchSnapshot();
});

test('eraseLine sequence', () => {
expect(ansiEscapes.eraseLine).toMatchSnapshot();
});

test('saveCursorPosition sequence', () => {
expect(ansiEscapes.saveCursorPosition).toMatchSnapshot();
});

test('saveCursorPosition sequence on Terminal App', () => {
process.env.TERM_PROGRAM = 'Apple_Terminal';

expect(ansiEscapes.saveCursorPosition).toMatchSnapshot();
});

test('restoreCursorPosition sequence', () => {
expect(ansiEscapes.restoreCursorPosition).toMatchSnapshot();
});

test('restoreCursorPosition sequence on Terminal App', () => {
process.env.TERM_PROGRAM = 'Apple_Terminal';

expect(ansiEscapes.restoreCursorPosition).toMatchSnapshot();
});

test('hideCursor sequence', () => {
expect(ansiEscapes.hideCursor).toMatchSnapshot();
});

test('showCursor sequence', () => {
expect(ansiEscapes.showCursor).toMatchSnapshot();
});
});
33 changes: 33 additions & 0 deletions packages/jest-util/src/ansiEscapes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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.
*/

// Reference: https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_(Control_Sequence_Introducer)_sequences

const isTerminalApp = process.env.TERM_PROGRAM === 'Apple_Terminal';
const isWindows = process.platform === 'win32';

export const clearTerminal = isWindows
? '\x1b[2J\x1b[0f'
: '\x1b[2J\x1b[3J\x1b[H';

export const cursorUp = (rows = 1): string => `\x1b[${rows}A`;
export const cursorDown = (rows = 1): string => `\x1b[${rows}B`;

export const cursorToFirstColumn = '\x1b[G';

export const cursorTo = (row: number, column: number): string =>
`\x1b[${row};${column}H`;

export const eraseScreenDown = '\x1b[J';

export const eraseLine = '\x1b[2K';

export const saveCursorPosition = isTerminalApp ? '\x1b7' : '\x1b[s';
export const restoreCursorPosition = isTerminalApp ? '\x1b8' : '\x1b[u';

export const hideCursor = '\x1b[?25l';
export const showCursor = '\x1b[?25h';
2 changes: 2 additions & 0 deletions packages/jest-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
*/

// need to do this for api-extractor: https://github.com/microsoft/rushstack/issues/2780
import * as ansiEscapes from './ansiEscapes';
import * as preRunMessage from './preRunMessage';
import * as specialChars from './specialChars';

export {ansiEscapes};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more idea. Additionally jest-watcher could also expose ansiEscapes so that plugins could use this implementation as well.

export {default as clearLine} from './clearLine';
export {default as createDirectory} from './createDirectory';
export {default as ErrorWithStack} from './ErrorWithStack';
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-util/src/specialChars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

import {clearTerminal} from './ansiEscapes';

const isWindows = process.platform === 'win32';

export const ARROW = ' \u203A ';
Expand All @@ -15,4 +17,5 @@ export const ICONS = {
todo: '\u270E',
};

export const CLEAR = isWindows ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H';
// TODO Remove reexport in Jest 30
export const CLEAR = clearTerminal;
1 change: 0 additions & 1 deletion packages/jest-watcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"@jest/test-result": "workspace:^",
"@jest/types": "workspace:^",
"@types/node": "*",
"ansi-escapes": "^4.2.1",
"chalk": "^4.0.0",
"emittery": "^0.10.2",
"jest-util": "workspace:^",
Expand Down
Loading