无法使用CoffeeScript运行Mocha [英] Cannot run Mocha with CoffeeScript

查看:175
本文介绍了无法使用CoffeeScript运行Mocha的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



example.coffee - Content

 描述feature, - > 
it应该添加两个数字, - >
(2 + 2).should.equal 4

运行 make test ,得到以下错误:

  ^^^^^^^^ 

node.js:201
throw e; // process.nextTick错误或第一个标记为error的事件
^
语法错误:意外的字符串
at Module._compile(module.js:429:25)
Object.js(module.js:459:10)
at Module.load(module.js:348:31)
at Function._load(module.js:308:12)
在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 $($)
at load(/ home / my_username / testcode / coffeepress / node_modules / mocha / bin / _mocha:258:9) < anonymous> (/ home / my_username / testcode / coffeepress / node_modules / mocha / bin / _mocha:249:1)
at Module._compile(module.js:441:26)
at Object..js .js:459:10)
at Module.load(module.js:348:31)
at Function._load(module.js:308:12)
at Array.0 module.js:479:10)
在EventEmitter._tickCallback(node.js:192:40)


$ b b

运行Mocha与js文件成功,但不能让它与CoffeeScript一起运行。我真的想 - 代码简洁。



请指导。

解决方案

自Mocha 1.0起:


咖啡脚本不再支持开箱即用。 CS和类似的转码器可以通过映射文件扩展名(用于--watch)和模块名称来使用。例如 - 编译器咖啡:coffee-script with CoffeeScript 1.6-或 - 编译器咖啡:coffee-script / register with CoffeeScript 1.7 +。


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

   - 编译器咖啡:coffee-script / register 

或对于CS <= 1.6.x,

   - 编译器咖啡:coffee-script 
c>

$ <$> $ <$> $ <$> $ <$> $ <$> $ <$> b $ b

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.

解决方案

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.

这篇关于无法使用CoffeeScript运行Mocha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