在nodeJS中运行Mocha测试时出错

时间:2014-01-29 12:47:16

标签: node.js express coffeescript mocha

我试图让Mocha在nodejs和expressjs中运行。我的测试如下:

assert  = require 'assert'
request = require 'request'
app = require '../../server'

describe "authentication", ->
  describe "GET /login ", ->
    body = null
    before  (done) ->
        options = 
            uri: "http://localhost:3000/login"
        request options, (err, response, _body) ->
            body = _body
            done()
    it "has user field", ->
        assert.ok /user/.test(body)
        # assert.match body, /user/

我在我的server.js文件中添加了coffee-script作为依赖项:

require('coffee-script');

var express = require('express');
var http = require('http');
var path = require('path');

var app = model.exports = express();

我有一个帮助文件_helper.js:

require('coffee-script')

我运行命令:

  

mocha test / _helper.js test \ apps \ authentication-test.coffee

会出现以下错误:

    (exports, require, module, __filename, __dirname) { assert     = require 'assert'

SyntaxError: Unexpected string
 at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at C:\Program Files\nodejs\node_modules\mocha\lib\mocha.js:172:27
at Array.forEach (native)
at Mocha.loadFiles (C:\Program Files\nodejs\node_modules\mocha\lib\mocha.js:16
9:14)
at Mocha.run (C:\Program Files\nodejs\node_modules\mocha\lib\mocha.js:356:31)
at Object.<anonymous> (C:\Program Files\nodejs\node_modules\mocha\bin\_mocha:3
 59:16)
at Module._compile (module.js:456:26)
at  Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3

任何人都能看到我所缺少的东西?

3 个答案:

答案 0 :(得分:10)

如果你正在使用CoffeeScript 1.7,你需要使用要求coffee-script/register模块来动态地要求和编译.coffee文件(参见changelog)。尝试使用:

运行Mocha
mocha --compilers coffee:coffee-script/register

或修改您的Mocha帮助文件:

require('coffee-script/register')

答案 1 :(得分:1)

默认情况下,Mocha不会检查Coffeescript文件。您必须指定编译器选项:

  mocha --compilers coffee:coffee-script

或添加

  --compilers coffee:coffee-script

在您的mocha.opts文件中

答案 2 :(得分:1)

我需要进行两项更改才能让CoffeeScript与Mocha一起使用:

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