如何使用gulp和tape?

4

我正在尝试将Gulp与Tape(https://github.com/substack/tape),即NodeJs测试工具集成。

我该如何做?似乎没有现有的Gulp插件。

我看到了这个,但它看起来非常不优雅:

var shell = require('gulp-shell')

gulp.task('exec-tests', shell.task([
  'tape test/* | faucet',
]));

gulp.task('autotest', ['exec-tests'], function() {
  gulp.watch(['app/**/*.js', 'test/**/*.js'], ['exec-tests']);
});

我尝试了这个,看起来应该有效:
var tape = require('tape');
var spec = require('tap-spec');


gulp.task('test', function() {
    return gulp.src(paths.serverTests, {
            read: false
        })
        .pipe(tape.createStream())
        .pipe(spec())
        .pipe(process.stdout);
});

但是我遇到了一个TypeError:无效的非字符串/缓冲区块错误。

6个回答

4

你的“不够优雅”的答案是最好的。并非每个问题都能通过流来最好地解决,使用gulp作为包装器也不是罪过。


我确认。我尝试了许多其他的“解决方案”,但这是唯一一个在手表内良好运行的。所有其他的,如gulp-tape或使用xargs只能运行一次,然后在由手表触发的第二次运行时崩溃。 - MathieuLescure

2

没错,你的任务不会工作,因为gulp流是基于vinyl的,它是一个虚拟文件抽象层。我认为在gulp中没有好的处理方式,似乎应该直接使用tape API。如果你愿意,你可以在它周围加上一些gulp任务。

var test = require('tape');
var spec = require('tap-spec');
var path = require('path');
var gulp = require('gulp');
var glob = require('glob');

gulp.task('default', function () {
    var stream = test.createStream()
        .pipe(spec())
        .pipe(process.stdout);

    glob.sync('path/to/tests/**/*.js').forEach(function (file) {
        require(path.resolve(file));
    });

    return stream;
});

我觉得这个代码有点混乱,不仅因为我们没有使用gulp的流抽象,而且我们甚至没有将它放到一个可以连接到gulp管道的方式中。此外,使用这段代码时,您也无法获得gulp任务完成消息。如果有人知道解决方法,请告诉我。

我认为我更喜欢在命令行上使用tape。但是,如果您希望在gulpfile中拥有所有构建步骤任务,则可能需要选择这种方法。


1

只需使用以下代码和 gulp tdd 即可使用 tape 进行 TDD :)

const tapNotify = require('tap-notify');
const colorize = require('tap-colorize');
const tape = require('gulp-tape');
const through = require('through2');
gulp.task('test',function(){
    process.stdout.write('\x1Bc');
    const reporter = through.obj();
    reporter.pipe(tapNotify({
        passed: {title: 'ok', wait:false},
        failed: {title: 'missing',wait:false}
    }));
    reporter
        .pipe(colorize())
        .pipe(process.stdout);
    return gulp.src('test/**/*.js')
        .pipe(tape({
            outputStream: through.obj(),
            reporter: reporter
        }));
});
gulp.task('tdd', function() {
    gulp.run('test');
    gulp.watch(['app/scripts/**/*.js*', 'test/**/*.js'],['test']);
});

0
在一个GitHub issue for tape中,jokeyrhyme提到gulp任务可以是Promises,并建议一种使用方法来运行tape测试。基于这个建议,我已经做了以下操作:

gulpfile.babel.js:

import glob from "glob";

gulp.task("test", () => {
    let module = process.argv[process.argv.length - 1];

    return new Promise(resolve => {
        // Crude test for 'gulp test' vs. 'gulp test --module mod'
        if (module !== "test") {
            require(`./js/tape/${module}.js`);
            resolve();
            return;
        }

        glob.sync("./js/tape/*.js").forEach(f => require(f)));
        resolve();
    });
});

看了Ben的回答,我怀疑我所做的并不好,因为我注意到失败的测试并没有导致非零的退出代码(虽然我还没有尝试过Ben的方法来验证是否会这样)。


0

这是我的解决方案的一个例子:

var gulp = require('gulp');
var tape = require('tape');
var File = require('vinyl');
var through = require('through2');
var exec = (require('child_process')).execSync;

function execShell(shcmd, opts) {
    var out = '';
    try {
        out = exec(shcmd, opts);
    } catch (e) {
        if (e.error) throw e.error;
        if (e.stdout) out = e.stdout.toString();
    }
    return out;
};

gulp.task('testreport', function(){
    return gulp.src(
        'testing/specs/tape_unit.js', {read: false}
    ).pipe(
        through.obj(function(file, encoding, next) {
            try{
                // get tape's report
                var tapout = execShell(
                    "./node_modules/.bin/tape " + file.path
                );
                // show the report in a console with tap-spec
                execShell(
                    "./node_modules/.bin/tap-spec", { input: tapout, stdio: [null, 1, 2] }
                );
                // make a json report
                var jsonout = execShell(
                    "./node_modules/.bin/tap-json", { input: tapout }
                );
                // do something with report's object
                // or prepare it for something like Bamboo
                var report = JSON.parse(jsonout.toString());
                // continue the stream with the json report
                next(null, new File({
                    path: 'spec_report.json',
                    contents: new Buffer(JSON.stringify(report, null, 2))
                }));
            }catch(err){ next(err) }
        })
    ).pipe(
        gulp.dest('testing/reports')
    );
});

0
// npm i --save-dev gulp-tape
// npm i --save-dev faucet (just an example of using a TAP reporter)

import gulp from 'gulp';
import tape from 'gulp-tape'; 
import faucet from 'faucet';

gulp.task('test:js', () => {
    return gulp.src('src/**/*test.js')
        .pipe(tape({
            reporter: faucet()
        }));
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接