我正试图在Karma中运行Jasmine测试。这是针对使用RequireJS的AngularJS应用程序,但目前,测试是一个简单的expect(true).toBe(true);
测试,没有Angular依赖。我发现它在禁用Karma的requirejs
模块并在"client/app/require-shared.js"
中注释"test/require-test.js"
和files
并直接加载testSpec.js文件时运行。
但是,只要我重新启用requirejs
并将RequireJS配置文件添加回karma.conf.js的files
- 保持testSpec.js文件在那里“静态”加载 - 我看到了错误
Chrome 33.0.1750 (Windows 7) ERROR
Uncaught TypeError: Cannot read property 'failedCount' of null
at C:/.../project/node_modules/karma-jasmine/lib/adapter.js:126
Chrome 33.0.1750 (Windows 7) ERROR
Uncaught TypeError: Cannot read property 'length' of null
at C:/.../project/node_modules/karma-jasmine/lib/jasmine.js:2518
Chrome 33.0.1750 (Windows 7): Executed 1 of 2 ERROR (0.009 secs / 0.001 secs)
如果我尝试完整的Angular测试,我会得到关于$injector
为空的错误,但我认为这完全是因为这个requirejs模块问题。在我的设置中我缺少什么来使这项工作?
测试/ karma.conf.js
module.exports = function(config) {
config.set({
basePath: "../", // resolves to "project/"
frameworks: ["jasmine", "requirejs"],
files: [
// load the RequireJS config files first
"client/app/require-shared.js",
"test/require-test.js",
// set included to false for files to be loaded via RequireJS
// {pattern: "bower_components/**/*.js", included: false},
// {pattern: "client/**/*.js", included: false},
// {pattern: "test/unit/*Spec.js", included: false}
{pattern: "test/unit/*Spec.js"}
],
// list of files to exclude
exclude: [
// "client/app/app.js",
// "client/app/bootstrap.js",
// "client/app/require-main.js",
"*.conf.js"
],
reporters: ["progress"],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ["Chrome"],
singleRun: true
});
};
客户端/应用程序/需要-shared.js
requirejs.config({
paths: {
"app": "../app/app",
"angular": "../extlibs/angular-1.2.14",
"jquery": "../extlibs/jquery-2.1.0.min",
"ng-cookies": "../extlibs/AngularPlugins/angular-cookies-1.2.13",
"ng-resource": "../extlibs/AngularPlugins/angular-resource-1.2.13",
"ng-route": "../extlibs/AngularPlugins/angular-route-1.2.14",
}
,shim: {
angular: {
exports: "angular"
}
,"ng-resource": {
deps: [ "angular" ],
exports: "ng-resource"
},
,"ng-cookies": ["angular"]
,"ng-route": ["angular"]
,"ngui-bootstrap": ["angular"]
}
});
测试/需要-test.js
var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$/i;
var pathToModule = function(path) {
return path.replace(/^\/base\//, "").replace(/\.js$/, "");
};
Object.keys(window.__karma__.files).forEach(function(file) {
if (TEST_REGEXP.test(file)) {
// Normalize paths to RequireJS module names.
var normalPath = pathToModule(file);
console.log("adding " + file + " as " + normalPath + " to allTestFiles array");
allTestFiles.push(normalPath);
}
});
requirejs.config({
baseUrl: "/base/client/components/"
/*
* "path" is defined in require-shared.js and shared with
* require-main. Add paths for the testing files.
*/
,paths: {
"test": "../../test",
"bower": "../../bower_components",
"angular-mocks": "bower/angular-mocks/angular-mocks"
}
,shim: {
'angular-mocks': {
deps: [ 'ng-resource' ],
exports: 'angular.mock'
}
}
,deps: allTestFiles
,callback: window.__karma__.start
});
测试/单元/ testSpec.js
describe('a basic test of version', function() {
it('true should be true', function() {
expect(true).toBe(true);
});
});
答案 0 :(得分:2)
问题在于TEST_REGEXP
生成的karma init
在文件名中查找了spec
或test
。这导致 require-test.js 文件作为测试规范被引入。
解决方案是
之一(spec|test)
更改为RE spec
/^(?:(?!require-test\.js$).)*(spec|test)\.js$/i
(另请参阅this answer)答案 1 :(得分:0)
这也可能是您的问题的原因,因为它在备用情况下报告相同的错误(缺少package.json):https://github.com/tkellen/js-matchdep/issues/11。但是,如果您看到此结果的错误消息,那么您将运行旧版本的npm。升级会给你一个不同的错误信息(根据线程)。