2012-03-30 26 views
42

Makefile - Nội dung: thư mục gốc của dự ánKhông thể chạy Mocha với CoffeeScript

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 

có một thư mục thử nghiệm với hai tập tin: mocha.opts và example.coffee

example.coffee - Nội dung

describe "feature", -> 
    it "should add two numbers", -> 
     (2+2).should.equal 4 

Khi chạy make test, nhận được lỗi sau:

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) 

Chạy Mocha với các tệp js thành công, nhưng không thể chạy tệp đó với CoffeeScript. Tôi thực sự muốn - cho ngắn gọn mã.

Vui lòng hướng dẫn.

Trả lời

89

Tính đến Mocha 1.0:

coffee-script is no longer supported out of the box. CS and similar transpilers may be used by mapping the file extensions (for use with --watch) and the module name. For example --compilers coffee:coffee-script with CoffeeScript 1.6- or --compilers coffee:coffee-script/register with CoffeeScript 1.7+.

(Trích dẫn http://visionmedia.github.io/mocha/#compilers-option) Vì vậy, bạn cần phải thêm dòng

--compilers coffee:coffee-script/register 

hoặc, đối với CS < = 1.6.x,

--compilers coffee:coffee-script 

vào tệp mocha.opts của bạn.

28

Từ CoffeeScript 1,7 trở đi, tùy chọn nên là:

--compilers coffee:coffee-script/register 

Một issue được nộp trên trang web của github Mocha của.

+0

lẽ cập nhật này để phản ánh v2 trở đi và do đó thiếu một dấu gạch ngang. 'coffee: coffeescript/register' đã làm việc cho tôi – aaaaaa

0

tôi cần hai thay đổi args mocha của tôi để có được điều này để làm việc:

--require coffee-script/register 
--compilers coffee:coffee-script/register 
Các vấn đề liên quan