簡體   English   中英

ng2日期管道格式化在業力測試中觸發“無區域設置數據”錯誤

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

我有一個用angular-cli創建的基本AngularJS2項目。 我從新生成的項目開始。 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 install --save來安裝依賴項。

為了使讀者更容易,項目存儲在這里

丟失的是什么? 謝謝。

我實際上是試圖尋找解決方案,最后這對我有用,我的所有測試都通過了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

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

注意資本“我”

如果您有一個較舊的angular-cli (1.0.1)項目,則polyfills.ts缺少導入

>> 轉到

應用程序導入部分,並在此行import 'intl'; // Run npm install --save intl. import 'intl'; // Run npm install --save intl.

>> 粘貼這個

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