gulp-mocha how to pass the compilers flag?

对着背影说爱祢 提交于 2019-12-03 12:38:45
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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!