0

I recently wrote a primitive pipe used for searching items:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'search'
})
export class SearchPipe implements PipeTransform {

  transform(value: any, args?: any): any {

    if (!value) {return null; }
    if (!args) {return value; }

    args = args.toLowerCase();

    return value.filter((item) => {
        return JSON.stringify(item).toLowerCase().includes(args);
    });
}

}

My pipe test file is the default one:

import { SearchPipe } from './search.pipe';

describe('SearchPipe', () => {
  it('create an instance', () => {
    const pipe = new SearchPipe();
    expect(pipe).toBeTruthy();
  });
});

When I run my test inside of another component, which uses the search pipe I get the following error:

Failed: Template parse errors:
The pipe 'search' could not be found ("target.value)">
      <!-- maybe add | async pipe -->
    <option class="fetchedUser" *ngFor="let [ERROR ->]user of users | search: query" value="{{user.firstname}}">
        {{user?.lastname}}, {{user?.first"): ng:///DynamicTestModule/UsersComponent.html@13:44

Any clue why this is happening? How to fix this error?

Munchkin
  • 857
  • 5
  • 24
  • 51
  • Does this answer your question? [The pipe ' ' could not be found angular2 custom pipe](https://stackoverflow.com/questions/39007130/the-pipe-could-not-be-found-angular2-custom-pipe) – nopassport1 Jan 27 '20 at 13:26
  • @nopassport1 If I try to follow the answer I get the error `error TS2339: Property 'forRoot' does not exist on type 'typeof SearchPipe'.` :/ – Munchkin Jan 27 '20 at 13:32
  • @nopassport1 if I add it without .forRoot() I get the following error: `Failed: Unexpected pipe 'SearchPipe' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation. ` – Munchkin Jan 27 '20 at 13:34
  • Isn't the answer technically wrong, in my case I imported in my module.ts my pipe as a declaration. – Munchkin Jan 27 '20 at 13:38

0 Answers0