beta.1 broke date pipe on unit tests
After updating to beta.1 the unit tests for components that used the date pipe broke. "The pipe 'date' could not be found"
This is still happening in beta.9, please see the referenced issue. Thank you!
Hit this, so +1. Probably quite important as it means no tests on components using pipes in their template...
Does anyone know if there is any way around this issue without overriding the template?
Never mind, figured out that you could do this in beforeEachProviders to solve it.
import {provide, PLATFORM_PIPES} from "@angular/core";
provide(PLATFORM_PIPES, {useValue: YourPipe, multi: true}),
Did anyone find a work around for this? Unit Tests on Components with the Date pipe in the template fail fairly ugly when running on PhantomJS. Remove the pipe, and all is well. Change from PhantomJS to Chrome, all is well.
PhantomJS + Date Pipe fails, other pipes work fine.
@tklever take a look at my comment before yours. That should solve it.
@tallkid24 That did work using overrideProvider, thank you for your help.
Hope this issue gets fixed in the future.
I can't seem to get the date pipe to work, any help would be greatly appreciated. I'm using rc4 and getting the error "ReferenceError: Can't find variable: Intl"
Here's my entire spec file:
import { provide, PLATFORM_PIPES } from '@angular/core';
import { DatePipe } from '@angular/common';
import { addProviders, async, inject } from '@angular/core/testing';
import { Post, PostComponent, PostHtmlComponent } from './';
import { usingComponentFixture } from '../../test-helpers';
describe('Component: Post', () => {
beforeEach(() => {
provide(PLATFORM_PIPES, {useValue: DatePipe, multi: true });
addProviders([PostComponent, PostHtmlComponent, ]);
});
it('should render an h1 tag with text matching the post title',
usingComponentFixture(PostComponent, fixture => {
let component = <PostComponent>fixture.componentInstance;
let element = fixture.nativeElement;
component.post = <Post>{ title: 'Hello', publishedOn: new Date('8/5/2016') };
fixture.detectChanges();
expect(element.querySelector('.blog-post-header h1').innerText).toBe('Hello');
})
);
});
And here's the template:
<div class="col-lg-8 col-md-7 col-sm-6">
<h1>{{post.title}}</h1>
<p class="lead">{{post.publishedOn | date:'fullDate'}}</p>
</div>
I don't think it's anything to do with the pipe. Sounds like something in your component.
On Aug 6, 2016 11:56 PM, "peterjp80" [email protected] wrote:
I can't seem to get the date pipe to work, any help would be greatly appreciated. I'm using rc4 and getting the error "ReferenceError: Can't find variable: Intl"
Here's my entire spec file:
`import { provide, PLATFORM_PIPES } from '@angular/core'; import { DatePipe } from '@angular/common'; import { addProviders, async, inject } from '@angular/core/testing';
import { Post, PostComponent, PostHtmlComponent } from './'; import { usingComponentFixture } from '../../test-helpers';
describe('Component: Post', () => { beforeEach(() => { provide(PLATFORM_PIPES, {useValue: DatePipe, multi: true }); addProviders([PostComponent, PostHtmlComponent, ]); });
it('should render an h1 tag with text matching the post title', usingComponentFixture(PostComponent, fixture => { let component =
fixture.componentInstance; let element = fixture.nativeElement; component.post = <Post>{ title: 'Hello', publishedOn: new Date('8/5/2016') }; fixture.detectChanges(); expect(element.querySelector('.blog-post-header h1').innerText).toBe('Hello'); }));
});`
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/angular/angular/issues/6405#issuecomment-238063656, or mute the thread https://github.com/notifications/unsubscribe-auth/AHkS2C4-dDTS6ABnQS0VQadlZkfEloCIks5qdWWMgaJpZM4HCfVj .
@tallkid24 I don't receive the error when I remove the date pipe from the template.
Are you passing a variable named Intl to the pipe?
On Aug 7, 2016 1:41 AM, "peterjp80" [email protected] wrote:
@tallkid24 https://github.com/tallkid24 I don't receive the error when I remove the date pipe from the template.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/angular/angular/issues/6405#issuecomment-238066808, or mute the thread https://github.com/notifications/unsubscribe-auth/AHkS2AcXGa_6FjGYrsmMXbjhJNV_BvqTks5qdX4xgaJpZM4HCfVj .
@tallkid24 No, I'm not using a variable named Intl anywhere in my code, but the date pipe provided in @angular/common does have this line:
var intl_1 = require('../facade/intl');
So I'm guessing Intl is used somewhere within the date pipe.
I was able to resolve this issue. Here's what I had to do:
- npm install karma-intl-shim --save-dev
- Add 'intl-shim' to the frameworks collection in karma.conf.js
- Add the following to karma-test-shim.js (this is referenced in the files collection of karma.conf.js)
require('karma-intl-shim'); require('./en-us.js'); // copied from https://github.com/andyearnshaw/Intl.js/blob/master/locale-data/json/en-US.json Intl.__addLocaleData(enUsLocaleData);
Sounds like a support request that got resolved. Closing.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.