Javascript 如何在Karma中配置System.js来测试依赖于moment.js的代码
如果我的应用程序代码使用ng2 boostrap模块,我无法在Karma中运行jasmine测试,这取决于moment.js库 该应用程序在浏览器中运行良好,jasmine测试可以在浏览器中运行,方法是将mapping for moment添加到系统:Javascript 如何在Karma中配置System.js来测试依赖于moment.js的代码,javascript,karma-runner,momentjs,systemjs,ng2-bootstrap,Javascript,Karma Runner,Momentjs,Systemjs,Ng2 Bootstrap,如果我的应用程序代码使用ng2 boostrap模块,我无法在Karma中运行jasmine测试,这取决于moment.js库 该应用程序在浏览器中运行良好,jasmine测试可以在浏览器中运行,方法是将mapping for moment添加到系统: -- unit-test.html --- ... <script src="node_modules/ng2-bootstrap/bundles/ng2-bootstrap.min.js"></script> ... &
-- unit-test.html ---
...
<script src="node_modules/ng2-bootstrap/bundles/ng2-bootstrap.min.js"></script>
...
<body>
<script>
// #2. Configure SystemJS to use the .js extension
// for imports from the app folder
System.config({
packages: {
'app': {defaultExtension: 'js'}
},
map: {
'moment': 'node_modules/moment/moment.js'
}
});
在我的因果报应中,我有
files: [
...
{pattern: 'node_modules/angular2/bundles/testing.dev.js', included: true, watched: true},
....
{pattern: 'node_modules/moment/moment.js',included: true, watched: true},
{pattern: 'node_modules/ng2-bootstrap/bundles/ng2-bootstrap.min.js', included: true, watched: true},
{pattern: 'karma-test-shim.js', included: true, watched: true},
{pattern: 'app/**/*.js', included: false, watched: true},
....
在karma测试垫片中:
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/src/app/hero.service.js?f4523daf879cfb7310ef6242682ccf10b2041b3e'
var moduleName = appPath.replace(/^\/base\/app\//, './').replace(/\.js$/, '');
pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath]
return pathsMapping;
}, {})
}
},
map: {
'moment': 'node_modules/moment/moment.js'
}
});
映射部分可以工作,因为取决于我在moment
下放置的内容,我稍后会在XHR错误路径中获取它
完整文件可在以下位置的简单测试项目中找到:
业力配置是基于
我很高兴使用一个更简单的配置,但我很幸运地从零开始使用System.js/Karma,并且这个配置在没有ng2 boostrap依赖项的情况下工作(测试中的代码只从ng2 boostrap导入,这足以让Karma绊倒,可以注释为通过测试)
我一定是在System.js配置中遗漏了一些明显的东西。我正在使用新的“ng2引导”
和也遇到了同样的问题
修复了该问题-将以下代码添加到“karma.conf.js”中的文件部分下:
{pattern: 'node_modules/ngx-bootstrap/bundles/ngx-bootstrap.umd.min.js', included: false, watched: false}
{pattern: 'node_modules/ngx-bootstrap/bundles/ngx-bootstrap.umd.min.js', included: false, watched: false}