如何将编译器选项传递给mocha [英] how to pass compiler options to mocha
问题描述
我运行mocha命令运行测试
I run a mocha command to run my tests
$ ./node_modules/.bin/mocha --compilers coffee:coffee-script -R spec
我希望传递额外的选项到咖啡脚本编译器只是为了避免在编译.coffee到.js时引入的外部闭包)。有办法做到这一点吗?我尝试了
I wish to pass additional options to the coffee-script compiler (--bare to avoid the outer closure that is introduced when compiling .coffee to .js). Is there a way to do this? I tried
$ ./node_modules/.bin/mocha --compilers coffee:coffee-script --bare -R spec
但看起来不正确。它也失败说--bare不是摩卡的有效选项。
but that doesn't look right. It also failed saying that --bare is not a valid option for mocha.
error: unknown option `--bare'
推荐答案
--compiler选项不支持这个,但是你可以编写一个脚本,激活编译器的选项,然后使用mocha --require选项激活注册脚本。例如,在名为babelhook.js的项目根目录下创建一个文件:
The --compiler option doesn't support this, but you can write a script which activates the compiler with your options, then use mocha's --require option to activate your registration script. For example, create a file at the root of the project called 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:
Then add this to mocha.opts:
--require babelhook
Mocha在任何测试之前都需要babelhook.js。
And that's it. Mocha will require babelhook.js before any tests.
这篇关于如何将编译器选项传递给mocha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!