exec' node app'挂在gulp任务中

时间:2014-08-24 08:41:12

标签: javascript node.js gulp testem

此gulp任务挂起exec('node config/app')行。第一个exec工作正常但第二个只是挂起。

gulp.task('test', function(cb) {
    var exec = require('child_process').exec;

    exec('echo 3', function(err, stdout) {
        console.log(stdout);
    });

    exec('node config/app', function(err, stdout, stderr) {

        console.log(stdout);

        var testemOptions = {
            file: 'testem.json'
        };

        var t = new testem();

        return t.startCI(testemOptions, function() {
            cb();
        });
    });

});

我可以看到输出3但是没有显示第二个console.log的输出。

我在使用testem运行测试之前尝试运行我的服务器。

我尝试过这种类似的解决方案,但它不起作用:Exec not returning anything when trying to run git shortlog with nodejs

此外,我最近还问过一个悬挂的testem gulp任务问题:Testem gulp task hangs after finished

修改

我目前的解决方案是:

gulp.task('test', /*['build'],*/ function(cb) {
    var spawn = require('child_process').spawn;

    var proc = spawn('node', ['config/app']);

    proc.stdout.on('readable', function() {
        var output = proc.stdout.read();

        if (output && output.toString().match('express listening')) {

            var testemOptions = {
                file: 'testem.json'
            };

            var t = new testem();

            t.startCI(testemOptions, function() {
                proc.kill();
                cb();
            });
        }

    });

});

2 个答案:

答案 0 :(得分:3)

如果你想使用testem来测试"节点config / app"服务器,你不能使用exec。

Exec应该在命令完成时回调,所以在你的情况下它永远不会回调。

尝试

gulp.task('test', function(cb) {
    var spawn = require('child_process').spawn;

    var proc = spawn('node', ['config/app']);

    var testStarted = false;

    proc.stdout.on('readable', function() {

        if (testStarted) return;

        testStarted = true;

        var testemOptions = {
           file: 'testem.json'
        };

        var t = new testem();

        t.startCI(testemOptions, function() {
            proc.kill()
            cb();
        });

    }
});

请注意,我没有测试此代码,并且它可能无法处理您可能遇到的所有极端情况(例如,如果服务器意外停止)

您可能还想查看插件https://github.com/sargentsurg/gulp-testem

答案 1 :(得分:0)

github上有ŧestem个插件。