gulp-mocha how to pass the compilers flag?

二次信任 提交于 2019-12-04 19:01:55

问题


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?


回答1:


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

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



回答2:


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.




回答3:


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




回答4:


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.




回答5:


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



回答6:


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


来源:https://stackoverflow.com/questions/28858957/gulp-mocha-how-to-pass-the-compilers-flag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!