3

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

但是我有点坚持尝试运行测试。我有一个 Makefile,目前看起来像这样:

REPORTER = dot

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

.PHONY: test

我已经设置了我的 package.json 文件来运行测试,如下所示:

{
  "scripts": {
    "test": "make test"
  }
}

我发现的问题是,因为我也在尝试使用 Coffeescript 编写我的 Mocha 测试,所以当我运行“npm test”时,Mocha 不会在“test/”文件夹中提取我的任何测试。我知道我可以通过在终端中使用以下命令来告诉 Mocha 运行 .coffee 文件(有效):

mocha --compilers coffee:coffee-script

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

4

3 回答 3

5

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

注意:对于 CoffeeScript 1.7+ --require 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

然后将 package.json 更改为:

#package.json

{
  "scripts": {
    "test": "cake test"
  }
}

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

npm install coffee-script

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

于 2013-05-04T10:22:23.673 回答
4

我直接使用 npm 配置 mocha 测试

package.json(仅限脚本)

"scripts": {
  "start": "node app.js",
  "start-watch": "./node_modules/.bin/node-dev app.js",
  "test": "NODE_ENV=test ./node_modules/.bin/mocha --require coffee-script --compilers coffee:coffee-script --recursive ./test",
  "test-watch": "NODE_ENV=test ./node_modules/.bin/mocha --require coffee-script --compilers coffee:coffee-script --recursive ./test --watch"
}

然后通过运行执行测试咖啡脚本文件

npm test

或者

npm run-script test-watch
于 2013-10-15T05:50:20.907 回答
0

下面是一个有效的Makefilepackage.json

生成文件:

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

package.json(仅限 devDependencies):

  "devDependencies": {
    "coffee-script": "~1.6.3",
    "chai": "~1.7.2",
    "mocha": "~1.12.0"
  }

然后做:

% make clean
% make test
于 2013-08-16T07:08:14.480 回答