how to pass compiler options to mocha

江枫思渺然 提交于 2019-12-21 07:28:39

问题


I run a mocha command to run my tests

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

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

but that doesn't look right. It also failed saying that --bare is not a valid option for mocha.

  error: unknown option `--bare'

回答1:


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
});

Then add this to mocha.opts:

--require babelhook

And that's it. Mocha will require babelhook.js before any tests.




回答2:


Simply add a .babelrc file to your root. Then any instances of babel (build, runtime, testing, etc) will reference that. https://babeljs.io/docs/usage/babelrc/

You can even add specific config options per-environment.




回答3:


In case anyone stumbles upon this. The 'experimental' option in babel has been deprecated. Your 'babelhook.js' should now read:

// 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
});


来源:https://stackoverflow.com/questions/20049790/how-to-pass-compiler-options-to-mocha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!