Issue running karma task from gulp

前端 未结 10 2209
隐瞒了意图╮
隐瞒了意图╮ 2020-12-25 09:44

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

Error: 1
   at formatError (C:\\Users\\Tim\\AppData\\Roaming\\npm\\node_modules\\g         


        
相关标签:
10条回答
  • 2020-12-25 09:54

    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-util and updating the task to the below fix the error output very nicely, while maintaining exit status correctly.

    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();
    });
    
    0 讨论(0)
  • 2020-12-25 09:55

    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.

    0 讨论(0)
  • 2020-12-25 09:55

    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:

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

    Note the omission of 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.

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

    0 讨论(0)
  • 2020-12-25 10:01

    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 comment in this github thread.

    gulp.src(src)
        // pipeline...
        .on('error', function (error) {
            console.error('' + error);
        });
    
    0 讨论(0)
  • 2020-12-25 10:03

    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.

    /**
     * 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();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-25 10:10

    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:

    gulp.task('test', function() {
        karma.start({
            configFile: __dirname + '/karma.conf.js',
            singleRun: true
        }, function(karmaExitStatus) {
               if (karmaExitStatus) {
                   process.exit(1);
               }
        });
    });
    
    0 讨论(0)
提交回复
热议问题