node.js 从 gulp 发出运行 karma 任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26614738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:03:10  来源:igfitidea点击:

Issue running karma task from gulp

node.jsgulpkarma-runner

提问by Timur

I am trying to run karma tests from gulp task and I am getting this error:

我正在尝试从 gulp 任务运行业力测试,但出现此错误:

Error: 1
   at formatError (C:\Users\Tim\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:161:10)
   at Gulp.<anonymous> (C:\Users\Tim\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:187:15)
   at Gulp.emit (events.js:95:17)
   at Gulp.Orchestrator._emitTaskDone (C:\path\to\project\node_modules\gulp\node_modules\orchestrator\index.js:264:8)
   at C:\path\to\project\node_modules\gulp\node_modules\orchestrator\index.js:275:23
   at finish (C:\path\to\project\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:21:8)
   at cb (C:\path\to\project\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:29:3)
   at removeAllListeners (C:\path\to\project\node_modules\karma\lib\server.js:216:7)
   at Server.<anonymous> (C:\path\to\project\node_modules\karma\lib\server.js:227:9)
   at Server.g (events.js:180:16)

My system is Windows 7, nodejs version is v0.10.32, gulp version:

我的系统是Windows 7,nodejs版本是,gulpv0.10.32版本:

[10:26:52] CLI version 3.8.8
[10:26:52] Local version 3.8.9

Also, the same error I am getting on Ubuntu 12.04 LTSwhile on newer Ubuntu (not sure what version) and mac os it is seems to be working ok. What can cause this error?

此外,我Ubuntu 12.04 LTS在较新的 Ubuntu(不确定是什么版本)和 mac os 上遇到的相同错误似乎工作正常。什么会导致此错误?

Update 5/11/2016: Before writing comment about the fact that accepted answer hide errors, please, see first two comments to that particular accepted answer. Use it only if know what you are doing. Related info: https://github.com/karma-runner/gulp-karma/pull/15

2016 年 5 月 11 日更新:在对接受的答案隐藏错误这一事实发表评论之前,请参阅该特定接受答案的前两条评论。只有在知道自己在做什么时才使用它。相关信息:https: //github.com/karma-runner/gulp-karma/pull/15

回答by McDamon

How are you running your tests with Gulp? I came up against this issue recently on OSX, running node v0.11.14and gulp 3.8.10, whenever there were failing tests.

你如何使用 Gulp 运行你的测试?我最近在 OSX 上遇到了这个问题,运行 nodev0.11.14和 gulp 3.8.10,只要有失败的测试。

Changing from the recommended:

从推荐的更改:

gulp.task('test', function(done) {
    karma.start({
        configFile: __dirname + '/karma.conf.js',
        singleRun: true
    }, done);
});

To:

到:

gulp.task('test', function(done) {
    karma.start({
        configFile: __dirname + '/karma.conf.js',
        singleRun: true
    }, function() {
        done();
    });
});

...got rid of this error.

...摆脱了这个错误。

Seems to be down to how gulp handles error messages when an error is signalled in a callback. See Improve error messages on exitfor more information.

似乎归结为在回调中发出错误信号时 gulp 如何处理错误消息。有关更多信息,请参阅改进退出时的错误消息

回答by brocksamson

None of these solutions worked correctly for me using gulp 3.9.1 and karma 1.1.1. Adding a reference to gulp-util npm install --save-dev gulp-utiland updating the task to the below fix the error output very nicely, while maintaining exit status correctly.

使用 gulp 3.9.1 和 karma 1.1.1,这些解决方案都不适合我。添加对 gulp-util 的引用并将npm install --save-dev gulp-util任务更新到以下内容可以很好地修复错误输出,同时正确保持退出状态。

var gutil = require('gulp-util');

gulp.task('test', function (done) {
  new Server({
    configFile: __dirname + '/karma.conf.js',
    singleRun: true
  }, function(err){
        if(err === 0){
            done();
        } else {
            done(new gutil.PluginError('karma', {
                message: 'Karma Tests failed'
            }));
        }
    }).start();
});

回答by Igor Lino

Below is a code snippet from gulp patterns on using Karma. It's a bit similar, but also uses the newer method how to start the karma.

下面是使用 Karma 的 gulp 模式的代码片段。它有点相似,但也使用了较新的方法如何开始业力。

/**
 * Start the tests using karma.
 * @param  {boolean} singleRun - True means run once and end (CI), or keep running (dev)
 * @param  {Function} done - Callback to fire when karma is done
 * @return {undefined}
 */
