【问题标题】:Unexpected '->' when running mocha coffeescript based test under node在节点下运行基于 mocha coffeescript 的测试时出现意外的“->”
【发布时间】:2012-06-22 14:16:59
【问题描述】:

我正在尝试在 Windows 下的节点上运行 mocha。我还决定为什么不把 CoffeeScript 扔进去取乐。

describe 'Array', ->
  describe '#indexOf()', ->
    it 'should return -1 when not present' ->
      [1,2,3].indexOf(4).should.equal -1

问题是我遇到了错误:

C:\Users\ShaneC\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\coffee-script.js:51
      throw err;
            ^
Error: In C:\projects\BowlingKata\test\test.coffee, Parse error on line 3: Unexpected '->'
    at Object.parseError (C:\Users\ShaneC\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\parser.js:477:11)
    at Object.parse (C:\Users\ShaneC\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\parser.js:554:22)
    at C:\Users\ShaneC\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\coffee-script.js:43:20
    at Object..coffee (C:\Users\ShaneC\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\coffee-script.js:19:17)
    at Module.load (module.js:353:31)

这是我的 mocha.opts 文件:

--reporter spec
--ui bdd
-r should
--compilers coffee:coffee-script

关于我可能做错的任何想法?我从http://net.tutsplus.com/tutorials/javascript-ajax/better-coffeescript-testing-with-mocha/ 复制了代码,没有人报告任何问题..

【问题讨论】:

    标签: node.js coffeescript mocha.js


    【解决方案1】:

    假设你正在调用一个函数it,带有参数“不存在时应该返回-1”,然后是一个检查这个的函数,你需要用逗号分隔参数:

    describe 'Array', ->
      describe '#indexOf()', ->
        it 'should return -1 when not present', ->
          [1,2,3].indexOf(4).should.equal -1
    

    这编译为:

    describe('Array', function() {
      return describe('#indexOf()', function() {
        return it('should return -1 when not present', function() {
          return [1, 2, 3].indexOf(4).should.equal(-1);
        });
      });
    });
    

    【讨论】:

    • 这里我忽略了 WebStorm 中的错误,因为我认为它没有正确理解 CofeeScript。谢天谢地,今天是星期五。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-08-07
    • 2016-05-04
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 2022-07-23
    • 1970-01-01
    相关资源
    最近更新 更多