Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用CoffeeScript运行Mocha_Coffeescript_Mocha.js - Fatal编程技术网

无法使用CoffeeScript运行Mocha

无法使用CoffeeScript运行Mocha,coffeescript,mocha.js,Coffeescript,Mocha.js,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 sr

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

例如:咖啡-内容

describe "feature", ->
   it "should add two numbers", ->
       (2+2).should.equal 4
运行
进行测试时,出现以下错误:

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)
cribe'feature',
^^^^^^^^^
node.js:201
抛出e;//process.nextTick错误,或第一次勾选时的“error”事件
^
SyntaxError:意外的字符串
在模块处编译(Module.js:429:25)
at Object..js(module.js:459:10)
在Module.load(Module.js:348:31)
at功能。加载(module.js:308:12)
at Module.require(Module.js:354:17)
根据需要(模块js:370:17)
at/home/my_username/testcode/coffeepress/node_modules/mocha/bin/_mocha:261:27
at Array.forEach(本机)
加载时(/home/my_username/testcode/coffeepress/node_modules/mocha/bin/_mocha:258:9)
反对。(/home/my_username/testcode/coffeepress/node_modules/mocha/bin/_mocha:249:1)
在模块处编译(Module.js:441:26)
at Object..js(module.js:459:10)
在Module.load(Module.js:348:31)
at功能。加载(module.js:308:12)
at Array.0(module.js:479:10)
在EventEmitter上进行回调(node.js:192:40)
使用js文件运行Mocha成功,但无法使用CoffeeScript运行。我真的很想——为了代码的简洁

请导游

从摩卡1.0开始:

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

(引用)因此,您需要添加行

--编译器咖啡:咖啡脚本/寄存器

或者,对于CoffeeScript 1.7之后的CS,选项应为:

--compilers coffee:coffee-script/register

Mocha的github网站上提交了一个文件。

我需要对我的Mocha args进行两次更改才能使其正常工作:

--require coffee-script/register
--compilers coffee:coffee-script/register
mocha——需要咖啡脚本/注册表


来源:

显然,2018年4月对摩卡咖啡的一项改动(温和地)否决了
--compilers
选项。在命令行中,您现在可以看到:

(节点:27864)弃用警告:“--编译器”将在未来版本的Mocha中删除;有关更多信息,请参阅

正如链接所说,只要不使用
--compilers
和使用这个新的(简化的)
mocha.opts
选项,就可以很容易地解决这个问题:

--require coffeescript/register

test/*.coffee

最后一行是为了让摩卡明白它现在应该使用
*.coffee
文件作为测试文件。这似乎不包括在
--require
选项中。

随着摩卡的最新更新,require语句必须写入package.json文件中,如下所示:

  "mocha":{
    "require":"coffeescript",
    "reporter":"spec"
  },

可能会更新此版本以反映v2以后的版本,从而反映出缺少破折号的情况
coffee:coffeescript/register
为Med工作取决于您是否从npm安装了较旧的
coffeescript
或新的
coffeescript
(无破折号),您将需要
--需要coffeescript/register
--需要coffeescript/register
  "mocha":{
    "require":"coffeescript",
    "reporter":"spec"
  },