Node.js 告诉Mocha默认使用CoffeeScript文件

Node.js 告诉Mocha默认使用CoffeeScript文件,node.js,makefile,mocha.js,zappa,Node.js,Makefile,Mocha.js,Zappa,我目前正试图在Mocha中为我正在使用Zappa.js编写的应用程序设置测试。到目前为止,我一直在关注,并将我需要的东西从JS转换为Coffeescript 然而,我有点被试着运行测试卡住了。我有一个Makefile,当前看起来像这样: REPORTER = dot test: @NODE_ENV=test ./node_modules/.bin/mocha \ --reporter $(REPORTER) \ .PHONY: test #Cakefile {exec} =

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

然而,我有点被试着运行测试卡住了。我有一个Makefile,当前看起来像这样:

REPORTER = dot

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

.PHONY: test
#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
我已经设置了package.json文件来运行如下测试:

{
  "scripts": {
    "test": "make test"
  }
}
我发现的问题是,因为我试图使用Coffeescript编写我的Mocha测试,所以当我运行“npm测试”时,Mocha不会在“test/”文件夹中选择我的任何测试。我知道一个事实,我可以告诉Mocha通过在终端中使用以下命令来运行.coffee文件(该命令有效):


我想知道的是,我该如何告诉摩卡在默认情况下使用Coffeescript文件?

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

注意:对于CoffeeScript 1.7+--需要咖啡脚本需要更改为--需要咖啡脚本/寄存器

解决方案是创建Cakefile,而不是Makefile,如下所示:

REPORTER = dot

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

.PHONY: test
#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
然后将package.json更改为:

#package.json

{
  "scripts": {
    "test": "cake test"
  }
}
最后,我不得不使用以下方法将Coffeescript安装到项目中:

npm install coffee-script

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

下面是一个工作的Makefilepackage.json

Makefile:

REPORTER = dot
COMPILER = coffee:coffee-script

node_modules:
    @npm install

test: node_modules
    @./node_modules/.bin/mocha --reporter $(REPORTER) --compilers $(COMPILER)

clean: node_modules
    @$(RM) -r node_modules

.PHONY: clean test
  "devDependencies": {
    "coffee-script": "~1.6.3",
    "chai": "~1.7.2",
    "mocha": "~1.12.0"
  }
% make clean
% make test
package.json(仅限devdependences):

REPORTER = dot
COMPILER = coffee:coffee-script

node_modules:
    @npm install

test: node_modules
    @./node_modules/.bin/mocha --reporter $(REPORTER) --compilers $(COMPILER)

clean: node_modules
    @$(RM) -r node_modules

.PHONY: clean test
  "devDependencies": {
    "coffee-script": "~1.6.3",
    "chai": "~1.7.2",
    "mocha": "~1.12.0"
  }
% make clean
% make test
然后执行:

REPORTER = dot
COMPILER = coffee:coffee-script

node_modules:
    @npm install

test: node_modules
    @./node_modules/.bin/mocha --reporter $(REPORTER) --compilers $(COMPILER)

clean: node_modules
    @$(RM) -r node_modules

.PHONY: clean test
  "devDependencies": {
    "coffee-script": "~1.6.3",
    "chai": "~1.7.2",
    "mocha": "~1.12.0"
  }
% make clean
% make test

我直接使用npm配置mocha测试

package.json(仅限脚本)

然后通过运行

npm test


如果您希望贡献者能够执行您的测试,您需要执行
npm安装CoffeeScript--save dev
For CoffeeScript 1.7+
--require CoffeeScript
需要更改为
--require CoffeeScript/register
这在使用
--require CoffeeScript/register