Skip to content

Commit 614f739

Browse files
mattphillipscpojer
authored andcommitted
Replace hex with utf8 (#6253)
* Replace hex with utf8 * Add windows backspace check in constants * Fix flow types * Add changelog entry
1 parent aac32f9 commit 614f739

16 files changed

+61
-69
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@
109109

110110
### Fixes
111111

112+
* `[jest-cli]` Fix stdin encoding to utf8 for watch plugins.
113+
([#6253](https://github.com/facebook/jest/issues/6253))
112114
* `[expect]` Better detection of DOM Nodes for equality
113115
([#6246](https://github.com/facebook/jest/pull/6246))
114116
* `[jest-cli]` Fix misleading action description for F key when in "only failed

packages/jest-cli/src/__tests__/watch.test.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ jest.doMock(
6565
class WatchPlugin1 {
6666
getUsageInfo() {
6767
return {
68-
key: 's'.codePointAt(0),
68+
key: 's',
6969
prompt: 'do nothing',
7070
};
7171
}
@@ -79,7 +79,7 @@ jest.doMock(
7979
class WatchPlugin2 {
8080
getUsageInfo() {
8181
return {
82-
key: 'u'.codePointAt(0),
82+
key: 'u',
8383
prompt: 'do something else',
8484
};
8585
}
@@ -90,7 +90,6 @@ jest.doMock(
9090
const watch = require('../watch').default;
9191

9292
const nextTick = () => new Promise(res => process.nextTick(res));
93-
const toHex = char => Number(char.charCodeAt(0)).toString(16);
9493

9594
afterEach(runJestMock.mockReset);
9695

@@ -329,7 +328,7 @@ describe('Watch mode flows', () => {
329328
}
330329
getUsageInfo() {
331330
return {
332-
key: 'p'.codePointAt(0),
331+
key: 'p',
333332
prompt: 'custom "P" plugin',
334333
};
335334
}
@@ -352,7 +351,7 @@ describe('Watch mode flows', () => {
352351

353352
expect(pipe.write.mock.calls.reverse()[0]).toMatchSnapshot();
354353

355-
stdin.emit(toHex('p'));
354+
stdin.emit('p');
356355
await nextTick();
357356

358357
expect(run).toHaveBeenCalled();
@@ -405,7 +404,7 @@ describe('Watch mode flows', () => {
405404
}
406405
getUsageInfo() {
407406
return {
408-
key: 's'.codePointAt(0),
407+
key: 's',
409408
prompt: 'do nothing',
410409
};
411410
}
@@ -424,7 +423,7 @@ describe('Watch mode flows', () => {
424423
stdin,
425424
);
426425

427-
stdin.emit(Number('s'.charCodeAt(0)).toString(16));
426+
stdin.emit('s');
428427

429428
await nextTick();
430429

@@ -445,7 +444,7 @@ describe('Watch mode flows', () => {
445444
onKey() {}
446445
getUsageInfo() {
447446
return {
448-
key: 's'.codePointAt(0),
447+
key: 's',
449448
prompt: 'do nothing',
450449
};
451450
}
@@ -465,7 +464,7 @@ describe('Watch mode flows', () => {
465464
onKey() {}
466465
getUsageInfo() {
467466
return {
468-
key: 'z'.codePointAt(0),
467+
key: 'z',
469468
prompt: 'also do nothing',
470469
};
471470
}
@@ -484,14 +483,14 @@ describe('Watch mode flows', () => {
484483
stdin,
485484
);
486485

487-
stdin.emit(Number('s'.charCodeAt(0)).toString(16));
486+
stdin.emit('s');
488487
await nextTick();
489488
expect(run).toHaveBeenCalled();
490-
stdin.emit(Number('z'.charCodeAt(0)).toString(16));
489+
stdin.emit('z');
491490
await nextTick();
492491
expect(showPrompt2).not.toHaveBeenCalled();
493492
await resolveShowPrompt();
494-
stdin.emit(Number('z'.charCodeAt(0)).toString(16));
493+
stdin.emit('z');
495494
expect(showPrompt2).toHaveBeenCalled();
496495
});
497496

@@ -537,7 +536,7 @@ describe('Watch mode flows', () => {
537536
runJestMock.mockReset();
538537

539538
stdin.emit(KEYS.T);
540-
['t', 'e', 's', 't'].map(toHex).forEach(key => stdin.emit(key));
539+
['t', 'e', 's', 't'].forEach(key => stdin.emit(key));
541540
stdin.emit(KEYS.ENTER);
542541
await nextTick();
543542

@@ -556,7 +555,7 @@ describe('Watch mode flows', () => {
556555
runJestMock.mockReset();
557556

558557
stdin.emit(KEYS.P);
559-
['f', 'i', 'l', 'e'].map(toHex).forEach(key => stdin.emit(key));
558+
['f', 'i', 'l', 'e'].forEach(key => stdin.emit(key));
560559
stdin.emit(KEYS.ENTER);
561560
await nextTick();
562561

@@ -575,12 +574,12 @@ describe('Watch mode flows', () => {
575574
runJestMock.mockReset();
576575

577576
stdin.emit(KEYS.P);
578-
['f', 'i', 'l', 'e'].map(toHex).forEach(key => stdin.emit(key));
577+
['f', 'i', 'l', 'e'].forEach(key => stdin.emit(key));
579578
stdin.emit(KEYS.ENTER);
580579
await nextTick();
581580

582581
stdin.emit(KEYS.T);
583-
['t', 'e', 's', 't'].map(toHex).forEach(key => stdin.emit(key));
582+
['t', 'e', 's', 't'].forEach(key => stdin.emit(key));
584583
stdin.emit(KEYS.ENTER);
585584
await nextTick();
586585

packages/jest-cli/src/__tests__/watch_filename_pattern_mode.test.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ const watch = require('../watch').default;
8787

8888
const nextTick = () => new Promise(res => process.nextTick(res));
8989

90-
const toHex = char => Number(char.charCodeAt(0)).toString(16);
91-
9290
const globalConfig = {watch: true};
9391

9492
afterEach(runJestMock.mockReset);
@@ -122,11 +120,11 @@ describe('Watch mode flows', () => {
122120
};
123121

124122
// Write a pattern
125-
['p', '.', '*', '1', '0'].map(toHex).forEach(assertPattern);
123+
['p', '.', '*', '1', '0'].forEach(assertPattern);
126124

127125
[KEYS.BACKSPACE, KEYS.BACKSPACE].forEach(assertPattern);
128126

129-
['3'].map(toHex).forEach(assertPattern);
127+
['3'].forEach(assertPattern);
130128

131129
// Runs Jest again
132130
runJestMock.mockReset();
@@ -152,17 +150,14 @@ describe('Watch mode flows', () => {
152150
await nextTick();
153151

154152
['p', '.', '*', '1', '0']
155-
.map(toHex)
153+
156154
.concat(KEYS.ENTER)
157155
.forEach(key => stdin.emit(key));
158156

159157
stdin.emit(KEYS.T);
160158
await nextTick();
161159

162-
['t', 'e', 's', 't']
163-
.map(toHex)
164-
.concat(KEYS.ENTER)
165-
.forEach(key => stdin.emit(key));
160+
['t', 'e', 's', 't'].concat(KEYS.ENTER).forEach(key => stdin.emit(key));
166161

167162
await nextTick();
168163

packages/jest-cli/src/__tests__/watch_test_name_pattern_mode.test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ jest.doMock('../lib/terminal_utils', () => ({
9999

100100
const watch = require('../watch').default;
101101

102-
const toHex = char => Number(char.charCodeAt(0)).toString(16);
103-
104102
const globalConfig = {
105103
watch: true,
106104
};
@@ -136,11 +134,11 @@ describe('Watch mode flows', () => {
136134
};
137135

138136
// Write a pattern
139-
['c', 'o', 'n', ' ', '1', '2'].map(toHex).forEach(assertPattern);
137+
['c', 'o', 'n', ' ', '1', '2'].forEach(assertPattern);
140138

141139
[KEYS.BACKSPACE, KEYS.BACKSPACE].forEach(assertPattern);
142140

143-
['*'].map(toHex).forEach(assertPattern);
141+
['*'].forEach(assertPattern);
144142

145143
// Runs Jest again
146144
runJestMock.mockReset();

packages/jest-cli/src/constants.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,30 @@ const isWindows = process.platform === 'win32';
1212
export const CLEAR = isWindows ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H';
1313
export const ARROW = ' \u203A ';
1414
export const KEYS = {
15-
A: '61',
16-
ARROW_DOWN: '1b5b42',
17-
ARROW_LEFT: '1b5b44',
18-
ARROW_RIGHT: '1b5b43',
19-
ARROW_UP: '1b5b41',
20-
BACKSPACE: isWindows ? '08' : '7f',
21-
C: '63',
22-
CONTROL_C: '03',
23-
CONTROL_D: '04',
24-
ENTER: '0d',
25-
ESCAPE: '1b',
26-
F: '66',
27-
I: '69',
28-
O: '6f',
29-
P: '70',
30-
Q: '71',
31-
QUESTION_MARK: '3f',
32-
R: '72',
33-
S: '73',
34-
T: '74',
35-
U: '75',
36-
W: '77',
15+
A: 'a',
16+
ARROW_DOWN: '\u001b[B',
17+
ARROW_LEFT: '\u001b[D',
18+
ARROW_RIGHT: '\u001b[C',
19+
ARROW_UP: '\u001b[A',
20+
BACKSPACE: isWindows
21+
? Buffer.from('08', 'hex').toString()
22+
: Buffer.from('7f', 'hex').toString(),
23+
C: 'c',
24+
CONTROL_C: '\u0003',
25+
CONTROL_D: '\u0004',
26+
ENTER: '\r',
27+
ESCAPE: '\u001b',
28+
F: 'f',
29+
I: 'i',
30+
O: 'o',
31+
P: 'p',
32+
Q: 'q',
33+
QUESTION_MARK: '?',
34+
R: 'r',
35+
S: 's',
36+
T: 't',
37+
U: 'u',
38+
W: 'w',
3739
};
3840

3941
export const ICONS = {

packages/jest-cli/src/lib/Prompt.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,8 @@ export default class Prompt {
8484
case KEYS.ARROW_RIGHT:
8585
break;
8686
default:
87-
const char = new Buffer(key, 'hex').toString();
88-
8987
this._value =
90-
key === KEYS.BACKSPACE
91-
? this._value.slice(0, -1)
92-
: this._value + char;
88+
key === KEYS.BACKSPACE ? this._value.slice(0, -1) : this._value + key;
9389
this._offset = -1;
9490
this._selection = null;
9591
this._onChange();

packages/jest-cli/src/lib/__tests__/prompt.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import Prompt from '../Prompt';
1212
import {KEYS} from '../../constants';
1313

1414
const EXTRA_KEYS = Object.assign({}, KEYS, {
15-
E: '65',
16-
S: '73',
15+
E: 'e',
16+
S: 's',
1717
});
1818

1919
it('calls handler on change value', () => {

packages/jest-cli/src/lib/handle_deprecation_warnings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default (
2828
// $FlowFixMe
2929
stdin.setRawMode(true);
3030
stdin.resume();
31-
stdin.setEncoding('hex');
31+
stdin.setEncoding('utf8');
3232
stdin.on('data', (key: string) => {
3333
if (key === KEYS.ENTER) {
3434
resolve();

packages/jest-cli/src/lib/watch_plugins_helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const getSortedUsageRows = (
4141
const usageInfoB = b.getUsageInfo && b.getUsageInfo(globalConfig);
4242

4343
if (usageInfoA && usageInfoB) {
44-
return usageInfoA.key - usageInfoB.key;
44+
return usageInfoA.key.localeCompare(usageInfoB.key);
4545
}
4646

4747
return 0;

packages/jest-cli/src/plugins/quit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class QuitPlugin extends BaseWatchPlugin {
2929

3030
getUsageInfo() {
3131
return {
32-
key: 'q'.codePointAt(0),
32+
key: 'q',
3333
prompt: 'quit watch mode',
3434
};
3535
}

packages/jest-cli/src/plugins/test_name_pattern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class TestNamePatternPlugin extends BaseWatchPlugin {
2727

2828
getUsageInfo() {
2929
return {
30-
key: 't'.codePointAt(0),
30+
key: 't',
3131
prompt: 'filter by a test name regex pattern',
3232
};
3333
}

packages/jest-cli/src/plugins/test_path_pattern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class TestPathPatternPlugin extends BaseWatchPlugin {
2828

2929
getUsageInfo() {
3030
return {
31-
key: 'p'.codePointAt(0),
31+
key: 'p',
3232
prompt: 'filter by a filename regex pattern',
3333
};
3434
}

packages/jest-cli/src/plugins/update_snapshots.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class UpdateSnapshotsPlugin extends BaseWatchPlugin {
3939
getUsageInfo(globalConfig: GlobalConfig) {
4040
if (this._hasSnapshotFailure) {
4141
return {
42-
key: 'u'.codePointAt(0),
42+
key: 'u',
4343
prompt: 'update failing snapshots',
4444
};
4545
}

packages/jest-cli/src/plugins/update_snapshots_interactive.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class UpdateSnapshotInteractivePlugin extends BaseWatchPlugin {
9999
this._failedSnapshotTestAssertions.length > 0
100100
) {
101101
return {
102-
key: 'i'.codePointAt(0),
102+
key: 'i',
103103
prompt: 'update failing snapshots interactively',
104104
};
105105
}

packages/jest-cli/src/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {GlobalConfig} from 'types/Config';
1010
import type {JestHookSubscriber} from './jest_hooks';
1111

1212
export type UsageData = {
13-
key: number,
13+
key: string,
1414
prompt: string,
1515
};
1616

packages/jest-cli/src/watch.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ export default function watch(
277277
).find(plugin => {
278278
const usageData =
279279
(plugin.getUsageInfo && plugin.getUsageInfo(globalConfig)) || {};
280-
return usageData.key === parseInt(key, 16);
280+
return usageData.key === key;
281281
});
282282

283283
if (matchingWatchPlugin != null) {
@@ -359,7 +359,7 @@ export default function watch(
359359
if (typeof stdin.setRawMode === 'function') {
360360
stdin.setRawMode(true);
361361
stdin.resume();
362-
stdin.setEncoding('hex');
362+
stdin.setEncoding('utf8');
363363
stdin.on('data', onKeypress);
364364
}
365365

@@ -405,7 +405,7 @@ const usage = (
405405
plugin =>
406406
chalk.dim(' \u203A Press') +
407407
' ' +
408-
String.fromCodePoint(plugin.key) +
408+
plugin.key +
409409
' ' +
410410
chalk.dim(`to ${plugin.prompt}.`),
411411
),

0 commit comments

Comments
 (0)