forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwatch_filename_pattern_mode.test.js
More file actions
198 lines (157 loc) · 4.55 KB
/
watch_filename_pattern_mode.test.js
File metadata and controls
198 lines (157 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* Copyright (c) 2014-present, Facebook, Inc. 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.
*
*/
'use strict';
import chalk from 'chalk';
import {KEYS} from '../constants';
const runJestMock = jest.fn();
let terminalWidth;
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})]`,
}));
jest.mock(
'../search_source',
() =>
class {
constructor(context) {
this._context = context;
}
findMatchingTests(pattern) {
const paths = [
'./path/to/file1-test.js',
'./path/to/file2-test.js',
'./path/to/file3-test.js',
'./path/to/file4-test.js',
'./path/to/file5-test.js',
'./path/to/file6-test.js',
'./path/to/file7-test.js',
'./path/to/file8-test.js',
'./path/to/file9-test.js',
'./path/to/file10-test.js',
'./path/to/file11-test.js',
].filter(path => path.match(pattern));
return {
tests: paths.map(path => ({
context: this._context,
duration: null,
path,
})),
};
}
},
);
jest.doMock('chalk', () => new chalk.constructor({enabled: false}));
jest.doMock('strip-ansi');
require('strip-ansi').mockImplementation(str => str);
jest.doMock(
'../run_jest',
() =>
function() {
const args = Array.from(arguments);
const [{onComplete}] = args;
runJestMock.apply(null, args);
// Call the callback
onComplete({snapshot: {}});
return Promise.resolve();
},
);
jest.doMock('../lib/terminal_utils', () => ({
getTerminalWidth: () => terminalWidth,
}));
const nextTick = () => new Promise(res => process.nextTick(res));
const watch = require('../watch').default;
const toHex = char => Number(char.charCodeAt(0)).toString(16);
const globalConfig = {watch: true};
afterEach(runJestMock.mockReset);
describe('Watch mode flows', () => {
let pipe;
let hasteMapInstances;
let contexts;
let stdin;
beforeEach(() => {
terminalWidth = 80;
pipe = {write: jest.fn()};
hasteMapInstances = [{on: () => {}}];
contexts = [{config: {}}];
stdin = new MockStdin();
});
it('Pressing "P" enters pattern mode', async () => {
contexts[0].config = {rootDir: ''};
watch(globalConfig, contexts, pipe, hasteMapInstances, stdin);
// Write a enter pattern mode
stdin.emit(KEYS.P);
await nextTick();
expect(pipe.write).toBeCalledWith(' pattern › ');
const assertPattern = hex => {
pipe.write.mockReset();
stdin.emit(hex);
expect(pipe.write.mock.calls.join('\n')).toMatchSnapshot();
};
// Write a pattern
['p', '.', '*', '1', '0'].map(toHex).forEach(assertPattern);
[KEYS.BACKSPACE, KEYS.BACKSPACE].forEach(assertPattern);
['3'].map(toHex).forEach(assertPattern);
// Runs Jest again
runJestMock.mockReset();
stdin.emit(KEYS.ENTER);
await nextTick();
expect(runJestMock).toBeCalled();
// globalConfig is updated with the current pattern
expect(runJestMock.mock.calls[0][0].globalConfig).toEqual({
onlyChanged: false,
passWithNoTests: true,
testNamePattern: '',
testPathPattern: 'p.*3',
watch: true,
watchAll: false,
});
});
it('Pressing "c" clears the filters', async () => {
contexts[0].config = {rootDir: ''};
watch(globalConfig, contexts, pipe, hasteMapInstances, stdin);
stdin.emit(KEYS.P);
await nextTick();
['p', '.', '*', '1', '0']
.map(toHex)
.concat(KEYS.ENTER)
.forEach(key => stdin.emit(key));
await nextTick();
stdin.emit(KEYS.T);
await nextTick();
['t', 'e', 's', 't']
.map(toHex)
.concat(KEYS.ENTER)
.forEach(key => stdin.emit(key));
await nextTick();
stdin.emit(KEYS.C);
await nextTick();
pipe.write.mockReset();
stdin.emit(KEYS.P);
await nextTick();
expect(pipe.write.mock.calls.join('\n')).toMatchSnapshot();
});
});
class MockStdin {
constructor() {
this._callbacks = [];
}
setRawMode() {}
resume() {}
setEncoding() {}
on(evt, callback) {
this._callbacks.push(callback);
}
emit(key) {
this._callbacks.forEach(cb => cb(key));
}
}