Kyri Elia
Kyri Elia

Reputation: 1007

Tell Mocha to use CoffeeScript files by default

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?

Upvotes: 3

Views: 2545

Answers (3)

Kyri Elia
Kyri Elia

Reputation: 1007

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.

Upvotes: 5

Stuart McConnell
Stuart McConnell

Reputation: 665

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

Upvotes: 4

Wil Moore III
Wil Moore III

Reputation: 7194

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

Upvotes: 0

Related Questions