Skip to content

Commit

Permalink
avoid ANSI clearScreen
Browse files Browse the repository at this point in the history
clearScreen is quite aggressive and can e.g.
brick the user's shell theme.
Better use eraseScreen and cursorTo instead.
  • Loading branch information
jeysal committed Dec 17, 2018
1 parent 2dbd627 commit 6b6ed81
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
- `[jest-mock]` [**BREAKING**] Fix bugs with mock/spy result tracking of recursive functions ([#6381](https://github.com/facebook/jest/pull/6381))
- `[jest-haste-map]` [**BREAKING**] Recover files correctly after haste name collisions are fixed ([#7329](https://github.com/facebook/jest/pull/7329))
- `[pretty-format]` [**BREAKING**] Omit non-enumerable symbol properties ([#7448](https://github.com/facebook/jest/pull/7448))
- `[jest-cli]` Avoid watch mode causing bad terminal behavior in some cases (TODO)
- `[expect]` Standardize file naming in `expect` ([#7306](https://github.com/facebook/jest/pull/7306))
- `[jest-each]` Add empty array validation check ([#7249](https://github.com/facebook/jest/pull/7249))
- `[jest-cli]` Interrupt tests if interactive watch plugin key is pressed ([#7222](https://github.com/facebook/jest/pull/7222))
Expand Down
6 changes: 4 additions & 2 deletions packages/jest-cli/src/SnapshotInteractiveMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export default class SnapshotInteractiveMode {
}

_drawUIDoneWithSkipped() {
this._pipe.write(ansiEscapes.clearScreen);
this._pipe.write(ansiEscapes.eraseScreen);
this._pipe.write(ansiEscapes.cursorTo(0, 0));
const numPass = this._countPaths - this._testAssertions.length;

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

_drawUIDone() {
this._pipe.write(ansiEscapes.clearScreen);
this._pipe.write(ansiEscapes.eraseScreen);
this._pipe.write(ansiEscapes.cursorTo(0, 0));
const numPass = this._countPaths - this._testAssertions.length;

let stats = chalk.bold.dim(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import {KEYS} from 'jest-watcher';
import SnapshotInteractiveMode from '../SnapshotInteractiveMode';

jest.mock('ansi-escapes', () => ({
clearScreen: '[MOCK - eraseDown]',
cursorRestorePosition: '[MOCK - cursorRestorePosition]',
cursorSavePosition: '[MOCK - cursorSavePosition]',
cursorScrollDown: '[MOCK - cursorScrollDown]',
cursorTo: (x, y) => `[MOCK - cursorTo(${x}, ${y})]`,
cursorUp: () => '[MOCK - cursorUp]',
eraseDown: '[MOCK - eraseDown]',
eraseScreen: '[MOCK - eraseScreen]',
}));

jest.doMock('chalk', () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ exports[`SnapshotInteractiveMode skip 1 test, then quit 1`] = `
`;

exports[`SnapshotInteractiveMode skip 1 test, then quit 2`] = `
"[MOCK - eraseDown]
"[MOCK - eraseScreen]
[MOCK - cursorTo(0, 0)]

<bold>Interactive Snapshot Result</>
› <bold><dim>1 snapshot reviewed<bold></>, <bold><yellow>1 snapshot skipped</></>
Expand All @@ -43,7 +44,8 @@ exports[`SnapshotInteractiveMode skip 1 test, then restart 1`] = `
`;

exports[`SnapshotInteractiveMode skip 1 test, then restart 2`] = `
"[MOCK - eraseDown]
"[MOCK - eraseScreen]
[MOCK - cursorTo(0, 0)]

<bold>Interactive Snapshot Result</>
› <bold><dim>1 snapshot reviewed<bold></>, <bold><yellow>1 snapshot skipped</></>
Expand Down Expand Up @@ -100,7 +102,8 @@ exports[`SnapshotInteractiveMode skip 1 test, update 1 test, then finish and res
`;

exports[`SnapshotInteractiveMode skip 1 test, update 1 test, then finish and restart 3`] = `
"[MOCK - eraseDown]
"[MOCK - eraseScreen]
[MOCK - cursorTo(0, 0)]

<bold>Interactive Snapshot Result</>
› <bold><dim>2 snapshots reviewed<bold></>, <bold><green>1 snapshot updated</></>, <bold><yellow>1 snapshot skipped</></>
Expand Down Expand Up @@ -157,7 +160,8 @@ exports[`SnapshotInteractiveMode skip 2 tests, then finish and restart 2`] = `
`;

exports[`SnapshotInteractiveMode skip 2 tests, then finish and restart 3`] = `
"[MOCK - eraseDown]
"[MOCK - eraseScreen]
[MOCK - cursorTo(0, 0)]

<bold>Interactive Snapshot Result</>
› <bold><dim>2 snapshots reviewed<bold></>, <bold><yellow>2 snapshots skipped</></>
Expand Down Expand Up @@ -214,7 +218,8 @@ exports[`SnapshotInteractiveMode update 1 test, skip 1 test, then finish and res
`;

exports[`SnapshotInteractiveMode update 1 test, skip 1 test, then finish and restart 3`] = `
"[MOCK - eraseDown]
"[MOCK - eraseScreen]
[MOCK - cursorTo(0, 0)]

<bold>Interactive Snapshot Result</>
› <bold><dim>2 snapshots reviewed<bold></>, <bold><green>1 snapshot updated</></>, <bold><yellow>1 snapshot skipped</></>
Expand Down Expand Up @@ -256,7 +261,8 @@ exports[`SnapshotInteractiveMode update 1 test, then finish and return 1`] = `
`;

exports[`SnapshotInteractiveMode update 1 test, then finish and return 2`] = `
"[MOCK - eraseDown]
"[MOCK - eraseScreen]
[MOCK - cursorTo(0, 0)]

<bold>Interactive Snapshot Result</>
› <bold><dim>1 snapshot reviewed<bold></>, <bold><green>1 snapshot updated</></>
Expand Down Expand Up @@ -297,7 +303,8 @@ exports[`SnapshotInteractiveMode update 2 tests, then finish and return 2`] = `
`;

exports[`SnapshotInteractiveMode update 2 tests, then finish and return 3`] = `
"[MOCK - eraseDown]
"[MOCK - eraseScreen]
[MOCK - cursorTo(0, 0)]

<bold>Interactive Snapshot Result</>
› <bold><dim>2 snapshots reviewed<bold></>, <bold><green>2 snapshots updated</></>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ Object {

exports[`Watch mode flows Pressing "c" clears the filters 1`] = `
"[MOCK - cursorHide]
[MOCK - clearScreen]
[MOCK - eraseScreen]
[MOCK - cursorTo(0, 0)]
Pattern Mode Usage
› Press Esc to exit pattern mode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import {KEYS} from 'jest-watcher';
const runJestMock = jest.fn();

jest.mock('ansi-escapes', () => ({
clearScreen: '[MOCK - clearScreen]',
cursorDown: (count = 1) => `[MOCK - cursorDown(${count})]`,
cursorHide: '[MOCK - cursorHide]',
cursorRestorePosition: '[MOCK - cursorRestorePosition]',
cursorSavePosition: '[MOCK - cursorSavePosition]',
cursorShow: '[MOCK - cursorShow]',
cursorTo: (x, y) => `[MOCK - cursorTo(${x}, ${y})]`,
eraseScreen: '[MOCK - eraseScreen]',
}));

jest.mock(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import {KEYS} from 'jest-watcher';
const runJestMock = jest.fn();

jest.mock('ansi-escapes', () => ({
clearScreen: '[MOCK - clearScreen]',
cursorDown: (count = 1) => `[MOCK - cursorDown(${count})]`,
cursorHide: '[MOCK - cursorHide]',
cursorRestorePosition: '[MOCK - cursorRestorePosition]',
cursorSavePosition: '[MOCK - cursorSavePosition]',
cursorShow: '[MOCK - cursorShow]',
cursorTo: (x, y) => `[MOCK - cursorTo(${x}, ${y})]`,
eraseScreen: '[MOCK - eraseScreen]',
}));

jest.mock(
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-cli/src/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@ export default function watch(

const onCancelPatternPrompt = () => {
outputStream.write(ansiEscapes.cursorHide);
outputStream.write(ansiEscapes.clearScreen);
outputStream.write(ansiEscapes.eraseScreen);
outputStream.write(ansiEscapes.cursorTo(0, 0));
outputStream.write(usage(globalConfig, watchPlugins));
outputStream.write(ansiEscapes.cursorShow);
};
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-watcher/src/PatternPrompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export default class PatternPrompt {

run(onSuccess: Function, onCancel: Function, options?: {header: string}) {
this._pipe.write(ansiEscapes.cursorHide);
this._pipe.write(ansiEscapes.clearScreen);
this._pipe.write(ansiEscapes.eraseScreen);
this._pipe.write(ansiEscapes.cursorTo(0, 0));

if (options && options.header) {
this._pipe.write(options.header + '\n');
Expand Down

0 comments on commit 6b6ed81

Please sign in to comment.