告诉Mocha默认使用CoffeeScript文件

Kyr*_*lia 3 makefile mocha.js node.js zappa

我正在尝试在Mocha中为我正在使用Zappa.js编写的应用程序设置测试.到目前为止,我一直在关注本教程,并将我需要的东西从JS转换为Coffeescript.

但是我试图运行测试时有点困惑.我有一个Makefile,目前看起来像这样:

REPORTER = dot

test:
  @NODE_ENV=test ./node_modules/.bin/mocha \
    --reporter $(REPORTER) \

.PHONY: test
Run Code Online (Sandbox Code Playgroud)

我已经设置了我的package.json文件来运行这样的测试:

{
  "scripts": {
    "test": "make test"
  }
}
Run Code Online (Sandbox Code Playgroud)

我发现的问题是,因为我正在尝试使用Coffeescript编写我的Mocha测试,当我运行"npm test"时,Mocha不会在"test /"文件夹中选择任何测试.我知道一个事实,我可以通过在终端中使用以下内容告诉Mocha运行.coffee文件(有效):

mocha --compilers coffee:coffee-script
Run Code Online (Sandbox Code Playgroud)

我想知道的是如何告诉Mocha默认使用Coffeescript文件?

Kyr*_*lia 5

好的,我设法找到了解决自己问题的方法,所以我想我会分享以防其他人需要这个问题.

注意:对于CoffeeScript 1.7+ - 需要将coffee-script更改为--require coffee-script/register

解决方案是创建一个Cakefile而不是Makefile,它看起来像这样:

#Cakefile

{exec} = require "child_process"

REPORTER = "min"

task "test", "run tests", ->
  exec "NODE_ENV=test
    ./node_modules/.bin/mocha
    --compilers coffee:coffee-script
    --reporter #{REPORTER}
    --require coffee-script
    --require test/test_helper.coffee
    --colors
    ", (err, output) ->
      throw err if err
      console.log output
Run Code Online (Sandbox Code Playgroud)

然后将package.json更改为:

#package.json

{
  "scripts": {
    "test": "cake test"
  }
}
Run Code Online (Sandbox Code Playgroud)

最后我不得不使用以下方法将Coffeescript安装到项目中:

npm install coffee-script
Run Code Online (Sandbox Code Playgroud)

并创建一个文件test/test_helper.coffee,其中包含测试的全局声明.

  • 对于CoffeeScript 1.7+`--require coffee-script`需要更改为`--require coffee-script/register` http://stackoverflow.com/a/9943255/167815 (2认同)