2016-11-17 48 views
4

我有一個用angular-cli創建的基本AngularJS2項目。我從新創建的項目開始。在app.component.ts,我存儲日期:ng2日期管道格式化觸發'業餘測試中沒有本地數據'錯誤

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. 
[email protected]_modules/karma-intl-shim/lib/shim.js:11:1866 
[email protected]_modules/karma-intl-shim/lib/shim.js:11:8835 
[email protected]_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'), 
     ... 
    ] ... 

備註:

  • 角(CLI) 「1.0.0-beta.16」
  • 我切換到PhantomJS爲了方便但同樣具有Chrome發生。
  • 我確實執行了npm install --save來安裝依賴關係。

爲了方便讀者閱讀,項目存儲在here

缺失的是什麼?謝謝。

回答

3

其實我來到這個試圖找到解決方案,在這個工作對我和我所有的測試結束時通過與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 
      },] 

UPDATE:根據OS的路徑可能像

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

對國際變化VS

​​

通知的大寫字母 「I」

0

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

+0

我karma.conf.json文件中包含的文件:[{ 模式: './src/test.ts',眼睜睜地看着:假} ],它應該是更新文件:[{ 模式:' ./ src/test.ts',看過:false},'./node_modules/Intl/locale-data/jsonp/en-US.js' ] –

+0

@NaveedAhmed,你可能想檢查我的答案在這裏..http:/ /stackoverflow.com/questions/43182840/no-locale-data-error-in-karma-test-using-phantomjs/43228904#43228904 – N0mi

0

如果你有一個較舊的angular-cli (1.0.1)項目,在polyfills.ts

>>轉到

申請進口部分,波紋管這條線import 'intl'; // Run npm install --save intl.

>>粘貼此

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