0

I am writing unit test cases for angular 6 component, I have below code which is getting error, I almost covered 65% code coverage but this is annoying me and I dont know how to cover pipe in unit test cases.

TypeError: this.threadData.pipe is not a function

On below line :

this.subscriptions.add(
  this.msgDataService.pipe(first()).subscribe((data: any) => {
}

I was trying below but don't know if this is a solution or not. Angular testing - observable pipe is not a function

Thanks

aturan23
  • 4,798
  • 4
  • 28
  • 52
angularguy
  • 13
  • 6

1 Answers1

0

This error can occur if spy on pipe function is missing in unit test.

You will have to create a spy on pipe and return an Observable. Then it will call the pipe function and execute the callback with the returned mock data.

    beforeEach(async(() => {
        let responseData = {};
        this.msgDataServiceSpy = () => ({
            pipe: () => (of({responseData}))    
        });
        TestBed.configureTestingModule({
            imports: [..],
            schemas: [NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA],
            declarations: [..],
            providers: [
                {
                    provide: msgDataService,
                    useFactory: msgDataServiceSpy
                }
            ]
        })
        .compileComponents();
    }));
Amit Vishwakarma
  • 316
  • 2
  • 10