angular icon indicating copy to clipboard operation
angular copied to clipboard

beta.1 broke date pipe on unit tests

Open pjpsoares opened this issue 10 years ago • 15 comments

After updating to beta.1 the unit tests for components that used the date pipe broke. "The pipe 'date' could not be found"

pjpsoares avatar Jan 11 '16 17:01 pjpsoares

This is still happening in beta.9, please see the referenced issue. Thank you!

randyv12 avatar Mar 20 '16 05:03 randyv12

Hit this, so +1. Probably quite important as it means no tests on components using pipes in their template...

Doormouse2House avatar May 08 '16 10:05 Doormouse2House

Does anyone know if there is any way around this issue without overriding the template?

ghost avatar May 26 '16 17:05 ghost

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}),

ghost avatar May 26 '16 18:05 ghost

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 avatar Jun 13 '16 12:06 tklever

@tklever take a look at my comment before yours. That should solve it.

ghost avatar Jun 13 '16 21:06 ghost

@tallkid24 That did work using overrideProvider, thank you for your help.

Hope this issue gets fixed in the future.

tklever avatar Jun 14 '16 16:06 tklever

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>

peterjp80 avatar Aug 07 '16 04:08 peterjp80

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 .

ghost avatar Aug 07 '16 05:08 ghost

@tallkid24 I don't receive the error when I remove the date pipe from the template.

peterjp80 avatar Aug 07 '16 06:08 peterjp80

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 .

ghost avatar Aug 07 '16 06:08 ghost

@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.

peterjp80 avatar Aug 07 '16 06:08 peterjp80

I was able to resolve this issue. Here's what I had to do:

  1. npm install karma-intl-shim --save-dev
  2. Add 'intl-shim' to the frameworks collection in karma.conf.js
  3. 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);

peterjp80 avatar Aug 07 '16 08:08 peterjp80

Sounds like a support request that got resolved. Closing.

pkozlowski-opensource avatar Sep 01 '16 17:09 pkozlowski-opensource

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.