2013-05-04 47 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