George Mauer
George Mauer

Reputation: 122112

Wiring up gulp to run mocha tests written in coffeescript

Stitching together things off of websites I tried this

var gulp = require('gulp');
var gutil = require('gulp-util');
var coffee = require('gulp-coffee');
var mocha = require('gulp-mocha');

gulp.task('test', function() {
    return gulp.src(['tests/*.coffee'], { read: false })
            .pipe(coffee({bare: true}).on('error', gutil.log))
            .pipe(mocha({
                reporter: 'spec',
                globals: {
                    should: require('should')
                }
            }));
});

This always gives me an error on the first line of my tests/test.coffee file

require 'should'
describe "something", ->
    it "fails", -> true.should.equal false

This error:

(function (exports, require, module, __filename, __dirname) { require 'should'
                                                                  ^^^^^^^^

In addition it doesn't seem right to do this in one task. Each one feels like it should be a task that depends on the output of the previous.

How do I make all these pieces come together?

Upvotes: 0

Views: 4607

Answers (2)

Alex Lapa
Alex Lapa

Reputation: 1159

All you need to do to run coffeescript specs is npm install coffee-script. No need to have a gulpfile in coffeescript.

If you have coffeesctipt installed it's enough to have test task like this in your gulpfile:

gulp.task('test', function(){
  return gulp.src('./specs/**/*.spec.coffee')
    .pipe(mocha({reporter:'nyan'}));
});

So, no need to pipe *.coffee files through gulp-mocha

Upvotes: 0

Marko Gresak
Marko Gresak

Reputation: 8217

I have almost identical gulp config as yours, what I did to fix it was:

npm install --save-dev should

The --save-dev part is optional, but I like to store my dependancies into package.json file for easier portability.

Edit:

I have noticed that you're first piping it to coffee. You don't need to use this if using Gulpfile.coffee (you guessed it, Gulpfile written in CoffeeScript), mocha will run it fine as CoffeeScript file.

This is my test task in Gulpfile.coffee:

gulp.task 'test', - >
    gulp.src(testSources,
        read: false
    )
    .pipe(mocha(
        reporter: 'spec'
        globals:
        should: require('should')
    ))

To get Gulp to parse Gulpfile.coffee, save this as Gulpfile.js

require('coffee-script/register');
require('./Gulpfile.coffee');

Edit 2:

I have noticed that the JavaScript version isn't working as expected.

My only suggestion is to use Gulpfile written in CoffeeScript, that way it works fine. I could share mine to speed it up for you, but it takes less than 5 minutes to convert it to CoffeeScript manually, or even faster using Js2coffee.


Or look at this Gulpfile.js (not my work).


Or use Chai and use following code:

chai  = require 'chai'
chai.should() # add should to Object.prototype

describe "Test", ->
    it "should pass", ->
         true.should.equal true

Upvotes: 3

Related Questions