44

Makefile - Content:

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

The project root directory has a test folder with two files: mocha.opts and example.coffee

example.coffee - Content

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

On running make test, getting the following error:

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)

Running Mocha with js files succeeds, but cannot get it to run with CoffeeScript. I really want to - for code brevity.

Please guide.

Sameet
  • 2,191
  • 7
  • 28
  • 55

6 Answers6

88

As of Mocha 1.0:

coffee-script is no longer supported out of the box. CS and similar transpilers may be used by mapping the file extensions (for use with --watch) and the module name. For example --compilers coffee:coffee-script with CoffeeScript 1.6- or --compilers coffee:coffee-script/register with CoffeeScript 1.7+.

(Quoting http://visionmedia.github.io/mocha/#compilers-option) So, you need to add the line

--compilers coffee:coffee-script/register

or, for CS <= 1.6.x,

--compilers coffee:coffee-script

to your mocha.opts file.

epidemian
  • 18,817
  • 3
  • 62
  • 71
Trevor Burnham
  • 76,828
  • 33
  • 160
  • 196
28

From CoffeeScript 1.7 onwards, the option should be:

--compilers coffee:coffee-script/register

An issue was filed on Mocha's github site.

Louis
  • 146,715
  • 28
  • 274
  • 320
  • Maybe update this to reflect v2 onwards and thus the lack of a dash. `coffee:coffeescript/register` worked for me – aaaaaa Jan 23 '18 at 19:52
2

Apparently, a change in Mocha made in April 2018 (softly) deprecated the --compilers option. In the command line you now get:

(node:27864) DeprecationWarning: "--compilers" will be removed in a future version of Mocha; see https://git.io/vdcSr for more info

Like the link says, this can easily be fixed by just not using --compilers and using this new (simplified) mocha.opts options:

--require coffeescript/register

test/*.coffee

The last line is needed to make Mocha understand it should now use *.coffee files as test files. This seems not to be covered with the --require option.

palsch
  • 5,528
  • 4
  • 21
  • 32
  • Depending on whether you have the older `coffee-script` or the new `coffeescript` (no dash) installed from npm, you will need `--require coffee-script/register` or `--require coffeescript/register`. – palsch Jun 10 '18 at 00:00
1

mocha --require coffeescript/register

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

André Willik Valenti
  • 1,702
  • 14
  • 21
1

with the latest update of mocha the the require statement must written in package.json file as

  "mocha":{
    "require":"coffeescript",
    "reporter":"spec"
  },
5warag
  • 46
  • 2
0

I needed two changes to my mocha args to get this to work:

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