问题描述
我运行 mocha 命令来运行我的测试
I run a mocha command to run my tests
$ ./node_modules/.bin/mocha --compilers coffee:coffee-script -R spec
我希望将其他选项传递给咖啡脚本编译器(--bare 以避免将 .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 不是 mocha 的有效选项.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!