function startTests(singleRun, done) {
    var child;
    var excludeFiles = [];
    var fork = require('child_process').fork;
    var KarmaServer = require('karma').Server;
    var serverSpecs = config.serverIntegrationSpecs;

    if (args.startServers) {
        log('Starting servers');
        var savedEnv = process.env;
        savedEnv.NODE_ENV = 'dev';
        savedEnv.PORT = 8888;
        child = fork(config.nodeServer);
    } else {
        if (serverSpecs && serverSpecs.length) {
            excludeFiles = serverSpecs;
        }
    }

    var server = new KarmaServer({
        configFile: __dirname + '/karma.conf.js',
        exclude: excludeFiles,
        singleRun: singleRun
    }, karmaCompleted);
    server.start();

    ////////////////

    function karmaCompleted(karmaResult) {
        log('Karma completed');
        if (child) {
            log('shutting down the child process');
            child.kill();
        }
        if (karmaResult === 1) {
            done('karma: tests failed with code ' + karmaResult);
        } else {
            done();
        }
    }
}

回答by Blacksonic

What worked for me and gave a nice formatted error message is to provide an Error instance to the donecallback.

对我有用并给出了一个很好的格式化错误消息的是为done回调提供一个 Error 实例。

gulp.task('test', function(done) {
    karma.start({
        configFile: __dirname + '/karma.conf.js',
        singleRun: true
    }, function(result) {
        if (result > 0) {
            return done(new Error(`Karma exited with status code ${result}`));
        }

        done();
    });
});

回答by user3479425

If you want to return with an error code, and want to see Karma's error output but not Gulp's (probably unrelated) stack trace:

如果您想返回错误代码,并想查看 Karma 的错误输出而不是 Gulp 的(可能不相关的)堆栈跟踪:

gulp.task('test', function() {
    karma.start({
        configFile: __dirname + '/karma.conf.js',
        singleRun: true
    }, function(karmaExitStatus) {
           if (karmaExitStatus) {
               process.exit(1);
           }
    });
});

回答by onfilm

Not sure about Ubuntu, but I was getting a similar error on Windows, and installing one version back fixed it right away like this:

不确定 Ubuntu,但我在 Windows 上遇到了类似的错误,安装一个版本后立即修复它,如下所示:

npm install -g [email protected]
npm install [email protected]

回答by Laurent Picquet

this is gulp's way of telling your tests have failed and that karma exited with a return code of 1. Why you would want to call done yourself and not pass the error as a message baffles me.

这是 gulp 告诉你的测试失败的方式,并且 karma 以返回码 1 退出。为什么你想要自己调用 done 而不是传递错误,因为消息让我感到困惑。

回答by Seth

The right way to solve this according to Karma's documentation and https://github.com/pkozlowski-opensource, is to rely on Karma's watch mechanism rather than Gulp's:

根据 Karma 的文档和https://github.com/pkozlowski-opensource解决这个问题的正确方法是依靠 Karma 的监视机制而不是 Gulp 的:

gulp.task('tdd', function (done) {
  karma.start({
    configFile: __dirname + '/karma.conf.js'
  }, done);
});

Note the omission of singleRun: true.

注意省略singleRun: true

@McDamon's workaround will work for gulp.watch, but you don't want to swallow exit codes like that when running on a CI server.

@McDamon 的解决方法适用于gulp.watch,但您不想在 CI 服务器上运行时吞下这样的退出代码。

Gulp is also reworking how they handle exit codes in scenarios just like this one. See https://github.com/gulpjs/gulp/issues/71and the other dozen or so related issues.

Gulp 也在重新设计他们在这种情况下处理退出代码的方式。请参阅https://github.com/gulpjs/gulp/issues/71和其他十几个相关问题。

回答by mordant23

In case anyone else comes here, do not use the accepted solution. It will hide failed tests. If you need a quick solution to modify your gulp test task, you can use the solution found in this commentin thisgithub thread.

如果其他人来到这里,请不要使用已接受的解决方案。它将隐藏失败的测试。如果你需要一个快速的解决方案来修改你的 gulp 测试任务,你可以使用 在这个github 线程的这个评论中找到的解决方案。

gulp.src(src)
    // pipeline...
    .on('error', function (error) {
        console.error('' + error);
    });

回答by deBrice

gulp.task('test', function(done) {
    karma.start({
        configFile: __dirname + '/karma.conf.js',
        singleRun: false 
    }, done);
});

passing singleRun: falseargument will prevent the process from returning a value different of 0 (which would signify an error and exit gulp).

传递singleRun: false参数将阻止进程返回一个不同于 0 的值(这将表示错误并退出 gulp)。

Run with singleRun: trueif you only launching your test from a command line, not part of a continuous integration suite.

与运行singleRun: true,如果你只从一个命令行,持续集成套件的一部分不启动您的测试。