Skip to content

Commit

Permalink
Allow worker CLI flags to be specified
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
novemberborn committed Feb 13, 2018
1 parent 9482a9e commit ac300c1
Show file tree
Hide file tree
Showing 20 changed files with 53 additions and 107 deletions.
30 changes: 30 additions & 0 deletions docs/recipes/passing-arguments-to-your-test-files.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Passing arguments to your test files

You can pass command line arguments to your test files. Use the `--` argument terminator to separate AVA's arguments from your own:

```js
// test.js
import test from 'ava';

test('argv', t => {
t.deepEqual(process.argv.slice(2), ['--hello', 'world']);
});
```

```console
$ npx ava -- --hello world
```

You need two `--` argument terminators if you're invoking AVA through an `npm test` script:

```json
{
"scripts": {
"test": "ava"
}
}
```

```console
$ npm test -- -- --hello world
```
6 changes: 5 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ exports.run = () => {
color: {
type: 'boolean',
default: 'color' in conf ? conf.color : require('supports-color').stdout !== false
},
'--': {
type: 'string'
}
}
});
Expand Down Expand Up @@ -156,7 +159,8 @@ exports.run = () => {
concurrency: conf.concurrency ? parseInt(conf.concurrency, 10) : 0,
updateSnapshots: conf.updateSnapshots,
snapshotDir: conf.snapshotDir ? path.resolve(projectDir, conf.snapshotDir) : null,
color: conf.color
color: conf.color,
workerArgv: cli.flags['--']
});

let reporter;
Expand Down
2 changes: 1 addition & 1 deletion lib/fork.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = (file, opts, execArgv) => {
} : false
}, opts);

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

const ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), args, {
cwd: opts.projectDir,
Expand Down
3 changes: 3 additions & 0 deletions lib/process-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ exports.forceRefChannel = () => {
const opts = JSON.parse(process.argv[2]);
require('./worker-options').set(opts);

// Remove arguments received from fork.js and leave those specified by the user.
process.argv.splice(2, 2);

// Fake TTY support
if (opts.tty) {
process.stdout.isTTY = true;
Expand Down
2 changes: 1 addition & 1 deletion lib/test-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Check if the test is being run without AVA cli
{
const path = require('path');
const chalk = require('chalk');
const chalk = require('chalk'); // This processes the --color/--no-color argument passed by fork.js

const isForked = typeof process.send === 'function';
if (!isForked) {
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,7 @@ It's the [Andromeda galaxy](https://simple.wikipedia.org/wiki/Andromeda_galaxy).
- [Flow](docs/recipes/flow.md)
- [Configuring Babel][Babel recipe]
- [Using ES modules](docs/recipes/es-modules.md)
- [Passing arguments to your test files](docs/recipes/passing-arguments-to-your-test-files.md)
- [Testing React components](docs/recipes/react.md)
- [Testing Vue.js components](docs/recipes/vue.md)
- [JSPM and SystemJS](docs/recipes/jspm-systemjs.md)
Expand Down
28 changes: 7 additions & 21 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,27 +210,6 @@ test('precompiler require hook does not apply to source files', t => {
});
});

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);
Expand Down Expand Up @@ -863,3 +842,10 @@ test('workers load compiled helpers if in the require configuration', t => {
t.end();
});
});

test('additional arguments are forwarded to the worker', t => {
execCli(['worker-argv.js', '--serial', '--', '--hello', 'world'], {dirname: 'fixture'}, err => {
t.ifError(err);
t.end();
});
});
4 changes: 0 additions & 4 deletions test/fixture/pkg-conf/defaults/package.json

This file was deleted.

10 changes: 0 additions & 10 deletions test/fixture/pkg-conf/defaults/test.js

This file was deleted.

9 changes: 0 additions & 9 deletions test/fixture/pkg-conf/pkg-overrides/actual.js

This file was deleted.

11 changes: 0 additions & 11 deletions test/fixture/pkg-conf/pkg-overrides/package.json

This file was deleted.

2 changes: 0 additions & 2 deletions test/fixture/pkg-conf/pkg-overrides/required.js

This file was deleted.

7 changes: 0 additions & 7 deletions test/fixture/pkg-conf/pkg-overrides/test.js

This file was deleted.

7 changes: 0 additions & 7 deletions test/fixture/pkg-conf/precedence/a.js

This file was deleted.

7 changes: 0 additions & 7 deletions test/fixture/pkg-conf/precedence/b.js

This file was deleted.

10 changes: 0 additions & 10 deletions test/fixture/pkg-conf/precedence/c.js

This file was deleted.

2 changes: 0 additions & 2 deletions test/fixture/pkg-conf/precedence/default-required.js

This file was deleted.

12 changes: 0 additions & 12 deletions test/fixture/pkg-conf/precedence/package.json

This file was deleted.

2 changes: 0 additions & 2 deletions test/fixture/pkg-conf/precedence/required.js

This file was deleted.

5 changes: 5 additions & 0 deletions test/fixture/worker-argv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import test from '../../';

test('argv', t => {
t.deepEqual(process.argv, [process.execPath, require.resolve('../../lib/test-worker.js'), '--hello', 'world']);
});

0 comments on commit ac300c1

Please sign in to comment.