Testing a pipe

Pipes in Angular are functions that can transform input data into a user-readable format. Here is an example of a custom pipe called capitalise in our src folder, using the standard String.toUpperCase(). This is just an example; Angular already has its own pipe for capitalization:

//capitalise.pipe.tsimport {Pipe, PipeTransform} from '@angular/core';@Pipe({name: 'capitalise'})export class CapitalisePipe implements PipeTransform {transform(value: string): string {if (typeof value !== 'string') {throw new Error('Requires a String as input');}return value.toUpperCase();}}

The capitalise.pipe.spec.ts file will be as follows:

describe('Pipe: CapitalisePipe', () => {let pipe;//setupbeforeEach(() => TestBed.configureTestingModule({ ...

Get TypeScript 2.x for Angular Developers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.