大口喝摩卡如何传递编译器标志

gulp-mocha how to pass the compilers flag?

本文关键字:何传递 编译器 标志 摩卡      更新时间:2023-09-26

我正在尝试使用gulp-mocha模块,但无法找到传递编译器标志的好方法。有没有办法把它包括在我的吞咽任务中?也许是在一个单独的管道里?

例如,如果从命令行运行摩卡(效果良好)
mocha --compilers .:my_compiler.js test/**/*.js

例如,如果使用gulp-mocha(但在哪里可以指定编译器)?

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

我在gulp-mocha插件下看不到编译器选项,所以我想我需要通过管道添加文本来添加编译器?

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

最重要的答案依赖于使用require钩子。这只会在当前进程中起作用,如果您在单独的进程中运行Mocha测试(如gulp-spawn-mocha),则不会起作用。

这就是将编译器传递到mocha模块的方式:

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

Mocha将循环通过compilers属性的元素,并在:上拆分。它将把前面的字符串作为后面的扩展,并将后面的所有内容注入到require()语句中。

在gullfile 的开头使用require('babel-core/register');

我刚刚注意到文档处于底部状态-

对于CoffeeScript支持,请在CoffeeSript 1.6中添加require("ffee-script"),或在CoffeyScript 1.7+.中添加require("咖啡脚本/注册")

我在我的gump文件require('./my_compiler');的顶部添加了一个针对我自己编译器的require语句,这似乎很有效。

对于现在尝试它的人

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);
});

并且在gull文件(gullfile.js)之上

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

运行你的测试,它应该工作

gulp test-mocha
Justin Maat,您不需要修改您的gulpfile.js。当使用CLI:中的gulp时,您只需要使用--require
### before
gulp test
### after
gulp --require babel/register test

你注意到区别了吗?为了节省您的一些按键,请将其添加到您的.bashrc.zshrc:

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

下次,您可以使用gulp作为正常

gulp test