I have a basic AngularJS2 project created with angular-cli. I start with the freshly generated project. In app.component.ts
, I store a date:
theDate = new Date();
I display it using a date pipe:
{{theDate | date}}
The date is correctly displayed and formatted as expected. But if I run ng test
, I get the following error:
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
The failing test is:
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'),
...
] ...
Remarks:
PhantomJS
for convenience but the same occurs with Chrome
. npm install --save
to install dependencies. In order to make things easier to the reader, the project is stored here .
What is the missing piece? Thank you.
I actually came to this trying to find solution, at the end this worked for me and all my tests are passing with PhantomJS
Package.json
"dependencies": {
"intl": "^1.2.5",
..
},
"devDependencies": {
"karma-intl-shim": "^1.0.3",
"phantomjs-prebuilt": "~2.1.7",
}
In 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: Depending on OS the path might vary for Intl like
pattern: './node_modules/Intl/locale-data/jsonp/en-US.js',
VS
pattern: './node_modules/intl/locale-data/jsonp/en-US.js',
Notice the Capital "I"
In case that you have an older angular-cli (1.0.1)
project, there is a missing import in polyfills.ts
>> Go to
Application Imports section, and bellow this line import 'intl'; // Run npm install --save intl.
import 'intl'; // Run npm install --save intl.
>> Paste this
import 'intl/locale-data/jsonp/en'
;
您需要将以下内容添加到files
部分的karma.conf.json
文件中: './node_modules/Intl/locale-data/jsonp/en-US.js'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.