简体   繁体   中英

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?

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

gulp.task('mocha', function() {
    return gulp.src(['test/**/*.js'])
        .pipe(mocha({
            compilers: {
                js: babel
            }
        }));
});

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.

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

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.

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

Justin Maat, you don't need to modify your gulpfile.js . You just need to use --require when using gulp from CLI :

### before
gulp test

### after
gulp --require babel/register test

Do you notice the difference? And to save you a few keystrokes, add this to your .bashrc or .zshrc :

alias gulp='gulp --require babel/register'

and next time, you can use gulp as normal

gulp test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM