I am working with a pipe that looks like this:
@Pipe({name: 'myPipe', pure: false})
export class MyPipe implements PipeTransform {
constructor(private _ref:ChangeDetectorRef) {
_ref.markForCheck();
}
public transform(input: string | number) {
return 'something';
}
}
Below is my test specification for the pipe:
describe('Pipe: MyPipe', () => {
let changeDetector: ChangeDetectorRefMock;
let pipe: MyPipe;
beforeEach(async() => {
await TestBed.configureTestingModule({
providers: [
MyPipe,
{
provide: ChangeDetectorRef,
useClass: ChangeDetectorRefMock
}
];
}).compileComponents();
changeDetector = TestBed.inject(ChangeDetectorRef) as ChangeDetectorRefMock;
pipe = TestBed.inject(MyPipe); // <-- error is thrown (see below)
});
it('should create', () => {
expect(pipe).toBeTruthy();
});
});
@Injectable()
class ChangeDetectorRefMock {
public detectChanges(): void {}
}
When running the test spec, I encounter an error at line pipe = TestBed.inject(MyPipe);
:
TypeError: Cannot read properties of null (reading 'type')
at createViewRef (C:\interne\VEBLuZ\packages\core\src\change_detection\change_detector_ref.ts:163:20)
[...]
at C:\exampleProject\libs\example-lib\src\pipe\my.pipe.spec.ts:22:20
Do you have any suggestions on how to properly test this pipe with injection of ChangeDetectorRef?