Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular ng2日期管道格式化触发器';没有区域设置数据';业力测验中的错误_Angular_Karma Runner_Shim - Fatal编程技术网

Angular ng2日期管道格式化触发器';没有区域设置数据';业力测验中的错误

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

我有一个使用AngularCLI创建的基本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安装--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',