3

I'm currently trying to set up testing in Mocha for an application I'm writing using Zappa.js. So far I've been following this tutorial, and converting what I need from JS to Coffeescript.

However I'm a little stuck with trying to run tests. I have a Makefile, which currently looks like this:

REPORTER = dot

test:
  @NODE_ENV=test ./node_modules/.bin/mocha \
    --reporter $(REPORTER) \

.PHONY: test

And I've set up my package.json file to run tests like so:

{
  "scripts": {
    "test": "make test"
  }
}

The issue I'm finding is that, because I'm trying to write my Mocha tests using Coffeescript as well, Mocha does not pick up any of my tests in the "test/" folder when I run "npm test". I know for a fact that I can tell Mocha to run .coffee files by using the following in Terminal (which works):

mocha --compilers coffee:coffee-script

What I want to know is how do I go about telling Mocha to use Coffeescript files by default?

Kyri Elia
  • 997
  • 9
  • 18

3 Answers3

5

OK I managed to find a way to solve my own question, so I thought I'd share in case anyone else need this.

NOTE: For CoffeeScript 1.7+ --require coffee-script needs to be changed to --require coffee-script/register

The solution is to instead create a Cakefile as opposed to a Makefile, which looks like this:

#Cakefile

{exec} = require "child_process"

REPORTER = "min"

task "test", "run tests", ->
  exec "NODE_ENV=test
    ./node_modules/.bin/mocha
    --compilers coffee:coffee-script
    --reporter #{REPORTER}
    --require coffee-script
    --require test/test_helper.coffee
    --colors
    ", (err, output) ->
      throw err if err
      console.log output

And then change the package.json to this:

#package.json

{
  "scripts": {
    "test": "cake test"
  }
}

Finally I had to install Coffeescript into the project using:

npm install coffee-script

And create a file test/test_helper.coffee, which contains global declarations for the tests.

jcalfee314
  • 4,642
  • 8
  • 43
  • 75
Kyri Elia
  • 997
  • 9
  • 18
  • If you want contributors to be able to execute your tests, you'll want to do `npm install coffee-script --save-dev` – Wil Moore III Aug 16 '13 at 05:15
  • 2
    For CoffeeScript 1.7+ `--require coffee-script` needs to be changed to `--require coffee-script/register` http://stackoverflow.com/a/9943255/167815 – Dave James Miller May 18 '14 at 10:38
4

I configure the mocha tests directly using npm

package.json (scripts only)

"scripts": {
  "start": "node app.js",
  "start-watch": "./node_modules/.bin/node-dev app.js",
  "test": "NODE_ENV=test ./node_modules/.bin/mocha --require coffee-script --compilers coffee:coffee-script --recursive ./test",
  "test-watch": "NODE_ENV=test ./node_modules/.bin/mocha --require coffee-script --compilers coffee:coffee-script --recursive ./test --watch"
}

and then execute the test coffeescript files by running

npm test

or

npm run-script test-watch
0

Below is a working Makefile and package.json

Makefile:

REPORTER = dot
COMPILER = coffee:coffee-script

node_modules:
    @npm install

test: node_modules
    @./node_modules/.bin/mocha --reporter $(REPORTER) --compilers $(COMPILER)

clean: node_modules
    @$(RM) -r node_modules

.PHONY: clean test

package.json (devDependencies only):

  "devDependencies": {
    "coffee-script": "~1.6.3",
    "chai": "~1.7.2",
    "mocha": "~1.12.0"
  }

Then do:

% make clean
% make test
Wil Moore III
  • 6,968
  • 3
  • 36
  • 49