[英][web-server]: 404: /@angular/core/testing, systemjs, karma, angular2, jspm
我正在使用systemjs,karma,angular2和jspm。 该项目运行良好。 这是我运行测试时遇到的问题。 它说它无法找到核心测试库。 我不知道在哪里更改此配置。
任何帮助表示赞赏。
吉姆。
- 单元测试
import {it, describe, expect, beforeEach, inject} from '@angular/core/testing'
import {myCOmponent} from 'path to my comonent';
describe('myCOmponent ', () => {
it('should have the component of defined', () => {
expect(myCOmponent).toBeDefined();
});
});
----业力conf
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
// paths loaded by Karma
{pattern: 'jspm_packages/system-polyfills.js', included: true, watched: true},
{pattern: 'jspm_packages/system.src.js', included: true, watched: true},
{pattern: 'node_modules/es6-shim/es6-shim.js', included: true, watched: true},
{pattern: 'node_modules/angular2-polyfill/bundles/angular2-polyfill.js', included: true, watched: true},
{pattern: 'jspm_packages/npm/@angular/core/testing', included: true, watched: true},
{pattern: 'karma-test-shim.js', included: true, watched: true},
// paths to support debugging with source maps in dev tools
{pattern: 'app/**/*.ts', included: false, watched: false},
],
// proxied base paths
proxies: {
// required for component assests fetched by Angular's compiler
"/app/": "/base/app/"
},
preprocessors: {
// source files, that you wanna generate coverage for
// do not include tests or libraries
// (these files will be instrumented by Istanbul)
'app/**/*.js': ['coverage']
},
htmlReporter: {
outputFile: 'reports/test/index.html'
},
// optionally, configure the reporter
coverageReporter: {
type: 'json',
dir: 'reports/',
subdir: '.',
file: 'coverage.json'
},
reporters: ['progress', 'html', 'coverage'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
})
}
---业力垫片
// Tun on full stack traces in errors to help debugging
// Error.stackTraceLimit = Infinity;
Error.stackTraceLimit = 0;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 3000;
// // Cancel Karma's synchronous start,
// // we will call `__karma__.start()` later, once all the specs are loaded.
__karma__.loaded = function() {};
System.config({
packages: {
'base/app': {
defaultExtension: false,
format: 'register',
map: Object.keys(window.__karma__.files).
filter(onlyAppFiles).
reduce(function createPathRecords(pathsMapping, appPath) {
// creates local module name mapping to global path with karma's fingerprint in path, e.g.:
// './hero.service': '/base/public/app/hero.service.js?f4523daf879cfb7310ef6242682ccf10b2041b3e'
var moduleName = appPath.replace(/^\/base\/app\//, './').replace(/\.js$/, '');
pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath]
return pathsMapping;
}, {})
}
}
});
System.import('@angular/core/testing').then(function(testing) {
console.log(testing);
return System.import('@angular/platform-browser-dynamic/testing').then(function(providers) {
testing.setBaseTestProviders(providers.TEST_BROWSER_PLATFORM_PROVIDERS,
providers.TEST_BROWSER_APPLICATION_PROVIDERS);
});
}).then(function() {
return Promise.all(
Object.keys(window.__karma__.files) // All files served by Karma.
.filter(onlySpecFiles)
// .map(filePath2moduleName) // Normalize paths to module names.
.map(function(moduleName) {
// loads all spec files via their global module names (e.g. 'base/public/app/hero.service.spec')
return System.import(moduleName);
}));
})
.then(function() {
__karma__.start();
}, function(error) {
__karma__.error(error.stack || error);
});
function filePath2moduleName(filePath) {
return filePath.
replace(/^\//, ''). // remove / prefix
replace(/\.\w+$/, ''); // remove suffix
}
function onlyAppFiles(filePath) {
return /^\/base\/app\/.*\.js$/.test(filePath)
}
function onlySpecFiles(path) {
return /^\/base\/test\/.*\.js$/.test(path);
}
所以配置这个有两个部分。 你有Karma配置,你有SystemJS配置。 Karma是服务器,因此您需要在服务器中包含所有文件,就像您应用任何应用程序一样。 karma.conf
的files
数组是列出服务器中应包含的所有文件的位置。 所以目前你缺少一堆Angular文件。 您只需执行以下操作即可包含所有这些内容
{ pattern: 'jspm_packages/npm/@angular/**/*.js', included: false, watched: false },
{ pattern: 'jspm_packages/npm/@angular/**/*.js.map', included: false, watched: false },
这使用一种模式来包含所有@angular
文件。 include: false
因为您不希望它们作为<script>
添加到karma服务器中使用的索引页面。 watched: false
因为你不希望Karma在观看模式下观看它们。
您可能还需要其他文件,例如rxjs
。 查看Angular 2快速入门中的karma.conf
,了解它们包含的所有文件的列表。
您还会注意到systemjs.config.js
文件。 如果您使用SystemJS作为应用程序的模块加载器,那么您应该拥有此文件。 此文件加载了应用程序所需的所有模块,因此您也需要它们进行测试。
然后,您需要配置SystemJS进行测试。 您有应用程序systemjs.config.js
文件,但这仅适用于主应用程序。 你仍然需要加载@angular
模块进行测试,比如@angular/core/testing
。 这些不包含在应用程序systemjs.conf.js
文件中,因为您不需要在应用程序中使用这些测试文件。 这就是karma-test-shim
的用途。
所以在你的karma-test-shim
,你需要配置SystemJS来加载测试模块。 您可以从Angular 2快速入门中查看karma-test-shim
,了解它们如何配置它。 您需要创建一个映射,以便可以使用短名称加载文件,而不是完整路径。 例如
System.config({
paths: {
'jspm:': 'jspm_packages/npm/'
},
baseURL: 'base',
map: {
'@angular/core/testing': 'jspm:@angular/core/bundles/core-testing.umd.js',
'@angular/common/testing': 'jspm:@angular/common/bundles/common-testing.umd.js',
'@angular/compiler/testing': 'jspm:@angular/compiler/bundles/compiler-testing.umd.js',
'@angular/platform-browser/testing': 'jspm:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
'@angular/platform-browser-dynamic/testing': 'jspm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',
'@angular/http/testing': 'jspm:@angular/http/bundles/http-testing.umd.js',
'@angular/router/testing': 'jspm:@angular/router/bundles/router-testing.umd.js',
'@angular/forms/testing': 'jspm:@angular/forms/bundles/forms-testing.umd.js',
},
});
如果你查看paths
,你会看到它只是设置一个前缀,这样你以后就不需要输入太多。 创建映射时将使用此前缀。
然后在map
您将看到创建了别名。 例如
'@angular/core/testing': 'jspm:@angular/core/bundles/core-testing.umd.js',
在这里看到映射@angular/core/testing
被映射到实际的模块文件,使用前面提到的paths
前缀。 这就是我们能够做到的
import { TestBed } from '@angular/core/testing'
System.import('@angular/core/testing')
这是因为我们能够做到这一点的映射。 如果我们没有映射,SystemJS将查找文件名@angular/core/testing
,它将找不到,因此404。
我认为这应该给你足够的信息来帮助你弄清楚你需要做什么。 查看我链接到的快速入门中的文件,作为参考,如果您遇到困难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.