Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gulp-mocha how to pass the compilers flag?

I'm trying to use the gulp-mocha module but can't figure out a good way to pass over the compilers flag. Is there a way to include this in my gulp task? Maybe in a separate pipe somehow?

Example if running mocha from command line (works fine)
mocha --compilers .:my_compiler.js test/**/*.js

Example if using gulp-mocha (but where can I specify a compiler)?

gulp.task('test', function () {
    gulp.src(["test/**/*.js"], {
        read: false
    })
        .pipe(mocha({
            reporter: 'spec'
        }))
        .pipe(exit());
});

I don't see a compilers option under the gulp-mocha plugin, so I'm thinking I need to somehow add the compilers by appending the text through a pipe somehow?

like image 234
Justin Maat Avatar asked Mar 04 '15 15:03

Justin Maat


5 Answers

var mocha = require('gulp-mocha');
var babel = require('babel/register');

gulp.task('mocha', function() {
    return gulp.src(['test/**/*.js'])
        .pipe(mocha({
            compilers: {
                js: babel
            }
        }));
});
like image 54
Juan Du Avatar answered Oct 21 '22 13:10

Juan Du


The top answer relies on using the require hook. This will only work in the current process, and not if you run Mocha tests in a separate process, as with gulp-spawn-mocha.

This is how you pass compilers into the mocha module:

    return mocha({
        compilers: [
            'js:babel-core/register',
        ]
    });

Mocha will loop through the elements of the compilers property and split on :. It will treat the string before it as the extensions to follow, and will inject everything after it into a require() statement.

like image 38
Morley Zhi Avatar answered Oct 21 '22 12:10

Morley Zhi


Use require('babel-core/register'); at the start of the gulpfile

like image 8
gleb bahmutov Avatar answered Oct 21 '22 14:10

gleb bahmutov


I just noticed the docs at the bottom state -

For CoffeeScript support, add require('coffee-script') with CoffeeScript 1.6- or require('coffee-script/register') with CoffeeScript 1.7+.

I added a require statement for my own compiler at the top of my gulp file require('./my_compiler'); and this seemed to work.

like image 2
Justin Maat Avatar answered Oct 21 '22 13:10

Justin Maat


For anyone trying it now

gulp.task('test-mocha', function() {
    return gulp.src(['tests/acceptance/*.js'], {read: false})
        .pipe(
            mocha({
                compilers: 'js:babel-core/register',
                reporter: 'landing'
            })
        )
        .on('error', gutil.log);
});

and on top of the gulp file (gulpfile.js)

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

run your test and it should work

gulp test-mocha
like image 1
Waku-2 Avatar answered Oct 21 '22 14:10

Waku-2