2016-11-17 45 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';