2

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 が正常に終了しないのはなぜですか?

4

0 に答える 0