我的项目是ionic-angularjs-requirejs
我的环境是Mac OS X 10.10.1,节点v0.10.30
我想将karma测试集成到我的项目中,但是当我运行grunt test
时这是错误的:
结果:
Running "karma:unit" (karma) task
INFO [karma]: Karma v0.12.28 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Mac OS X)]: Connected on socket UhgMIttDejE4Xdm8I7mG with id 52208731
WARN [PhantomJS 1.9.8 (Mac OS X)]: Disconnected (1 times), because no message in 10000 ms.
Warning: Task "karma:unit" failed. Use --force to continue.
Aborted due to warnings.
以下是我的package.json中的依赖项:
"grunt-bower-requirejs": "^1.1.1",
"grunt-contrib-uglify": "^0.2.7",
"grunt-karma": "^0.9.0",
"karma": "^0.12.28",
"karma-jasmine": "^0.3.2",
"karma-phantomjs-launcher": "^0.1.4",
"karma-requirejs": "^0.2.2",
"requirejs": "^2.1.15"
这是我的Gruntfile.js:
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
bower: {
target: {
rjsConfig: 'js/main.js'
}
},
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
build: {
src: 'src/<%= pkg.name %>.js',
dest: 'build/<%= pkg.name %>.min.js'
},
},
karma: {
unit: {
options: {
frameworks: ['jasmine', 'requirejs'],
browsers: ['PhantomJS'],
autoWatch: true,
singleRun: true,
files: [
'lib/js/generated/angular/angular.js',
'lib/js/generated/angular-mocks/angular-mocks.js',
//'js/**/*.js',
//'templates/**/*.html',
'tests/*.tests.js'
],
exclude: [
'js/main.js'
]
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-bower-requirejs');
grunt.loadNpmTasks('grunt-karma');
// Tell Grunt what to do when we type "grunt" into the terminal
grunt.registerTask('default', [
'uglify',
'bower'
]);
grunt.registerTask('test', [
'karma'
]);
};
答案 0 :(得分:0)
我使用grunt-karma插件的经验是这样的,我花了更多的时间调试插件而不是我的实际网站。我的解决方案是完全避免它并使用grunt-exec运行我的karma命令或者只是不使用grunt并在新选项卡中手动运行karma。
grunt.loadNpmTasks('grunt-exec');
var config =
{
exec: {
karma : 'karma start {path to your karma.conf.js file}'
// or
// karma : 'karma run {path to your karma.conf.js file}'
}
}
我知道这可能不是您正在寻找的答案,但我的经验让我明白,您从原始程序中获得的每一步都只是您需要调试的另一个程序,而不是您自己的程序
据说, 您的文件似乎未包含在内。
我通常会将我的业力配置文件分成不同的文件:
module.exports = [
'public/lib/angular/angular.js',
'public/lib/lodash/dist/lodash.compat.js',
'public/lib/jquery/dist/jquery.js',
'public/lib/angular-ui-router/release/angular-ui-router.js',
'tests/test-main.js',
{pattern: 'public/js/**/*.js', included: true},
{pattern: 'tests/unit/**/*.js', included: true}
];
var files = require('./files.js');
module.exports = {
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '../',
frameworks: ['jasmine', 'requirejs'],
// list of files / patterns to load in the browser
files: files,
// list of files to exclude
exclude: [
],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
coverageReporter: {
type : 'html',
dir : './tests/coverage/',
subdir: function(browser) {
"use strict";
// normalization process to keep a consistent browser name accross different
// OS
return browser.toLowerCase().split(/[ /-]/)[0];
}
},
};
var commonConfig = require('./ common.conf.js');
module.exports = function(config) {
"use strict";
commonConfig.reporters = ['nyan', 'growl'];
commonConfig.browsers = ['PhantomJS'];
commonConfig.captureTimeout = 60000;
commonConfig.singleRun = false;
commonConfig.logLevel = config.LOG_DEBUG;
config.set(commonConfig);
};
// commonConfig.browsers = ['PhantomJS', 'Chrome']
// commonConfig.browsers = ['PhantomJS', 'Chrome', 'Safari', 'Firefox'],
var commonConfig = require('./common.conf.js');
module.exports = function(config) {
"use strict";
commonConfig.reporters = ['progress'];
commonConfig.browsers = ['PhantomJS'];
commonConfig.captureTimeout = 120000;
commonConfig.singleRun = true;
commonConfig.logLevel = config.LOG_DEBUG;
config.set(commonConfig);
};
var commonConfig = require('./common.conf.js');
module.exports = function(config) {
"use strict";
commonConfig.reporters = ['progress','coverage'];
commonConfig.browsers = ['PhantomJS', 'Chrome', 'Safari', 'Firefox'];
commonConfig.captureTimeout = 120000;
commonConfig.singleRun = true;
commonConfig.logLevel = config.LOG_DEBUG;
commonConfig.preprocessors = {};
commonConfig.preprocessors['./public/js/*.js'] = ['coverage'];
config.set(commonConfig);
};
这样做可以让我针对不同的情况运行不同的业力配置。
开发时我使用: karma start config / karma.conf.js
要在我运行的所有浏览器中获得测试覆盖率: karma start config / test.coverage.js
并验证我的构建是否干净,这是我运行的持续集成过程的一部分 karma start config / build.conf.js
使用此方法,我可以再次使用grunt-exec在适当的时间运行这些命令。