无法使用CoffeeScript运行Mocha

Sam*_*eet 43 mocha.js coffeescript

Makefile - 内容:

REPORTER = dot

all: build

build:
    @./node_modules/coffee-script/bin/coffee \
        -c \
        -o lib src

clean:
    rm -rf lib
    mkdir lib

watch:
    @./node_modules/coffee-script/bin/coffee \
        -o lib \
        -cw src

test:
    @./node_modules/mocha/bin/mocha \
        --reporter $(REPORTER) \
        test/*.coffee

.PHONY: build clean watch test
Run Code Online (Sandbox Code Playgroud)

项目根目录有一个包含两个文件的测试文件夹:mocha.opts和example.coffee

example.coffee - 内容

describe "feature", ->
   it "should add two numbers", ->
       (2+2).should.equal 4
Run Code Online (Sandbox Code Playgroud)

运行时make test,出现以下错误:

cribe 'feature',
      ^^^^^^^^^

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
SyntaxError: Unexpected string
    at Module._compile (module.js:429:25)
    at Object..js (module.js:459:10)
    at Module.load (module.js:348:31)
    at Function._load (module.js:308:12)
    at Module.require (module.js:354:17)
    at require (module.js:370:17)
    at /home/my_username/testcode/coffeepress/node_modules/mocha/bin/_mocha:261:27
    at Array.forEach (native)
    at load (/home/my_username/testcode/coffeepress/node_modules/mocha/bin/_mocha:258:9)
    at Object.<anonymous> (/home/my_username/testcode/coffeepress/node_modules/mocha/bin/_mocha:249:1)
    at Module._compile (module.js:441:26)
    at Object..js (module.js:459:10)
    at Module.load (module.js:348:31)
    at Function._load (module.js:308:12)
    at Array.0 (module.js:479:10)
    at EventEmitter._tickCallback (node.js:192:40)
Run Code Online (Sandbox Code Playgroud)

使用js文件运行Mocha成功,但无法使用CoffeeScript运行它.我真的很想 - 为了代码简洁.

请指导.

Tre*_*ham 89

截至摩卡1.0:

开箱即用的咖啡脚本不再受支持.可以通过映射文件扩展名(与--watch一起使用)和模块名称来使用CS和类似的转换器.例如,--compilers coffee:coffee-script使用CoffeeScript 1.6或--compilers coffee:coffee-script/registerCoffeeScript 1.7+.

(引用http://visionmedia.github.io/mocha/#compilers-option)所以,你需要添加一行

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

或者,对于CS <= 1.6.x,

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

到你的mocha.opts文件.


Lou*_*uis 28

从CoffeeScript 1.7开始,选项应该是:

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

在Mocha的github网站上提出了一个问题.