Skip to content

Commit ac300c1

Browse files
authored
Allow worker CLI flags to be specified
This removes AVA's internal flags, set by fork.js and consumed by test-worker.js. Fixes #1393. AVA now forwards arguments, provided after an `--` argument terminator, to the worker process. Arguments are available from `process.argv[2]` onwards.
1 parent 9482a9e commit ac300c1

File tree

20 files changed

+53
-107
lines changed

20 files changed

+53
-107
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Passing arguments to your test files
2+
3+
You can pass command line arguments to your test files. Use the `--` argument terminator to separate AVA's arguments from your own:
4+
5+
```js
6+
// test.js
7+
import test from 'ava';
8+
9+
test('argv', t => {
10+
t.deepEqual(process.argv.slice(2), ['--hello', 'world']);
11+
});
12+
```
13+
14+
```console
15+
$ npx ava -- --hello world
16+
```
17+
18+
You need two `--` argument terminators if you're invoking AVA through an `npm test` script:
19+
20+
```json
21+
{
22+
"scripts": {
23+
"test": "ava"
24+
}
25+
}
26+
```
27+
28+
```console
29+
$ npm test -- -- --hello world
30+
```

lib/cli.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ exports.run = () => {
107107
color: {
108108
type: 'boolean',
109109
default: 'color' in conf ? conf.color : require('supports-color').stdout !== false
110+
},
111+
'--': {
112+
type: 'string'
110113
}
111114
}
112115
});
@@ -156,7 +159,8 @@ exports.run = () => {
156159
concurrency: conf.concurrency ? parseInt(conf.concurrency, 10) : 0,
157160
updateSnapshots: conf.updateSnapshots,
158161
snapshotDir: conf.snapshotDir ? path.resolve(projectDir, conf.snapshotDir) : null,
159-
color: conf.color
162+
color: conf.color,
163+
workerArgv: cli.flags['--']
160164
});
161165

162166
let reporter;

lib/fork.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = (file, opts, execArgv) => {
3434
} : false
3535
}, opts);
3636

37-
const args = [JSON.stringify(opts), opts.color ? '--color' : '--no-color'];
37+
const args = [JSON.stringify(opts), opts.color ? '--color' : '--no-color'].concat(opts.workerArgv);
3838

3939
const ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), args, {
4040
cwd: opts.projectDir,

lib/process-adapter.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ exports.forceRefChannel = () => {
3939
const opts = JSON.parse(process.argv[2]);
4040
require('./worker-options').set(opts);
4141

42+
// Remove arguments received from fork.js and leave those specified by the user.
43+
process.argv.splice(2, 2);
44+
4245
// Fake TTY support
4346
if (opts.tty) {
4447
process.stdout.isTTY = true;

lib/test-worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Check if the test is being run without AVA cli
44
{
55
const path = require('path');
6-
const chalk = require('chalk');
6+
const chalk = require('chalk'); // This processes the --color/--no-color argument passed by fork.js
77

88
const isForked = typeof process.send === 'function';
99
if (!isForked) {

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,7 @@ It's the [Andromeda galaxy](https://simple.wikipedia.org/wiki/Andromeda_galaxy).
11611161
- [Flow](docs/recipes/flow.md)
11621162
- [Configuring Babel][Babel recipe]
11631163
- [Using ES modules](docs/recipes/es-modules.md)
1164+
- [Passing arguments to your test files](docs/recipes/passing-arguments-to-your-test-files.md)
11641165
- [Testing React components](docs/recipes/react.md)
11651166
- [Testing Vue.js components](docs/recipes/vue.md)
11661167
- [JSPM and SystemJS](docs/recipes/jspm-systemjs.md)

test/cli.js

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -210,27 +210,6 @@ test('precompiler require hook does not apply to source files', t => {
210210
});
211211
});
212212

213-
test('pkg-conf: defaults', t => {
214-
execCli([], {dirname: 'fixture/pkg-conf/defaults'}, err => {
215-
t.ifError(err);
216-
t.end();
217-
});
218-
});
219-
220-
test('pkg-conf: pkg-overrides', t => {
221-
execCli([], {dirname: 'fixture/pkg-conf/pkg-overrides'}, err => {
222-
t.ifError(err);
223-
t.end();
224-
});
225-
});
226-
227-
test('pkg-conf: cli takes precedence', t => {
228-
execCli(['--match=foo*', '--no-serial', '--cache', '--no-fail-fast', 'c.js'], {dirname: 'fixture/pkg-conf/precedence'}, err => {
229-
t.ifError(err);
230-
t.end();
231-
});
232-
});
233-
234213
test('pkg-conf(resolve-dir): works as expected when run from the package.json directory', t => {
235214
execCli(['--verbose'], {dirname: 'fixture/pkg-conf/resolve-dir'}, (err, stdout, stderr) => {
236215
t.ifError(err);
@@ -863,3 +842,10 @@ test('workers load compiled helpers if in the require configuration', t => {
863842
t.end();
864843
});
865844
});
845+
846+
test('additional arguments are forwarded to the worker', t => {
847+
execCli(['worker-argv.js', '--serial', '--', '--hello', 'world'], {dirname: 'fixture'}, err => {
848+
t.ifError(err);
849+
t.end();
850+
});
851+
});

test/fixture/pkg-conf/defaults/package.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/fixture/pkg-conf/defaults/test.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/fixture/pkg-conf/pkg-overrides/actual.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)