【问题标题】:Cannot run Mocha with CoffeeScript无法使用 CoffeeScript 运行 Mocha
【发布时间】:2012-04-14 00:18:02
【问题描述】:

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

项目根目录有一个test文件夹,里面有两个文件:mocha.opts和example.coffee

example.coffee - 内容

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

在运行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)

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

请指导。

【问题讨论】:

    标签: coffeescript mocha.js


    【解决方案1】:

    对于 mocha 的最新更新,require 语句必须写在 package.json 文件中

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

    【讨论】:

      【解决方案2】:

      显然,2018 年 4 月对 Mocha 所做的更改(温和地)弃用了 --compilers 选项。在命令行中你现在得到:

      (node:27864) DeprecationWarning: "--compilers" 将在 Mocha 的未来版本中删除;请参阅https://git.io/vdcSr 了解更多信息

      就像链接说的那样,这可以通过不使用 --compilers 并使用这个新的(简化的)mocha.opts 选项来轻松解决:

      --require coffeescript/register
      
      test/*.coffee
      

      最后一行需要让 Mocha 理解它现在应该使用 *.coffee 文件作为测试文件。 --require 选项似乎没有涵盖这一点。

      【讨论】:

      • 根据您是从 npm 安装旧的 coffee-script 还是新的 coffeescript(无破折号),您将需要 --require coffee-script/register--require coffeescript/register
      【解决方案3】:

      mocha --require coffeescript/register

      来源:https://github.com/mochajs/mocha/wiki/compilers-deprecation

      【讨论】:

        【解决方案4】:

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

        --require coffee-script/register
        --compilers coffee:coffee-script/register
        

        【讨论】:

          【解决方案5】:

          从 Mocha 1.0 开始:

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

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

          --compilers coffee:coffee-script/register
          

          或者,对于 CS

          --compilers coffee:coffee-script
          

          到您的mocha.opts 文件。

          【讨论】:

            【解决方案6】:

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

            --compilers coffee:coffee-script/register
            

            在 Mocha 的 github 网站上提交了一个 issue

            【讨论】:

            • 也许更新它以反映 v2 以后的版本,因此缺少破折号。 coffee:coffeescript/register 为我工作
            猜你喜欢
            • 2023-03-23
            • 2013-04-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-09-05
            • 1970-01-01
            • 1970-01-01
            • 2011-10-21
            相关资源
            最近更新 更多