我在我的应用上设置了单元测试。我的基本测试规范project.spec.ts
看起来像
import {Project} from './project';
describe('Project', () => {
let p = new Project('New project');
it('should have the name given in the constructor', () => {
expect(p.name).toBe('New project');
});
});
Project
是我正在测试的类的名称,它在文件app/entities/project.ts
中定义(即在同一文件夹中)。
但是,只要测试规范尝试导入另一个文件,业力就会产生404错误:
01 03 2016 17:21:04.955:WARN [web-server]: 404: /base/app/entities/project
Chrome 48.0.2564 (Windows 10 0.0.0) ERROR
Error: XHR error (404 Not Found) loading C:/Dev/mps/app/entities/project
Error loading C:/Dev/mps/app/entities/project as "./project" from C:/Dev/mps/app/entities/project.spec.js
看起来编译的文件project.spec.js
已正确加载,但文件project.js无法加载。此外,我无法理解/base/
中前缀/base/app/entities/project
的来源。
描述了类似的案例here。但是,建议的解决方案对我不起作用。以下导入变体都不起作用:
let Project = require('./project');
let Project = System.import('./project'); // produces "404: /project" instead
let Project = require('./project.js');
let Project = System.import('./project.js');
我的配置是:
karma.conf.js
module.exports = function(config) {
config.set({
basePath: '',
plugins: ['karma-systemjs', 'karma-jasmine', 'karma-chrome-launcher'],
frameworks: ['systemjs', 'jasmine'],
systemjs: {
configFile: 'system.conf.js'
},
files: [
'app/entities/project.spec.js'
],
exclude: [ ],
preprocessors: { },
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
concurrency: Infinity
})
}
system.conf.js
System.config({
paths: {
'traceur': 'node_modules/traceur/bin/traceur.js',
'systemjs': 'node_modules/systemjs/dist/system.js',
'system-polyfills': 'node_modules/systemjs/dist/system-polyfills.js',
'es6-module-loader': 'node_modules/es6-module-loader/dist/es6-module-loader.js'
}
});
我做错了什么?
答案 0 :(得分:2)
知道了。必须向SystemJS提供引用的文件。这可以使用systemjs.serveFiles
中的参数karma.conf.js
完成:
systemjs: {
serveFiles: [
'app/entities/project.js'
]
}
更好的方法是使用globs配置此参数一次:
systemjs: {
serveFiles: [
'node_modules/**/*.js',
'app/**/*.js'
]
}