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

gpr*_*ant 15 mocha.js coffeescript

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

$ ./node_modules/.bin/mocha --compilers coffee:coffee-script -R spec
Run Code Online (Sandbox Code Playgroud)

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

$ ./node_modules/.bin/mocha --compilers coffee:coffee-script --bare -R spec
Run Code Online (Sandbox Code Playgroud)

但这看起来不对.它也没有说--bare不是摩卡的有效选择.

  error: unknown option `--bare'
Run Code Online (Sandbox Code Playgroud)

Rus*_*gan 33

--compiler选项不支持此功能,但您可以编写一个脚本,使用您的选项激活编译器,然后使用mocha的--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
});
Run Code Online (Sandbox Code Playgroud)

然后将其添加到mocha.opts:

--require babelhook
Run Code Online (Sandbox Code Playgroud)

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

  • 不是babel-register? (2认同)

Wil*_*ern 27

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

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