简体   繁体   English

ng2日期管道格式化在业力测试中触发“无区域设置数据”错误

[英]ng2 date pipe formatting triggers 'No locale data' error in karma test

I have a basic AngularJS2 project created with angular-cli. 我有一个用angular-cli创建的基本AngularJS2项目。 I start with the freshly generated project. 我从新生成的项目开始。 In app.component.ts , I store a date: app.component.ts ,我存储了一个日期:

theDate = new Date();

I display it using a date pipe: 我用日期管道显示它:

{{theDate | date}}

The date is correctly displayed and formatted as expected. 日期正确显示并按预期格式化。 But if I run ng test , I get the following error: 但如果我运行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

The failing test is: 失败的测试是:

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 : package.json

"karma-intl-shim": "^1.0.3"

karma.conf : karma.conf

module.exports = function (config) {
  config.set({
    ...
    frameworks: ['jasmine', 'angular-cli', 'intl-shim'],
    plugins: [
      require('karma-intl-shim'),
      ...
    ] ...

Remarks: 备注:

  • angular-cli "1.0.0-beta.16" angular-cli“1.0.0-beta.16”
  • I've switched to PhantomJS for convenience but the same occurs with Chrome . 为方便起见,我已切换到PhantomJS ,但Chrome
  • I did perform npm install --save to install dependencies. 我确实执行了npm install --save来安装依赖项。

In order to make things easier to the reader, the project is stored here . 为了使读者更容易,项目存储在这里

What is the missing piece? 丢失的是什么? Thank you. 谢谢。

I actually came to this trying to find solution, at the end this worked for me and all my tests are passing with PhantomJS 我实际上是试图寻找解决方案,最后这对我有用,我的所有测试都通过了PhantomJS

Package.json 的package.json

  "dependencies": {  
  "intl": "^1.2.5",
..
   },
 "devDependencies": {
    "karma-intl-shim": "^1.0.3",
    "phantomjs-prebuilt": "~2.1.7",
}

In Karma.conf.js 在Karma.conf.js中

 - frameworks: [... 'intl-shim' ..],
 - plugin: [... require('karma-intl-shim') . ]
 -  files: [{
                pattern: './node_modules/Intl/locale-data/jsonp/en-US.js',
                watched: false
            },]

UPDATE: Depending on OS the path might vary for Intl like 更新:根据操作系统,Intl的路径可能会有所不同

                pattern: './node_modules/Intl/locale-data/jsonp/en-US.js',

VS VS

                pattern: './node_modules/intl/locale-data/jsonp/en-US.js',

Notice the Capital "I" 注意资本“我”

In case that you have an older angular-cli (1.0.1) project, there is a missing import in polyfills.ts 如果您有一个较旧的angular-cli (1.0.1)项目,则polyfills.ts缺少导入

>> Go to >> 转到

Application Imports section, and bellow this line import 'intl'; // Run npm install --save intl. 应用程序导入部分,并在此行import 'intl'; // Run npm install --save intl. import 'intl'; // Run npm install --save intl.

>> Paste this >> 粘贴这个

import 'intl/locale-data/jsonp/en' ; import 'intl/locale-data/jsonp/en' ;

您需要将以下内容添加到files部分的karma.conf.json文件中: './node_modules/Intl/locale-data/jsonp/en-US.js'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM