对于测试 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
但是,当 anexpect
不验证时,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 在报告测试错误后无法正常退出?