/
cli.js
287 lines (259 loc) 路 7.62 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
'use strict';
const path = require('path');
const del = require('del');
const updateNotifier = require('update-notifier');
const figures = require('figures');
const arrify = require('arrify');
const meow = require('meow');
const Promise = require('bluebird');
const isCi = require('is-ci');
const loadConfig = require('./load-config');
// Bluebird specific
Promise.longStackTraces();
function exit(message) {
console.error(`\n${require('./chalk').get().red(figures.cross)} ${message}`);
process.exit(1); // eslint-disable-line unicorn/no-process-exit
}
exports.run = async () => { // eslint-disable-line complexity
const {flags: {config: configFile}} = meow({ // Process the --config flag first
autoHelp: false, // --help should get picked up by the next meow invocation.
flags: {
config: {type: 'string'}
}
});
let conf = {};
let confError = null;
try {
conf = loadConfig({configFile});
} catch (error) {
confError = error;
}
const cli = meow(`
Usage
ava [<file> ...]
Options
--watch, -w Re-run tests when tests and source files change
--match, -m Only run tests with matching title (Can be repeated)
--update-snapshots, -u Update snapshots
--fail-fast Stop after first test failure
--timeout, -T Set global timeout (milliseconds or human-readable, e.g. 10s, 2m)
--serial, -s Run tests serially
--concurrency, -c Max number of test files running at the same time (Default: CPU cores)
--verbose, -v Enable verbose output
--tap, -t Generate TAP output
--color Force color output
--no-color Disable color output
--reset-cache Reset AVA's compilation cache and exit
--config JavaScript file for AVA to read its config from, instead of using package.json
or ava.config.js files
Examples
ava
ava test.js test2.js
ava test-*.js
ava test
The above relies on your shell expanding the glob patterns.
Without arguments, AVA uses the following patterns:
**/test.js **/test-*.js **/*.spec.js **/*.test.js **/test/**/*.js **/tests/**/*.js **/__tests__/**/*.js
`, {
flags: {
watch: {
type: 'boolean',
alias: 'w'
},
match: {
type: 'string',
alias: 'm',
default: conf.match
},
'update-snapshots': {
type: 'boolean',
alias: 'u'
},
'fail-fast': {
type: 'boolean',
default: conf.failFast
},
timeout: {
type: 'string',
alias: 'T',
default: conf.timeout
},
serial: {
type: 'boolean',
alias: 's',
default: conf.serial
},
concurrency: {
type: 'string',
alias: 'c',
default: conf.concurrency
},
verbose: {
type: 'boolean',
alias: 'v',
default: conf.verbose
},
tap: {
type: 'boolean',
alias: 't',
default: conf.tap
},
color: {
type: 'boolean',
default: 'color' in conf ? conf.color : require('supports-color').stdout !== false
},
'reset-cache': {
type: 'boolean',
default: false
},
'--': {
type: 'string'
}
}
});
updateNotifier({pkg: cli.pkg}).notify();
const chalk = require('./chalk').set({enabled: cli.flags.color});
if (confError) {
if (confError.parent) {
exit(`${confError.message}\n\n${chalk.gray((confError.parent && confError.parent.stack) || confError.parent)}`);
} else {
exit(confError.message);
}
}
const {projectDir} = conf;
if (cli.flags.resetCache) {
const cacheDir = path.join(projectDir, 'node_modules', '.cache', 'ava');
try {
await del('*', {
cwd: cacheDir,
nodir: true
});
console.error(`\n${chalk.green(figures.tick)} Removed AVA cache files in ${cacheDir}`);
process.exit(0); // eslint-disable-line unicorn/no-process-exit
} catch (error) {
exit(`Error removing AVA cache files in ${cacheDir}\n\n${chalk.gray((error && error.stack) || error)}`);
}
return;
}
if (cli.flags.watch && cli.flags.tap && !conf.tap) {
exit('The TAP reporter is not available when using watch mode.');
}
if (cli.flags.watch && isCi) {
exit('Watch mode is not available in CI, as it prevents AVA from terminating.');
}
if (
cli.flags.concurrency === '' ||
(cli.flags.concurrency && (!Number.isInteger(Number.parseFloat(cli.flags.concurrency)) || parseInt(cli.flags.concurrency, 10) < 0))
) {
exit('The --concurrency or -c flag must be provided with a nonnegative integer.');
}
const ciParallelVars = require('ci-parallel-vars');
const Api = require('./api');
const VerboseReporter = require('./reporters/verbose');
const MiniReporter = require('./reporters/mini');
const TapReporter = require('./reporters/tap');
const Watcher = require('./watcher');
const babelPipeline = require('./babel-pipeline');
const normalizeExtensions = require('./extensions');
const {normalizeGlobs} = require('./globs');
const validateEnvironmentVariables = require('./environment-variables');
let babelConfig = null;
try {
babelConfig = babelPipeline.validate(conf.babel);
} catch (error) {
exit(error.message);
}
let environmentVariables;
try {
environmentVariables = validateEnvironmentVariables(conf.environmentVariables);
} catch (error) {
exit(error.message);
}
let extensions;
try {
extensions = normalizeExtensions(conf.extensions || [], babelConfig);
} catch (error) {
exit(error.message);
}
let globs;
try {
globs = normalizeGlobs(conf.files, conf.helpers, conf.sources, extensions.all);
} catch (error) {
exit(error.message);
}
// Copy resultant cli.flags into conf for use with Api and elsewhere
Object.assign(conf, cli.flags);
let parallelRuns = null;
if (isCi && ciParallelVars) {
const {index: currentIndex, total: totalRuns} = ciParallelVars;
parallelRuns = {currentIndex, totalRuns};
}
const match = arrify(conf.match);
const resolveTestsFrom = cli.input.length === 0 ? projectDir : process.cwd();
const api = new Api({
babelConfig,
cacheEnabled: conf.cache !== false,
color: conf.color,
compileEnhancements: conf.compileEnhancements !== false,
concurrency: conf.concurrency ? parseInt(conf.concurrency, 10) : 0,
extensions,
failFast: conf.failFast,
failWithoutAssertions: conf.failWithoutAssertions !== false,
globs,
environmentVariables,
match,
parallelRuns,
projectDir,
ranFromCli: true,
require: arrify(conf.require),
resolveTestsFrom,
serial: conf.serial,
snapshotDir: conf.snapshotDir ? path.resolve(projectDir, conf.snapshotDir) : null,
timeout: conf.timeout,
updateSnapshots: conf.updateSnapshots,
workerArgv: cli.flags['--']
});
let reporter;
if (conf.tap && !conf.watch) {
reporter = new TapReporter({
reportStream: process.stdout,
stdStream: process.stderr
});
} else if (conf.verbose || isCi || !process.stdout.isTTY) {
reporter = new VerboseReporter({
reportStream: process.stdout,
stdStream: process.stderr,
watching: conf.watch
});
} else {
reporter = new MiniReporter({
reportStream: process.stdout,
stdStream: process.stderr,
watching: conf.watch
});
}
api.on('run', plan => {
reporter.startRun(plan);
plan.status.on('stateChange', evt => {
if (evt.type === 'interrupt') {
reporter.endRun();
process.exit(1); // eslint-disable-line unicorn/no-process-exit
}
});
});
const files = cli.input.map(file => path.relative(resolveTestsFrom, path.resolve(process.cwd(), file)));
if (conf.watch) {
const watcher = new Watcher({
api,
reporter,
files,
globs,
resolveTestsFrom
});
watcher.observeStdin(process.stdin);
} else {
const runStatus = await api.run(files);
process.exitCode = runStatus.suggestExitCode({matching: match.length > 0});
reporter.endRun();
}
};