Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

mocha.js - Cannot run Mocha with CoffeeScript

Makefile - Content:

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

The project root directory has a test folder with two files: mocha.opts and example.coffee

example.coffee - Content

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

On running make test, getting the following error:

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)

Running Mocha with js files succeeds, but cannot get it to run with CoffeeScript. I really want to - for code brevity.

Please guide.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As of 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+.

(Quoting http://visionmedia.github.io/mocha/#compilers-option) So, you need to add the line

--compilers coffee:coffee-script/register

or, for CS <= 1.6.x,

--compilers coffee:coffee-script

to your mocha.opts file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...