当我在Angular2中运行Karma Tests时,我收到以下错误 .
未捕获的TypeError:意外的匿名System.register调用 . 在http://localhost:9876/base/node_modules/systemjs/dist/system.src.js?38538ebca96bc7b222f7e7ba15943f173a485f6e:2885
我想是因为var系统目前无法使用 . 即使它实际装载 . 有谁知道为什么这不起作用?
这是我的精简配置:
C:\项目\ karma.conf.js
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'node_modules/systemjs/dist/system.src.js',
'node_modules/angular2/bundles/angular2.dev.js',
'tests/**/*Spec.js'
],
exclude: [
],
preprocessors: {
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['Chrome', 'Firefox'],
singleRun: false,
concurrency: Infinity
})
}
C:\ project \ tests \ MyTestSpec.ts这一行足以触发错误 .
import {describe, expect, it, xit, inject, beforeEachProviders} from 'angular2/testing';
渲染到C:\ project \ tests \ MyTestSpec.js并在第一次出现系统时产生错误
System.register([], function(exports_1) {
return {
setters:[],
execute: function() {
}
}
});
//# sourceMappingURL=CmiCloudSpec.js.map
C:\项目\ tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"watch": true,
},
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
]
}
C:\项目的\ src \ node_modules
包含文件夹angular2和systemjs以及karma,karma-jasmine,jasmine-core和typescript
3 回答
问题是您应该将所有测试文件直接包含在
karma.conf.js
的files
属性中的Karma中 . 因为您没有在tsc配置中定义outFile
属性(并且在编译的JS文件中指定了's fine here), the name of modules aren't) .您需要创建一个
karma-test-shim.js
文件(将包含该文件)并配置SystemJS以按文件名加载模块并使用System.import
导入这些文件 .以下是
karma-test-shim.js
文件的示例:以及
karma.conf.js
中的相应配置:它认为这篇文章及其源代码可以帮助您:
http://twofuckingdevelopers.com/2016/01/testing-angular-2-with-karma-and-jasmine/
https://github.com/Elecash/angular2-testing
非常感谢你 . 无法加载'tests / ** / * Spec.js'中的捆绑文件的说明非常有意义 .
它现在正在运作 .
为了使解释简单,我将karma-test-shim.js添加到文件部分,就是这样 .
我稍后会更改文件部分样式 .
在
karma.conf.js
文件中还需要另一件事需要,否则测试将不会执行