I'm looking for ways to pass arguments to my ava test file via command line and I found this documentation. https://github.com/avajs/ava/blob/main/docs/recipes/passing-arguments-to-your-test-files.md
// test.js const test = require('ava');
test('argv', t => {
t.deepEqual(process.argv.slice(2), ['--hello', 'world']);
});
$ npx ava -- --hello world
I was wondering that what this code is actually doing but I can't find other related topic online talking about this. Is there anyone who can explain to me?
This code is just showing you that in node - and therefore also in ava
, the arguments are available on the array process.argv
. You can find it documented here
The slice(2)
just trims off the first two elements of the array. From the documentation I linked above, this is because:
The first element will be process.execPath.
The second element will be the path to the JavaScript file being executed
So your arguments start at process.argv[2]
.
The t.deepEqual
is just illustrative to show the reader that the value of process.argv.slice(2)
is:
['--hello', 'world']