Gulp退出错误,Karma测试失败

时间:2015-11-18 12:34:25

标签: angularjs gulp karma-jasmine gulp-karma

测试AngularJS应用程序相当新,我使用的是Gulp(4.0),AngularJS(1.4.5),Karma(0.13.15)和Jasmine(2.3.4)的混合物。我正在使用OS X(10.11.1)。

成功后,测试运行并且Gulp顺利退出

$ gulp test-directives
[12:56:20] Using gulpfile ~/Workspace/liv/gulpfile.js
[12:56:20] Starting 'test-directives'...
18 11 2015 12:56:20.487:INFO [karma]: Karma v0.13.15 server started at http://localhost:9876/
18 11 2015 12:56:20.503:INFO [launcher]: Starting browser PhantomJS
18 11 2015 12:56:22.147:INFO [PhantomJS 1.9.8 (Mac OS X 0.0.0)]: Connected on socket neBNpf0iNbUVgxlcAAAA with id 50015324
PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 1 of 1 SUCCESS (0.003 secs / 0.002 secs)
[12:56:22] Finished 'test-directives' after 1.91 s

但是,当expect未验证时,Gulp会以错误退出

$ gulp test-directives
[12:56:34] Using gulpfile ~/Workspace/liv/gulpfile.js
[12:56:34] Starting 'test-directives'...
18 11 2015 12:56:34.505:INFO [karma]: Karma v0.13.15 server started at http://localhost:9876/
18 11 2015 12:56:34.526:INFO [launcher]: Starting browser PhantomJS
18 11 2015 12:56:36.173:INFO [PhantomJS 1.9.8 (Mac OS X 0.0.0)]: Connected on socket h_NZdyzooHzb88VdAAAA with id 85829482
PhantomJS 1.9.8 (Mac OS X 0.0.0) field.js › livField should pass as it is a simple test FAILED
    Expected true to equal false.
        at /Users/angrybacon/Workspace/liv/src/scripts/ui/field.spec.js:12
PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.003 secs / 0.002 secs)
[12:56:36] 'test-directives' errored after 1.94 s
[12:56:36] Error: 1
    at formatError (/Users/angrybacon/Workspace/liv/node_modules/gulp-cli/lib/versioned/^4.0.0-alpha.1/formatError.js:20:10)
    at Gulp.<anonymous> (/Users/angrybacon/Workspace/liv/node_modules/gulp-cli/lib/versioned/^4.0.0-alpha.1/log/events.js:26:15)
    at emitOne (events.js:82:20)
    at Gulp.emit (events.js:169:7)
    at Object.error (/Users/angrybacon/Workspace/liv/node_modules/undertaker/lib/helpers/createExtensions.js:58:10)
    at handler (/Users/angrybacon/Workspace/liv/node_modules/now-and-later/lib/map.js:46:14)
    at f (/Users/angrybacon/Workspace/liv/node_modules/once/once.js:17:25)
    at f (/Users/angrybacon/Workspace/liv/node_modules/once/once.js:17:25)
    at done (/Users/angrybacon/Workspace/liv/node_modules/async-done/index.js:24:15)
    at removeAllListeners (/Users/angrybacon/Workspace/liv/node_modules/karma/lib/server.js:332:7)
    at Server.<anonymous> (/Users/angrybacon/Workspace/liv/node_modules/karma/lib/server.js:343:9)
    at Server.g (events.js:260:16)
    at emitNone (events.js:72:20)
    at Server.emit (events.js:166:7)
    at emitCloseNT (net.js:1521:8)
    at doNTCallback1 (node.js:439:9)

我使用gulpfile.js和3个部分:build.js,dist.js和test.js

// gulpfile.js

(function() {
    'use strict';

    var glob = require('glob');
    var gulp = require('gulp');
    var extend = require('extend');
    var fs = require('fs');
    var karma = require('karma');

    function getTask(task, dependencies) {
        dependencies = dependencies || [];
        dependencies.unshift(gulp);
        return require('./gulp_tasks/' + task).apply(this, dependencies);
    }

    var test = getTask('test', [extend, fs, glob, karma]);
    gulp.task('test-directives', test.directives);
})();
// gulp_tasks/test.js

(function() {
    'use strict';

    module.exports = function(gulp, extend, fs, glob, karma) {

        // NOTE: The glob patterns to get the specs from
        var DIRECTORIES = {

            // Directives
            directives: {
                field: ['src/scripts/ui/field.spec.js']
            }
        };

        // NOTE: Extend Karma server's default options
        var OPTIONS = {
            autoWatch: false,
            configFile: __dirname + '/../karma.conf.js',
            files: [
                'bower_components/angular/angular.js',
                'bower_components/angular-mocks/angular-mocks.js'
            ],
            singleRun: true
        };

        // NOTE: Compute list of files to import, start the Karma server, run the tests
        function runSuite(parameters) {

            // Required parameters
            var suite = parameters.suite;
            var done = parameters.done;
            if (typeof suite === 'undefined' || typeof done === 'undefined') {
                console.error('Wrong parameters for runSuite call.')
            }

            // Do the thing
            else {
                var components = parameters.components || Object.keys(DIRECTORIES[suite]);
                for (var component of components) {
                    var files = [];
                    var specFiles = [];
                    for (var specFilePattern of DIRECTORIES[suite][component]) {
                        var specFilesTmp = glob.sync(specFilePattern);
                        for (var specFile of specFilesTmp) {
                            var file = specFile.replace('.spec', '');
                            fs.statSync(file);
                            files.push(file);
                        }
                        specFiles = specFiles.concat(specFilesTmp);
                    }
                    new karma.Server(extend({}, OPTIONS, {
                        files: OPTIONS.files.concat(files.concat(specFiles))
                    }), done).start();
                }
            }
        }

        return {
            directives: function(done) { runSuite({suite: 'directives', done: done}); }
        };
    };
})();

这是正在测试的field.spec.js

// src/scripts/ui/fields.spec.js

describe('field.js ›', function() {

    beforeEach(module('liv.ui.field'));

    describe('livField', function() {
        it('should pass as it is a simple test', function() {
            expect(true).toEqual(false);
        });
    });
});

我遵循了建议,要么返回Gulp流,要么提供回调以保持异步。为什么Gulp在报告测试错误后没有正常退出?

0 个答案:

没有答案