2013-05-04 48 views
2

我目前正在尝试在我正在使用Zappa.js编写的应用程序中为摩卡设置测试。到目前为止,我一直在关注this tutorial,并将我需要的JS转换为Coffeescript。告诉摩卡默认使用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 /”文件夹中选择我的任何测试。我知道一个事实,我可以告诉摩卡通过使用终端(工作)以下运行.coffee文件:

mocha --compilers coffee:coffee-script 

我想知道的是我怎么去告诉摩卡使用的CoffeeScript文件默认?

回答

5

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

注:CoffeeScript的1.7+ --require咖啡脚本需要改为--require咖啡脚本/注册

的解决方案是改为建立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,其中包含测试的全局声明。

+0

如果你想贡献者能够执行你的测试执行测试的CoffeeScript文件,你”你需要做'npm install coffee-script --save-dev' – 2013-08-16 05:15:39

+2

对于CoffeeScript 1.7+' - 需要咖啡脚本'需要改为' - 需要咖啡脚本/注册表'http:// stackoverflow。 COM/A/99432 55/167815 – 2014-05-18 10:38:40

0

下面是一个工作的Makefile的package.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 

的package.json(仅devDependencies):

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

然后做:

% make clean 
% make test 
4

我配置摩卡测试直接使用NPM

包。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 
+0

这个**在使用'--require coffee-script/register'时可以工作** – meridius 2016-10-09 14:39:17