2

我的 nodeJS 有以下脚本。

"scripts": {
    "start": "grunt",
    "test": "node --debug --harmony node_modules/grunt-cli/bin/grunt test"
}

我正在运行节点 v0.11.13,所以我需要设置 --harmony 标志。在 grunt 上,如果我使用 npm test 启动测试,则测试配置正确,但我更愿意将它们全部放在 gruntfile 中。有没有办法配置 grunt 来启动服务器并运行测试?

4

1 回答 1

3

您可以创建一个别名任务,使用这些节点标志生成 grunt,如下所示:

grunt.registerTask('debug', function() {
  var done = this.async();
  // Specify tasks to run spawned
  var tasks = Array.prototype.slice.call(arguments, 0);
  grunt.util.spawn({
    // Use the existing node path
    cmd: process.execPath,
    // Add the flags and use process.argv[1] to get path to grunt bin
    args: ['--debug', '--harmony', process.argv[1]].concat(tasks),
    // Print everything this process is doing to the parent stdio
    opts: { stdio: 'inherit' }
  }, done);
});

然后你可以启动服务器并运行测试:grunt default debug:test

或者真的是任何组合:

  • grunt server test(在没有节点标志的情况下运行)
  • grunt debug:server:test(使用节点标志运行两者)。
于 2014-07-17T06:54:50.017 回答