gulp-mocha how to pass the compilers flag?

后端 未结 6 1095
[愿得一人]
[愿得一人] 2021-02-14 09:28

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

相关标签:
6条回答
  • 2021-02-14 10:07

    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
    
    0 讨论(0)
  • 2021-02-14 10:09

    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
    
    0 讨论(0)
  • 2021-02-14 10:11

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

    0 讨论(0)
  • 2021-02-14 10:17
    var mocha = require('gulp-mocha');
    var babel = require('babel/register');
    
    gulp.task('mocha', function() {
        return gulp.src(['test/**/*.js'])
            .pipe(mocha({
                compilers: {
                    js: babel
                }
            }));
    });
    
    0 讨论(0)
  • 2021-02-14 10:22

    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.

    0 讨论(0)
  • 2021-02-14 10:29

    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.

    0 讨论(0)
提交回复
热议问题