2

I'm trying to get Mocha to run in nodejs and expressjs. My test is as follow:

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/

I have added coffee-script as a dependency in my server.js file:

require('coffee-script');

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

var app = model.exports = express();

And I have a helper file _helper.js:

require('coffee-script')

I run the command:

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

which gives the following error:

    (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

Anyone sees what I'm missing ?

Sam
  • 13,934
  • 26
  • 108
  • 194

3 Answers3

10

If you're using CoffeeScript 1.7, you'll need to use require the coffee-script/register module to be able to require and compile .coffee files on the fly (see changelog). Try running Mocha with:

mocha --compilers coffee:coffee-script/register

Or modifying your Mocha helper file:

require('coffee-script/register')
epidemian
  • 18,817
  • 3
  • 62
  • 71
1

Mocha doesn't check for Coffeescript files by default. You have to specified a compiler option:

  mocha --compilers coffee:coffee-script

or add

  --compilers coffee:coffee-script

in your mocha.opts file

Aurélien Thieriot
  • 5,853
  • 2
  • 24
  • 25
  • I've added the compiler option on the command line as per your recommandation but I'm getting the same error. – Sam Jan 29 '14 at 13:14
  • It's weird, because the error seems to come from the fact that the first line is not in an expected format. Hence the thought about Coffeescript compiler – Aurélien Thieriot Jan 29 '14 at 13:17
1

I needed two changes to get CoffeeScript to work with Mocha:

--require coffee-script/register
--compilers coffee:coffee-script/register
Evan Moran
  • 3,825
  • 34
  • 20