如何将 arguments/parameters 传递给通过 Grunt 调用的摩卡测试

How to pass arguments/parameters to mocha tests invoked via Grunt

我有一个 Gruntfile.js,通过它我使用 g运行t-mocha-test 模块调用 mochaTest。我可以从命令行将 argument/parameter 传递给 g运行tTask,但我正在努力通过上述模块将相同的参数传递到规范文件 运行ning。代码如下所示,

mochaTest: {
  test: {
    options: {
      reporter: 'spec',
      quiet: false,
      clearRequireCache: false,
      clearCacheFilter: (key) => true,
      noFail: false
    },
    src: [
          'test/createSpec.js'
         ]
  }
}

任务注册如下,

grunt.registerTask('e2etest', function(scope) {
  console.log(scope); // logs user/session based on the parameter passed
  grunt.task.run('mochaTest');
});
// Above task is invoked like,
grunt e2etest:user
(or)
grunt e2etest:session

我需要将此值 (user/session) 传递到 mochaTest 中,以便可以在规范文件中访问它。从根本上说,目标是 运行 用户和会话的 createSpec.js 文件,这个值在规范文件中被参数化,并且基于传递给套件的值 运行.

有没有可能做到这一点?请指教

您可以利用节点 process.argv 从名为 createSpec.js.

的文件中读取参数(即 usersession

要更好地了解操作方法,请按照以下步骤操作:

  1. createSpec.js 的顶部添加以下代码行:

    console.log(process.argv);
    
  2. 然后 运行 grunt e2etest:user 通过您的 CLI,您应该会看到以下内容记录到您的控制台:

    [ 'node', '/usr/local/bin/grunt', 'e2etest:user' ]

    注意:您需要的信息位于数组的第二个索引处。

  3. 现在,从 createSpec.js.

  4. 中删除我们刚刚添加的 console.log(process.argv);

createSpec.js

因此,上面的步骤 (1-3) 说明了可以使用 process.argvcreateSpec.js 中访问参数(usersession)。在这种情况下,您可以在 createSpec.js.

中执行类似以下操作
const argument = process.argv[2].split(':').pop();

if (argument === 'user') {
  // Run `user` specific tests here...
} else if (argument === 'session') {
  // Run `session` specific tests here...
}

注意,我们使用 process.argv[2].split(/:/).pop(); 从位于索引二的数组项中提取 usersession,其初始值为 e2etest:user或分别 e2etest:session


G运行t文件

您的 createSpec.js 文件现在在某种程度上取决于正确调用名为 e2etest 的 g运行t 任务。例如,如果用户要 运行 grunt e2etest 而不提供参数,那么 createSpec.js 不会做太多事情。

要强制正确使用 e2etest 任务(即必须 运行 使用 grunt e2etest:usergrunt e2etest:session),您可以在您的Gruntfile如下:

grunt.registerTask('e2etest', function(scope) {
  if (!scope || !(scope === 'user' || scope === 'session')) {
    grunt.warn(`Must be invoked with: ${this.name}:user or ${this.name}:session`);
  }
  grunt.task.run('mochaTest');
});

上面的要点最初检查是否提供了参数并且是 usersession。如果参数不正确或丢失,则使用 grunt.warn 警告用户。

如果你的nodejs版本不支持ES6 Template literals那么使用grunt.warn代替:

grunt.warn('Must be invoked with: ' + this.name + ':user or ' + this.name + ':session');

补充评论

如果您的用例与您在问题中提到的完全一致,则上面 createSpec.js 部分中显示的 code/gist 将起作用。 IE。您可以使用 grunt e2etest:usergrunt e2etest:session 通过命令行调用。但是,如果这种情况发生变化并且您不能保证 e2etest:usere2etest:session 将准确定位在 process.argv 数组的索引二处,那么您可能需要在 createSpec.js 改为:

// Return the value in the array which starts with
// `e2etest` then extract the string after the colon `:`
const argument = process.argv.filter(arg => {
  return arg.match(/^e2etest:.*/);
}).toString().split(':').pop();

if (argument === 'user') {
  // Run `user` specific tests here...
} else if (argument === 'session') {
  // Run `session` specific tests here...
}

详情请参考此issue,我认为您需要的解决方案是:

node <node flags here> node_modules/mocha/bin/_mocha <mocha arguments here>