ng2 日期管道格式化触发业力测试中的 'No locale data' 错误
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
安装依赖项。
为了方便reader,项目存储here.
少了什么?谢谢。
您需要将以下内容添加到 karma.conf.json
文件的 files
部分:
'./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
},]
更新:根据 OS,国际路径可能会有所不同,例如
pattern: './node_modules/Intl/locale-data/jsonp/en-US.js',
VS
pattern: './node_modules/intl/locale-data/jsonp/en-US.js',
注意首都"I"
如果您有一个较旧的 angular-cli (1.0.1)
项目,polyfills.ts
中缺少导入
>> 转到
Application Imports 部分,并在这一行下方 import 'intl'; // Run npm install --save intl.
>> 粘贴这个
import 'intl/locale-data/jsonp/en'
;
我有一个使用 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
安装依赖项。
为了方便reader,项目存储here.
少了什么?谢谢。
您需要将以下内容添加到 karma.conf.json
文件的 files
部分:
'./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
},]
更新:根据 OS,国际路径可能会有所不同,例如
pattern: './node_modules/Intl/locale-data/jsonp/en-US.js',
VS
pattern: './node_modules/intl/locale-data/jsonp/en-US.js',
注意首都"I"
如果您有一个较旧的 angular-cli (1.0.1)
项目,polyfills.ts
>> 转到
Application Imports 部分,并在这一行下方 import 'intl'; // Run npm install --save intl.
>> 粘贴这个
import 'intl/locale-data/jsonp/en'
;