diff --git a/docs/recipes/passing-arguments-to-your-test-files.md b/docs/recipes/passing-arguments-to-your-test-files.md new file mode 100644 index 000000000..f3d837b6e --- /dev/null +++ b/docs/recipes/passing-arguments-to-your-test-files.md @@ -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 +``` diff --git a/lib/cli.js b/lib/cli.js index 0b5859246..ab6981aea 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -107,6 +107,9 @@ exports.run = () => { color: { type: 'boolean', default: 'color' in conf ? conf.color : require('supports-color').stdout !== false + }, + '--': { + type: 'string' } } }); @@ -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; diff --git a/lib/fork.js b/lib/fork.js index 7a7d4f73a..e24340c8c 100644 --- a/lib/fork.js +++ b/lib/fork.js @@ -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, diff --git a/lib/process-adapter.js b/lib/process-adapter.js index e44105950..cf4685b4f 100644 --- a/lib/process-adapter.js +++ b/lib/process-adapter.js @@ -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; diff --git a/lib/test-worker.js b/lib/test-worker.js index ff93e5ad0..d343f35f5 100644 --- a/lib/test-worker.js +++ b/lib/test-worker.js @@ -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) { diff --git a/readme.md b/readme.md index eaa8257b2..ca63dec8b 100644 --- a/readme.md +++ b/readme.md @@ -1160,6 +1160,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) diff --git a/test/cli.js b/test/cli.js index 07652fc39..64c933255 100644 --- a/test/cli.js +++ b/test/cli.js @@ -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); @@ -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(); + }); +}); diff --git a/test/fixture/pkg-conf/defaults/package.json b/test/fixture/pkg-conf/defaults/package.json deleted file mode 100644 index daa8ae391..000000000 --- a/test/fixture/pkg-conf/defaults/package.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "name": "application-name", - "version": "0.0.1" -} diff --git a/test/fixture/pkg-conf/defaults/test.js b/test/fixture/pkg-conf/defaults/test.js deleted file mode 100644 index 86163e864..000000000 --- a/test/fixture/pkg-conf/defaults/test.js +++ /dev/null @@ -1,10 +0,0 @@ -import test from '../../../../'; - -const opts = JSON.parse(process.argv[2]); - -test('test', t => { - t.is(opts.failFast, false); - t.is(opts.serial, false); - t.is(opts.cacheEnabled, true); - t.deepEqual(opts.require, []); -}); diff --git a/test/fixture/pkg-conf/pkg-overrides/actual.js b/test/fixture/pkg-conf/pkg-overrides/actual.js deleted file mode 100644 index fd1383802..000000000 --- a/test/fixture/pkg-conf/pkg-overrides/actual.js +++ /dev/null @@ -1,9 +0,0 @@ -import test from '../../../../'; - -const opts = JSON.parse(process.argv[2]); - -test('test', t => { - t.is(opts.failFast, true); - t.is(opts.serial, true); - t.is(opts.cacheEnabled, false); -}); diff --git a/test/fixture/pkg-conf/pkg-overrides/package.json b/test/fixture/pkg-conf/pkg-overrides/package.json deleted file mode 100644 index d1a83fb43..000000000 --- a/test/fixture/pkg-conf/pkg-overrides/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "application-name", - "version": "0.0.1", - "ava": { - "files": "actual.js", - "serial": true, - "failFast": true, - "cache": false, - "require": "./required.js" - } -} diff --git a/test/fixture/pkg-conf/pkg-overrides/required.js b/test/fixture/pkg-conf/pkg-overrides/required.js deleted file mode 100644 index 8a292376a..000000000 --- a/test/fixture/pkg-conf/pkg-overrides/required.js +++ /dev/null @@ -1,2 +0,0 @@ -'use strict'; -module.exports = 'foo'; diff --git a/test/fixture/pkg-conf/pkg-overrides/test.js b/test/fixture/pkg-conf/pkg-overrides/test.js deleted file mode 100644 index 232ea423b..000000000 --- a/test/fixture/pkg-conf/pkg-overrides/test.js +++ /dev/null @@ -1,7 +0,0 @@ -import test from '../../../../'; - -// This should never be loaded - package.json overrides files to call `actual.js` - -test('test', t => { - t.fail(); -}); diff --git a/test/fixture/pkg-conf/precedence/a.js b/test/fixture/pkg-conf/precedence/a.js deleted file mode 100644 index 232ea423b..000000000 --- a/test/fixture/pkg-conf/precedence/a.js +++ /dev/null @@ -1,7 +0,0 @@ -import test from '../../../../'; - -// This should never be loaded - package.json overrides files to call `actual.js` - -test('test', t => { - t.fail(); -}); diff --git a/test/fixture/pkg-conf/precedence/b.js b/test/fixture/pkg-conf/precedence/b.js deleted file mode 100644 index 232ea423b..000000000 --- a/test/fixture/pkg-conf/precedence/b.js +++ /dev/null @@ -1,7 +0,0 @@ -import test from '../../../../'; - -// This should never be loaded - package.json overrides files to call `actual.js` - -test('test', t => { - t.fail(); -}); diff --git a/test/fixture/pkg-conf/precedence/c.js b/test/fixture/pkg-conf/precedence/c.js deleted file mode 100644 index a08d757e0..000000000 --- a/test/fixture/pkg-conf/precedence/c.js +++ /dev/null @@ -1,10 +0,0 @@ -import test from '../../../../'; - -const opts = JSON.parse(process.argv[2]); - -test('foo', t => { - t.is(opts.failFast, false); - t.is(opts.serial, false); - t.is(opts.cacheEnabled, true); - t.deepEqual(opts.match, ['foo*']); -}); diff --git a/test/fixture/pkg-conf/precedence/default-required.js b/test/fixture/pkg-conf/precedence/default-required.js deleted file mode 100644 index 595e37674..000000000 --- a/test/fixture/pkg-conf/precedence/default-required.js +++ /dev/null @@ -1,2 +0,0 @@ -'use strict'; -module.exports = 'bar'; diff --git a/test/fixture/pkg-conf/precedence/package.json b/test/fixture/pkg-conf/precedence/package.json deleted file mode 100644 index d27f53801..000000000 --- a/test/fixture/pkg-conf/precedence/package.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "name": "application-name", - "version": "0.0.1", - "ava": { - "files": ["a.js", "b.js"], - "serial": true, - "failFast": true, - "cache": false, - "match": ["*"], - "require": "./default-required.js" - } -} diff --git a/test/fixture/pkg-conf/precedence/required.js b/test/fixture/pkg-conf/precedence/required.js deleted file mode 100644 index 8a292376a..000000000 --- a/test/fixture/pkg-conf/precedence/required.js +++ /dev/null @@ -1,2 +0,0 @@ -'use strict'; -module.exports = 'foo'; diff --git a/test/fixture/worker-argv.js b/test/fixture/worker-argv.js new file mode 100644 index 000000000..614d723bf --- /dev/null +++ b/test/fixture/worker-argv.js @@ -0,0 +1,5 @@ +import test from '../../'; + +test('argv', t => { + t.deepEqual(process.argv, [process.execPath, require.resolve('../../lib/test-worker.js'), '--hello', 'world']); +});