-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Expand file tree
/
Copy pathrunner.test.js
More file actions
436 lines (346 loc) · 13.2 KB
/
runner.test.js
File metadata and controls
436 lines (346 loc) · 13.2 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/**
* 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.
*
* @flow
*/
'use strict';
jest.mock('../Process');
jest.mock('child_process', () => ({spawn: jest.fn()}));
jest.mock('os', () => ({tmpdir: jest.fn()}));
jest.mock('fs', () => {
// $FlowFixMe requireActual
const readFileSync = require.requireActual('fs').readFileSync;
// Replace `readFile` with `readFileSync` so we don't get multiple threads
return {
readFile: (path, type, closure) => {
const data = readFileSync(path);
closure(null, data);
},
readFileSync,
};
});
const path = require('path');
const fixtures = path.resolve(__dirname, '../../../../fixtures');
import ProjectWorkspace from '../project_workspace';
import {messageTypes} from '../types';
import {default as Runner} from '../Runner';
import {createProcess} from '../Process';
import {tmpdir} from 'os';
import {spawn} from 'child_process';
import {readFileSync} from 'fs';
import EventEmitter from 'events';
import type {ChildProcess} from 'child_process';
describe('Runner', () => {
describe('constructor', () => {
it('does not set watchMode', () => {
const workspace: any = {};
const sut = new Runner(workspace);
expect(sut.watchMode).not.toBeDefined();
});
it('sets the output filepath', () => {
tmpdir.mockReturnValueOnce('tmpdir');
const workspace: any = {};
const sut = new Runner(workspace);
expect(sut.outputPath).toBe('tmpdir/jest_runner.json');
});
it('sets the default options', () => {
const workspace: any = {};
const sut = new Runner(workspace);
expect(sut.options).toEqual({});
});
it('sets the options', () => {
const workspace: any = {};
const options = {};
const sut = new Runner(workspace, options);
expect(sut.options).toBe(options);
});
});
describe('start', () => {
beforeEach(() => {
jest.resetAllMocks();
(createProcess: any).mockImplementationOnce(
(workspace, args, options) => {
const process: any = new EventEmitter();
process.stdout = new EventEmitter();
process.stderr = new EventEmitter();
return process;
},
);
});
it('will not start when started', () => {
const workspace: any = {};
const sut = new Runner(workspace);
sut.start();
sut.start();
expect(createProcess).toHaveBeenCalledTimes(1);
});
it('sets watchMode', () => {
const expected = true;
const workspace: any = {};
const sut = new Runner(workspace);
sut.start(expected);
expect(sut.watchMode).toBe(expected);
});
it('calls createProcess', () => {
const workspace: any = {};
const sut = new Runner(workspace);
sut.start(false);
expect((createProcess: any).mock.calls[0][0]).toBe(workspace);
});
it('calls createProcess with the --json arg', () => {
const workspace: any = {};
const sut = new Runner(workspace);
sut.start(false);
expect((createProcess: any).mock.calls[0][1]).toContain('--json');
});
it('calls createProcess with the --useStderr arg', () => {
const workspace: any = {};
const sut = new Runner(workspace);
sut.start(false);
expect((createProcess: any).mock.calls[0][1]).toContain('--useStderr');
});
it('calls createProcess with the --jsonOutputFile arg for Jest 17 and below', () => {
const workspace: any = {localJestMajorVersion: 17};
const sut = new Runner(workspace);
sut.start(false);
const args = (createProcess: any).mock.calls[0][1];
const index = args.indexOf('--jsonOutputFile');
expect(index).not.toBe(-1);
expect(args[index + 1]).toBe(sut.outputPath);
});
it('calls createProcess with the --outputFile arg for Jest 18 and above', () => {
const workspace: any = {localJestMajorVersion: 18};
const sut = new Runner(workspace);
sut.start(false);
const args = (createProcess: any).mock.calls[0][1];
const index = args.indexOf('--outputFile');
expect(index).not.toBe(-1);
expect(args[index + 1]).toBe(sut.outputPath);
});
it('calls createProcess with the --watch arg when provided', () => {
const workspace: any = {};
const sut = new Runner(workspace);
sut.start(true);
expect((createProcess: any).mock.calls[0][1]).toContain('--watch');
});
it('calls createProcess with the --testNamePattern arg when provided', () => {
const expected = 'testNamePattern';
const workspace: any = {};
const options = {testNamePattern: expected};
const sut = new Runner(workspace, options);
sut.start(false);
const args = (createProcess: any).mock.calls[0][1];
const index = args.indexOf('--testNamePattern');
expect(index).not.toBe(-1);
expect(args[index + 1]).toBe(expected);
});
it('calls createProcess with a test path pattern when provided', () => {
const expected = 'testPathPattern';
const workspace: any = {};
const options = {testFileNamePattern: expected};
const sut = new Runner(workspace, options);
sut.start(false);
expect((createProcess: any).mock.calls[0][1]).toContain(expected);
});
it('calls createProcess with the shell option when provided', () => {
const workspace: any = {};
const options = {shell: true};
const sut = new Runner(workspace, options);
sut.start(false);
expect((createProcess: any).mock.calls[0][2]).toEqual({shell: true});
});
});
describe('closeProcess', () => {
let platformPV;
beforeEach(() => {
jest.resetAllMocks();
platformPV = process.platform;
// Remove the "process.platform" property descriptor so it can be writable.
delete process.platform;
});
afterEach(() => {
process.platform = platformPV;
});
it('does nothing if the runner has not started', () => {
const workspace: any = {};
const sut = new Runner(workspace);
sut.closeProcess();
expect(spawn).not.toBeCalled();
});
it('spawns taskkill to close the process on Windows', () => {
const workspace: any = {};
const sut = new Runner(workspace);
process.platform = 'win32';
sut.debugprocess = ({pid: 123}: any);
sut.closeProcess();
expect(spawn).toBeCalledWith('taskkill', ['/pid', '123', '/T', '/F']);
});
it('calls kill() to close the process on POSIX', () => {
const workspace: any = {};
const sut = new Runner(workspace);
process.platform = 'posix';
const kill = jest.fn();
sut.debugprocess = ({kill}: any);
sut.closeProcess();
expect(kill).toBeCalledWith();
});
it('clears the debugprocess property', () => {
const workspace: any = {};
const sut = new Runner(workspace);
sut.debugprocess = ({kill: () => {}}: any);
sut.closeProcess();
expect(sut.debugprocess).not.toBeDefined();
});
});
});
describe('events', () => {
let runner: Runner;
let fakeProcess: ChildProcess;
beforeEach(() => {
jest.resetAllMocks();
fakeProcess = (new EventEmitter(): any);
fakeProcess.stdout = new EventEmitter();
fakeProcess.stderr = new EventEmitter();
fakeProcess.kill = () => {};
(createProcess: any).mockImplementation(() => fakeProcess);
const workspace = new ProjectWorkspace(
'.',
'node_modules/.bin/jest',
'test',
18,
);
runner = new Runner(workspace);
// Sets it up and registers for notifications
runner.start();
});
it('expects JSON from stdout, then it passes the JSON', () => {
const data = jest.fn();
runner.on('executableJSON', data);
runner.outputPath = `${fixtures}/failing_jsons/failing_jest_json.json`;
// Emitting data through stdout should trigger sending JSON
fakeProcess.stdout.emit('data', 'Test results written to file');
expect(data).toBeCalled();
// And lets check what we emit
const dataAtPath = readFileSync(runner.outputPath);
const storedJSON = JSON.parse(dataAtPath.toString());
expect(data.mock.calls[0][0]).toEqual(storedJSON);
});
it('emits errors when process errors', () => {
const error = jest.fn();
runner.on('terminalError', error);
fakeProcess.emit('error', {});
expect(error).toBeCalled();
});
it('emits debuggerProcessExit when process exits', () => {
const close = jest.fn();
runner.on('debuggerProcessExit', close);
fakeProcess.emit('exit');
expect(close).toBeCalled();
});
it('should start jest process after killing the old process', () => {
runner.closeProcess();
runner.start();
expect(createProcess).toHaveBeenCalledTimes(2);
});
describe('stdout.on("data")', () => {
it('should emit an "executableJSON" event with the "noTestsFound" meta data property set', () => {
const listener = jest.fn();
runner.on('executableJSON', listener);
runner.outputPath = `${fixtures}/failing_jsons/failing_jest_json.json`;
(runner: any).doResultsFollowNoTestsFoundMessage = jest
.fn()
.mockReturnValueOnce(true);
fakeProcess.stdout.emit('data', 'Test results written to file');
expect(listener.mock.calls[0].length).toBe(2);
expect(listener.mock.calls[0][1]).toEqual({noTestsFound: true});
});
it('should clear the message type history', () => {
runner.outputPath = `${fixtures}/failing_jsons/failing_jest_json.json`;
runner.prevMessageTypes.push(messageTypes.noTests);
fakeProcess.stdout.emit('data', 'Test results written to file');
expect(runner.prevMessageTypes.length).toBe(0);
});
});
describe('stderr.on("data")', () => {
it('should identify the message type', () => {
(runner: any).findMessageType = jest.fn();
const expected = {};
fakeProcess.stderr.emit('data', expected);
expect(runner.findMessageType).toBeCalledWith(expected);
});
it('should add the type to the message type history when known', () => {
(runner: any).findMessageType = jest
.fn()
.mockReturnValueOnce(messageTypes.noTests);
fakeProcess.stderr.emit('data', Buffer.from(''));
expect(runner.prevMessageTypes).toEqual([messageTypes.noTests]);
});
it('should clear the message type history when the type is unknown', () => {
(runner: any).findMessageType = jest
.fn()
.mockReturnValueOnce(messageTypes.unknown);
fakeProcess.stderr.emit('data', Buffer.from(''));
expect(runner.prevMessageTypes).toEqual([]);
});
it('should emit an "executableStdErr" event with the type', () => {
const listener = jest.fn();
const data = Buffer.from('');
const type = {};
const meta = {type};
(runner: any).findMessageType = jest.fn().mockReturnValueOnce(type);
runner.on('executableStdErr', listener);
fakeProcess.stderr.emit('data', data, meta);
expect(listener).toBeCalledWith(data, meta);
});
it('should track when "No tests found related to files changed since the last commit" is received', () => {
const data = Buffer.from(
'No tests found related to files changed since last commit.\n' +
'Press `a` to run all tests, or run Jest with `--watchAll`.',
);
fakeProcess.stderr.emit('data', data);
expect(runner.prevMessageTypes).toEqual([messageTypes.noTests]);
});
it('should clear the message type history when any other other data is received', () => {
const data = Buffer.from('');
fakeProcess.stderr.emit('data', data);
expect(runner.prevMessageTypes).toEqual([]);
});
});
describe('findMessageType()', () => {
it('should return "unknown" when the message is not matched', () => {
const buf = Buffer.from('');
expect(runner.findMessageType(buf)).toBe(messageTypes.unknown);
});
it('should identify "No tests found related to files changed since last commit."', () => {
const buf = Buffer.from(
'No tests found related to files changed since last commit.\n' +
'Press `a` to run all tests, or run Jest with `--watchAll`.',
);
expect(runner.findMessageType(buf)).toBe(messageTypes.noTests);
});
it('should identify the "Watch Usage" prompt', () => {
const buf = Buffer.from('\n\nWatch Usage\n...');
expect(runner.findMessageType(buf)).toBe(messageTypes.watchUsage);
});
});
describe('doResultsFollowNoTestsFoundMessage()', () => {
it('should return true when the last message on stderr was "No tests found..."', () => {
runner.prevMessageTypes.push(messageTypes.noTests);
expect(runner.doResultsFollowNoTestsFoundMessage()).toBe(true);
});
it('should return true when the last two messages on stderr were "No tests found..." and "Watch Usage"', () => {
runner.prevMessageTypes.push(
messageTypes.noTests,
messageTypes.watchUsage,
);
expect(runner.doResultsFollowNoTestsFoundMessage()).toBe(true);
});
it('should return false otherwise', () => {
runner.prevMessageTypes.length = 0;
expect(runner.doResultsFollowNoTestsFoundMessage()).toBe(false);
});
});
});