Angular 2 Unit Test: Custom Pipe error The pipe could not be found
I have a custom pipe called 'myPipe'. I am getting:
The pipe 'myPipe' could not be found error
in my unit test ts. Pleas advice what to import and declare in my .spec.ts
Here is my .spec.ts
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { MyComponent } from './main-page-carousel.component';
describe('CarouselComponent', () => {
let component: MyComponent ;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MyComponent ],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
Thanks!
You should be able to do this:
import { MyPipe } from 'here put your custom pipe path';
TestBed.configureTestingModule({
declarations: [ MyComponentUnderTesting, MyPipe ]
})
I had the same problem, and fixed it by adding the following "mock pipe" to my spec.ts:
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({name: 'myPipe'})
class MockPipe implements PipeTransform {
transform(value: number): number {
// blah blah
return value;
}
}
Then you have to add MockPipe to the TestBed configureTestingModule declarations:
TestBed.configureTestingModule({
declarations: [ MyComponentUnderTesting, MockPipe ]
})
I had almost the same pipe issue; in cases of template parse errors, you need to take two steps:
-
Import the required pipe at the start like:
import {{ your_pipe_name }} from '../your/pipe/location'
; -
Add it to your declaration:
TestBed.configureTestingModule({ declarations: [ your_pipe ] });
Happy Coding!