如何将编译器选项传递给mocha

时间:2013-11-18 14:08:35

标签: coffeescript mocha

我运行mocha命令来运行我的测试

$ ./node_modules/.bin/mocha --compilers coffee:coffee-script -R spec

我希望将其他选项传递给coffee-script编译器( - 以避免在将.coffee编译为.js时引入的外部闭包)。有没有办法做到这一点?我试过了

$ ./node_modules/.bin/mocha --compilers coffee:coffee-script --bare -R spec

但这看起来并不合适。它也没有说--bare不是摩卡的有效选择。

  error: unknown option `--bare'

3 个答案:

答案 0 :(得分:32)

--compiler选项不支持此功能,但您可以编写一个脚本,使用您的选项激活编译器,然后使用mocha' s --require选项激活您的注册脚本。例如,在名为babelhook.js的项目根目录下创建一个文件:

// This file is required in mocha.opts
// The only purpose of this file is to ensure
// the babel transpiler is activated prior to any
// test code, and using the same babel options

require("babel-register")({
  experimental: true
});

然后将其添加到mocha.opts:

--require babelhook

就是这样。在进行任何测试之前,摩卡将需要babelhook.js。

答案 1 :(得分:27)

只需在您的根目录中添加.babelrc文件即可。 那么babel的任何实例(构建,运行时,测试等)都会引用它。 https://babeljs.io/docs/usage/babelrc/

您甚至可以为每个环境添加特定的配置选项。

答案 2 :(得分:4)

如果有人绊倒了这个。 babel的“实验性”选项已被弃用。你的'babelhook.js'现在应该是:

// This file is required in mocha.opts
// The only purpose of this file is to ensure
// the babel transpiler is activated prior to any
// test code, and using the same babel options

require("babel/register")({
  stage: 1
});
相关问题