将参数传递给 AVA 测试文件

Passing arguments to AVA test files

我正在寻找通过命令行将参数传递到我的 ava 测试文件的方法,我找到了这个文档。 https://github.com/avajs/ava/blob/main/docs/recipes/passing-arguments-to-your-test-files.md

// test.js 常量测试=要求('ava');

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

$ npx ava -- --hello world

我想知道这段代码实际上在做什么,但我在网上找不到其他相关主题来讨论这个问题。有谁可以给​​我解释一下吗?

此代码只是向您展示了在节点中 - 因此也在 ava 中,参数在数组 process.argv 中可用。您可以找到它的文档 here

slice(2) 只是去掉数组的前两个元素。从我上面链接的文档来看,这是因为:

The first element will be process.execPath.

The second element will be the path to the JavaScript file being executed

所以你的论点从 process.argv[2] 开始。

t.deepEqual 只是为了说明 reader 即 process.argv.slice(2) 的值为:

 ['--hello', 'world']