Angular ng2日期管道格式化触发器';没有区域设置数据';业力测验中的错误
我有一个使用AngularCLI创建的基本AngularJS2项目。我从新生成的项目开始。在Angular ng2日期管道格式化触发器';没有区域设置数据';业力测验中的错误,angular,karma-runner,shim,Angular,Karma Runner,Shim,我有一个使用AngularCLI创建的基本AngularJS2项目。我从新生成的项目开始。在app.component.ts中,我存储了一个日期: theDate = new Date(); 我使用日期管道显示它: {{theDate | date}} 日期的显示和格式与预期一致。但是如果我运行ng test,我会得到以下错误: Failed: Error in ./AppComponent class AppComponent - inline template:4:3 caused by
app.component.ts
中,我存储了一个日期:
theDate = new Date();
我使用日期管道显示它:
{{theDate | date}}
日期的显示和格式与预期一致。但是如果我运行ng test
,我会得到以下错误:
Failed: Error in ./AppComponent class AppComponent - inline template:4:3 caused by: No locale data has been provided for this object yet.
g@node_modules/karma-intl-shim/lib/shim.js:11:1866
F@node_modules/karma-intl-shim/lib/shim.js:11:8835
k@node_modules/karma-intl-shim/lib/shim.js:11:8335
失败的测试是:
it('should render title in a h1 tag', async(() => {
let fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
let compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h1').textContent).toContain('app works!');
}));
package.json
:
"karma-intl-shim": "^1.0.3"
karma.conf
:
module.exports = function (config) {
config.set({
...
frameworks: ['jasmine', 'angular-cli', 'intl-shim'],
plugins: [
require('karma-intl-shim'),
...
] ...
备注:
- angular cli“1.0.0-beta.16”
- 为了方便起见,我切换到了
,但是PhantomJS
也会出现同样的情况Chrome
- 我确实执行了
来安装依赖项npm安装--save
丢失的那块是什么?谢谢。您需要在
文件
部分的karma.conf.json
文件中添加以下内容:
'./node_modules/Intl/locale data/jsonp/en US.js'
我实际上是为了找到解决方案而来的,最后这对我来说很有效,我的所有测试都通过了PhantomJS
Package.json
"dependencies": {
"intl": "^1.2.5",
..
},
"devDependencies": {
"karma-intl-shim": "^1.0.3",
"phantomjs-prebuilt": "~2.1.7",
}
在Karma.conf.js中
- frameworks: [... 'intl-shim' ..],
- plugin: [... require('karma-intl-shim') . ]
- files: [{
pattern: './node_modules/Intl/locale-data/jsonp/en-US.js',
watched: false
},]
更新:根据操作系统的不同,类似Intl的路径可能会有所不同
pattern: './node_modules/Intl/locale-data/jsonp/en-US.js',
VS
请注意大写字母“I”如果您有一个较旧的
angular cli(1.0.1)
项目,则polyfills.ts中缺少导入
>>转到
“应用程序导入”部分,并在这一行下面加上“import'intl”;//运行npm安装--保存内部文件。
>>粘贴此文件
导入“intl/locale data/jsonp/en”
我的karma.conf.json文件包含以下文件:[{pattern:'./src/test.ts',wasted:false}],如果它被更新为文件:[{pattern:'./src/test.ts',wasted:false}'./node_modules/Intl/locale data/jsonp/en US.js']@navedahmed,您可能需要在这里检查我的答案。。
pattern: './node_modules/intl/locale-data/jsonp/en-US.js',