-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathcli.js
544 lines (468 loc) · 15.7 KB
/
cli.js
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
'use strict';
const fs = require('fs');
const path = require('path');
const childProcess = require('child_process');
const test = require('tap').test;
const getStream = require('get-stream');
const figures = require('figures');
const makeDir = require('make-dir');
const touch = require('touch');
const proxyquire = require('proxyquire');
const sinon = require('sinon');
const uniqueTempDir = require('unique-temp-dir');
const execa = require('execa');
const stripAnsi = require('strip-ansi');
const cliPath = path.join(__dirname, '../cli.js');
function execCli(args, opts, cb) {
let dirname;
let env;
if (typeof opts === 'function') {
cb = opts;
dirname = __dirname;
env = {};
} else {
dirname = path.join(__dirname, opts.dirname ? opts.dirname : '');
env = opts.env || {};
}
if (process.env.AVA_APPVEYOR) {
env.AVA_APPVEYOR = 1;
}
let child;
let stdout;
let stderr;
const processPromise = new Promise(resolve => {
child = childProcess.spawn(process.execPath, [path.relative(dirname, cliPath)].concat(args), {
cwd: dirname,
env,
stdio: [null, 'pipe', 'pipe']
});
child.on('close', (code, signal) => {
if (code) {
const err = new Error(`test-worker exited with a non-zero exit code: ${code}`);
err.code = code;
err.signal = signal;
resolve(err);
return;
}
resolve(code);
});
stdout = getStream(child.stdout);
stderr = getStream(child.stderr);
});
Promise.all([processPromise, stdout, stderr]).then(args => {
cb.apply(null, args);
});
return child;
}
test('disallow invalid babel config shortcuts', t => {
execCli(['es2015.js'], {dirname: 'fixture/invalid-babel-config'}, (err, stdout, stderr) => {
t.ok(err);
let expectedOutput = '\n ';
expectedOutput += figures.cross + ' Unexpected Babel configuration for AVA.';
expectedOutput += ' See https://github.com/avajs/ava#es2017-support for allowed values.';
expectedOutput += '\n';
t.is(stderr, expectedOutput);
t.end();
});
});
test('timeout', t => {
execCli(['fixture/long-running.js', '-T', '1s'], (err, stdout, stderr) => {
t.ok(err);
t.match(stderr, /Exited because no new tests completed within the last 1000ms of inactivity/);
t.end();
});
});
test('throwing a named function will report the to the console', t => {
execCli('fixture/throw-named-function.js', (err, stdout, stderr) => {
t.ok(err);
t.match(stderr, /function fooFn\(\) \{\}/);
// TODO(jamestalmage)
// t.ok(/1 uncaught exception[^s]/.test(stdout));
t.end();
});
});
test('improper use of t.throws will be reported to the console', t => {
execCli('fixture/improper-t-throws/throws.js', (err, stdout, stderr) => {
t.ok(err);
t.match(stderr, /Improper usage of `t\.throws\(\)` detected/);
t.match(stderr, /should be detected/);
t.match(stderr, /Try wrapping the first argument/);
t.end();
});
});
test('improper use of t.throws from within a Promise will be reported to the console', t => {
execCli('fixture/improper-t-throws/promise.js', (err, stdout, stderr) => {
t.ok(err);
t.match(stderr, /Improper usage of `t\.throws\(\)` detected/);
t.match(stderr, /should be detected/);
t.match(stderr, /Try wrapping the first argument/);
t.end();
});
});
test('improper use of t.throws from within a pending promise, even if caught and rethrown immediately, will be reported to the console', t => {
execCli('fixture/improper-t-throws/leaked-from-promise.js', (err, stdout, stderr) => {
t.ok(err);
t.match(stderr, /Improper usage of `t\.throws\(\)` detected/);
t.match(stderr, /should be detected/);
t.match(stderr, /Try wrapping the first argument/);
t.end();
});
});
test('improper use of t.throws from within an async callback will be reported to the console', t => {
execCli('fixture/improper-t-throws/async-callback.js', (err, stdout, stderr) => {
t.ok(err);
t.match(stderr, /Improper usage of `t\.throws\(\)` detected/);
t.match(stderr, /should be detected/);
t.match(stderr, /Try wrapping the first argument/);
t.end();
});
});
test('improper use of t.throws, swallowed as an unhandled rejection, will be reported to the console', t => {
execCli('fixture/improper-t-throws/unhandled-rejection.js', (err, stdout, stderr) => {
t.ok(err);
t.match(stderr, /Improper usage of `t\.throws\(\)` detected/);
t.match(stderr, /should be detected/);
t.match(stderr, /Try wrapping the first argument/);
t.end();
});
});
test('improper use of t.throws, even if caught, will be reported to the console', t => {
execCli('fixture/improper-t-throws/caught.js', (err, stdout, stderr) => {
t.ok(err);
t.match(stderr, /Improper usage of `t\.throws\(\)` detected/);
t.notMatch(stderr, /should be detected/);
t.match(stderr, /Try wrapping the first argument/);
t.end();
});
});
test('improper use of t.throws, even if caught and then rethrown immediately, will be reported to the console', t => {
execCli('fixture/improper-t-throws/caught-and-leaked.js', (err, stdout, stderr) => {
t.ok(err);
t.match(stderr, /Improper usage of `t\.throws\(\)` detected/);
t.match(stderr, /should be detected/);
t.match(stderr, /Try wrapping the first argument/);
t.end();
});
});
test('improper use of t.throws, even if caught and then later rethrown, will be reported to the console', t => {
execCli('fixture/improper-t-throws/caught-and-leaked-slowly.js', (err, stdout, stderr) => {
t.ok(err);
t.match(stderr, /Improper usage of `t\.throws\(\)` detected/);
t.match(stderr, /should be detected/);
t.match(stderr, /Try wrapping the first argument/);
t.end();
});
});
test('improper use of t.throws, even if caught and then rethrown too slowly, will be reported to the console', t => {
execCli('fixture/improper-t-throws/caught-and-leaked-too-slowly.js', (err, stdout, stderr) => {
t.ok(err);
t.match(stderr, /Improper usage of `t\.throws\(\)` detected/);
t.notMatch(stderr, /should be detected/);
t.match(stderr, /Try wrapping the first argument/);
t.end();
});
});
test('babel require hook only does not apply to source files', t => {
t.plan(3);
execCli('fixture/babel-hook.js', (err, stdout, stderr) => {
t.ok(err);
t.is(err.code, 1);
t.match(stderr, /Unexpected (token|reserved word)/);
t.end();
});
});
test('throwing a anonymous function will report the function to the console', t => {
execCli('fixture/throw-anonymous-function.js', (err, stdout, stderr) => {
t.ok(err);
t.match(stderr, /\(\) => \{\}/);
// TODO(jamestalmage)
// t.ok(/1 uncaught exception[^s]/.test(stdout));
t.end();
});
});
test('pkg-conf: defaults', t => {
execCli([], {dirname: 'fixture/pkg-conf/defaults'}, err => {
t.ifError(err);
t.end();
});
});
test('pkg-conf: pkg-overrides', t => {
execCli([], {dirname: 'fixture/pkg-conf/pkg-overrides'}, err => {
t.ifError(err);
t.end();
});
});
test('pkg-conf: cli takes precedence', t => {
execCli(['--match=foo*', '--no-serial', '--cache', '--no-fail-fast', 'c.js'], {dirname: 'fixture/pkg-conf/precedence'}, err => {
t.ifError(err);
t.end();
});
});
test('pkg-conf(resolve-dir): works as expected when run from the package.json directory', t => {
execCli(['--verbose'], {dirname: 'fixture/pkg-conf/resolve-dir'}, (err, stdout, stderr) => {
t.ifError(err);
t.match(stderr, /dir-a-base-1/);
t.match(stderr, /dir-a-base-2/);
t.notMatch(stderr, /dir-a-wrapper/);
t.notMatch(stdout, /dir-a-wrapper/);
t.end();
});
});
test('pkg-conf(resolve-dir): resolves tests from the package.json dir if none are specified on cli', t => {
execCli(['--verbose'], {dirname: 'fixture/pkg-conf/resolve-dir/dir-a-wrapper'}, (err, stdout, stderr) => {
t.ifError(err);
t.match(stderr, /dir-a-base-1/);
t.match(stderr, /dir-a-base-2/);
t.notMatch(stderr, /dir-a-wrapper/);
t.notMatch(stdout, /dir-a-wrapper/);
t.end();
});
});
test('pkg-conf(resolve-dir): resolves tests process.cwd() if globs are passed on the command line', t => {
execCli(['--verbose', 'dir-a/*.js'], {dirname: 'fixture/pkg-conf/resolve-dir/dir-a-wrapper'}, (err, stdout, stderr) => {
t.ifError(err);
t.match(stderr, /dir-a-wrapper-3/);
t.match(stderr, /dir-a-wrapper-4/);
t.notMatch(stderr, /dir-a-base/);
t.notMatch(stdout, /dir-a-base/);
t.end();
});
});
test('watcher reruns test files when they changed', t => {
let killed = false;
const child = execCli(['--verbose', '--watch', 'test.js'], {dirname: 'fixture/watcher'}, err => {
t.ok(killed);
t.ifError(err);
t.end();
});
let buffer = '';
let passedFirst = false;
child.stderr.on('data', str => {
buffer += str;
if (/1 test passed/.test(buffer)) {
if (!passedFirst) {
touch.sync(path.join(__dirname, 'fixture/watcher/test.js'));
buffer = '';
passedFirst = true;
} else if (!killed) {
child.kill();
killed = true;
}
}
});
});
test('watcher reruns test files when source dependencies change', t => {
let killed = false;
const child = execCli(['--verbose', '--watch', 'test-*.js'], {dirname: 'fixture/watcher/with-dependencies'}, err => {
t.ok(killed);
t.ifError(err);
t.end();
});
let buffer = '';
let passedFirst = false;
child.stderr.on('data', str => {
buffer += str;
if (/2 tests passed/.test(buffer) && !passedFirst) {
touch.sync(path.join(__dirname, 'fixture/watcher/with-dependencies/source.js'));
buffer = '';
passedFirst = true;
} else if (/1 test passed/.test(buffer) && !killed) {
child.kill();
killed = true;
}
});
});
test('`"tap": true` config is ignored when --watch is given', t => {
let killed = false;
const child = execCli(['--watch', 'test.js'], {dirname: 'fixture/watcher/tap-in-conf'}, () => {
t.ok(killed);
t.end();
});
let combined = '';
const testOutput = output => {
combined += output;
t.notMatch(combined, /TAP/);
if (/works/.test(combined)) {
child.kill();
killed = true;
}
};
child.stdout.on('data', testOutput);
child.stderr.on('data', testOutput);
});
test('bails when config contains `"tap": true` and `"watch": true`', t => {
execCli(['test.js'], {dirname: 'fixture/watcher/tap-and-watch-in-conf'}, (err, stdout, stderr) => {
t.is(err.code, 1);
t.match(stderr, 'The TAP reporter is not available when using watch mode.');
t.end();
});
});
['--watch', '-w'].forEach(watchFlag => {
['--tap', '-t'].forEach(tapFlag => {
test(`bails when ${tapFlag} reporter is used while ${watchFlag} is given`, t => {
execCli([tapFlag, watchFlag, 'test.js'], {dirname: 'fixture/watcher'}, (err, stdout, stderr) => {
t.is(err.code, 1);
t.match(stderr, 'The TAP reporter is not available when using watch mode.');
t.end();
});
});
});
});
['--watch', '-w'].forEach(watchFlag => {
test(`bails when CI is used while ${watchFlag} is given`, t => {
execCli([watchFlag, 'test.js'], {dirname: 'fixture/watcher', env: {CI: true}}, (err, stdout, stderr) => {
t.is(err.code, 1);
t.match(stderr, 'Watch mode is not available in CI, as it prevents AVA from terminating.');
t.end();
});
});
});
test('--match works', t => {
execCli(['-m=foo', '-m=bar', '-m=!baz', '-m=t* a* f*', '-m=!t* a* n* f*', 'fixture/matcher-skip.js'], err => {
t.ifError(err);
t.end();
});
});
['--tap', '-t'].forEach(tapFlag => {
test(`${tapFlag} should produce TAP output`, t => {
execCli([tapFlag, 'test.js'], {dirname: 'fixture/watcher'}, err => {
t.ok(!err);
t.end();
});
});
});
test('handles NODE_PATH', t => {
const nodePaths = `fixture/node-paths/modules${path.delimiter}fixture/node-paths/deep/nested`;
execCli('fixture/node-paths.js', {env: {NODE_PATH: nodePaths}}, err => {
t.ifError(err);
t.end();
});
});
test('works when no files are found', t => {
execCli('!*', (err, stdout, stderr) => {
t.is(err.code, 1);
t.match(stderr, 'Couldn\'t find any files to test');
t.end();
});
});
test('should warn ava is required without the cli', t => {
childProcess.execFile(process.execPath, [path.resolve(__dirname, '../index.js')], error => {
t.ok(error);
t.match(error.message, /Test files must be run with the AVA CLI/);
t.end();
});
});
test('prefers local version of ava', t => {
t.plan(1);
const stubModulePath = path.join(__dirname, '/fixture/empty');
const debugSpy = sinon.spy();
const resolveCwdStub = () => stubModulePath;
function debugStub() {
return message => {
let result = {
enabled: false
};
if (message) {
result = debugSpy(message);
}
return result;
};
}
proxyquire('../cli', {
debug: debugStub,
'resolve-cwd': resolveCwdStub
});
t.ok(debugSpy.calledWith('Using local install of AVA'));
t.end();
});
test('use current working directory if `package.json` is not found', () => {
const cwd = uniqueTempDir({create: true});
const testFilePath = path.join(cwd, 'test.js');
const cliPath = require.resolve('../cli.js');
const avaPath = require.resolve('../');
fs.writeFileSync(testFilePath, `import test from ${JSON.stringify(avaPath)};\ntest(t => { t.pass(); });`);
return execa(process.execPath, [cliPath], {cwd});
});
test('workers ensure test files load the same version of ava', t => {
const target = path.join(__dirname, 'fixture', 'ava-paths', 'target');
// Copy the index.js so the testFile imports it. It should then load the correct AVA install.
const targetInstall = path.join(target, 'node_modules/ava');
makeDir.sync(targetInstall);
fs.writeFileSync(
path.join(targetInstall, 'index.js'),
fs.readFileSync(path.join(__dirname, '../index.js'))
);
const testFile = path.join(target, 'test.js');
execCli([testFile], {dirname: path.join('fixture', 'ava-paths', 'cwd')}, err => {
t.ifError(err);
t.end();
});
});
test('worker errors are treated as uncaught exceptions', t => {
execCli(['--no-color', '--verbose', 'test.js'], {dirname: 'fixture/trigger-worker-exception'}, (_, __, stderr) => {
t.match(stderr, /Forced error/);
t.end();
});
});
test('uncaught exceptions are raised for worker errors even if the error cannot be serialized', t => {
execCli(['--no-color', '--verbose', 'test-fallback.js'], {dirname: 'fixture/trigger-worker-exception'}, (_, __, stderr) => {
t.match(stderr, /Failed to serialize uncaught exception/);
t.end();
});
});
test('tests without assertions do not fail if failWithoutAssertions option is set to false', t => {
execCli([], {dirname: 'fixture/pkg-conf/fail-without-assertions'}, err => {
t.ifError(err);
t.end();
});
});
test('callback tests fail if event loop empties before they\'re ended', t => {
execCli('callback.js', {dirname: 'fixture/stalled-tests'}, (_, __, stderr) => {
t.match(stderr, /`t\.end\(\)` was never called/);
t.end();
});
});
test('observable tests fail if event loop empties before they\'re resolved', t => {
execCli('observable.js', {dirname: 'fixture/stalled-tests'}, (_, __, stderr) => {
t.match(stderr, /Observable returned by test never completed/);
t.end();
});
});
test('promise tests fail if event loop empties before they\'re resolved', t => {
execCli('promise.js', {dirname: 'fixture/stalled-tests'}, (_, __, stderr) => {
t.match(stderr, /Promise returned by test never resolved/);
t.end();
});
});
test('snapshots work', t => {
try {
fs.unlinkSync(path.join(__dirname, 'fixture', 'snapshots', '__snapshots__', 'test.snap'));
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
// Test should pass, and a snapshot gets written
execCli(['--update-snapshots', 'test.js'], {dirname: 'fixture/snapshots'}, err => {
t.ifError(err);
// Test should pass, and the snapshot gets used
execCli(['test.js'], {dirname: 'fixture/snapshots'}, err => {
t.ifError(err);
t.end();
});
});
});
test('--no-color disables formatting colors', t => {
execCli(['--no-color', '--verbose', 'formatting-color.js'], {dirname: 'fixture'}, (err, stdout, stderr) => {
t.ok(err);
t.is(stripAnsi(stderr), stderr);
t.end();
});
});
test('--color enables formatting colors', t => {
execCli(['--color', '--verbose', 'formatting-color.js'], {dirname: 'fixture'}, (err, stdout, stderr) => {
t.ok(err);
t.isNot(stripAnsi(stderr), stderr);
t.end();
});
});