如何在 Karma 中配置 System.js 以测试取决于时刻.js的代码
How to configure System.js in Karma to test code that depends on moment.js
如果我的应用程序代码使用 ng2-boostrap 模块,我无法在 Karma 中运行茉莉花测试,这取决于 moment.js 库。
该应用程序在浏览器中运行良好,并且可以通过向系统添加映射来在浏览器中运行茉莉花测试:
-- 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'
}
});
我试图在 karma.shim 内部进行相同的操作,但它不起作用。我得到:
Error: XHR error (404 Not Found) loading http://localhost:9876/node_modules/moment/moment.js
Error loading http://localhost:9876/node_modules/moment/moment.js as "moment" from http://localhost:9876/ng2-bootstrap/components/datepicker/date-formatter
at addToError (D:/Projects/Angular/karma-ngbootstrap/node_modules/systemjs/dist/system.src.js:41:18)
在我的业力中,我有
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},
....
在业力测试垫片中:
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 错误路径中得到它。
完整文件可以在以下简单测试项目中找到:https://github.com/tzielins/angular-start-project
业力配置基于 http://twofuckingdevelopers.com/2016/01/testing-angular-2-with-karma-and-jasmine/
我很高兴使用更简单的配置,但我运气有 System.js/Karma 的经验从头开始,这个没有 ng2-boostrap 依赖项(测试中的代码仅从 ng2-boostrap 导入,这足以绊倒业力,可以注释掉以通过测试)。
我一定在系统.js配置中缺少一些明显的东西。
我正在使用新的"ng2-bootstrap" - "ngx-bootstrap"与角种子,也遇到了同样的问题。
修复了此问题 - 将以下代码添加到"karma.conf.js",在文件部分下:
{pattern: 'node_modules/ngx-bootstrap/bundles/ngx-bootstrap.umd.min.js', included: false, watched: false}
相关文章:
- 构建JS测试,警报窗口重复上一个Q,而不是问下一个Q
- node.js测试事件是否是在不使用超时的情况下使用sinon.js发出的
- Mocha js测试字符串不相等时不显示diff
- 非常简单的js测试;Don’不要和Minko一起工作
- 如何使用moment.js测试javascript对象是否为Date
- NodeJS用Mocha和should.js测试数据库
- 帆的“500”错误.js“测试项目”
- 如何模拟具有绝对路径的请求以进行JS测试
- Capybara JS 测试失败了 page.all
- 让 Frisby.js 测试同步运行
- 使用 Node.js 测试网址
- 如何使用 Wolfpack.js 测试我的 Sails.js 模型的唯一电子邮件属性
- 如何在我的 js 测试中使用模拟
- 使用实习生 Js 测试节点模块 - 无法解决“需求”
- 在节点.js测试后清理数据库
- 使用 Sinon.js 测试 navigator.browserLanguage 或 navigator.languag
- 简单的要求JS测试不起作用
- 使用 Sinon.js 测试一个函数是否调用 ES6 模块中的另一个函数
- 如何在angular.js测试中使用$http的实际实现
- 从inquirer.js菜单运行dractor.js测试