Pipe TestBed
Custom test bed for testing Pipe.
Works for standalone and non-standalone pipe.
Quick example
describe('AppPipe', () => {
const tb = pipeTestBed(AppPipe);
it('should transform values', tb(({ verify }) => {
verify({ data: 'format', parameters: ['my-'], expected: 'my-format' });
verify.many([
{ data: 'format', parameters: ['a-'], expected: 'a-format' },
{ data: 'format', parameters: ['a-', '-b'], expected: 'a-format-b' },
]);
}));
});
pipeTestBed(..)
Creates a specific test bed for pipe.
It returns a function to be used to wrap it's callback and from which you access tools (check PipeTools).
describe('AppPipe', () => {
const tb = pipeTestBed(AppPipe);
it('should ', tb((tools) => { // 👈 tb function used here
// ... expectations
}));
});
tb function supports the jasmine DoneFn and async/await notation.
describe('AppPipe', () => {
const tb = pipeTestBed(AppPipe);
it('should ', tb(async (tools) => {
// ... async expectations
}));
it('should ', tb((tools, done) => {
// ... expectations
done();
}));
});
TestBed Options
describe('AppPipe', () => {
const tb = pipeTestBed(AppPipe, {} /* 👈 here */);
it('should ', tb(() => {
// ... expectations
}));
});
imports
Default : []
Imports dependencies for the described pipe.
It is often used for non-standalone pipe, because standalone pipe embed its own importations.
Example :
const tb = pipeTestBed(AppPipe, {
imports: [SharedModule, MaterialModule],
});
providers
Default : []
List of providers to be available during tests for the described pipe.
Example :
const tb = pipeTestBed(AppPipe, {
providers: [AuthService, { provide: StoreService, useClass: MockStoreService }],
});
autoCompile
Default : true
Automatically compiles the custom test bed for each test.
checkCreate
Default : true
Automatically invokes the "should create" Angular test.
It checks if the provided described instance is truthy.
Assertion tools
The tb function provides PipeTools.
describe('AppPipe', () => {
const tb = pipeTestBed(AppPipe);
it('should ', tb((tools /* 👈 here */) => {
// ... expectations
}));
});
PipeTools extends BaseTools.
pipe
The described pipe instance.
The instance is typed according to the passed pipe Type<T> in pipeTestBed(..).
Example :
it('should ', tb(({ pipe }) => {
expect(pipe.prop).toEqual('foo');
}));
verify
Verifies pipe transform with passed data and parameters.
Example :
it('should transform values', tb(({ verify }) => {
verify({ data: 'format', parameters: ['my-'], expected: 'my-format' });
verify.many([
{ data: 'format', parameters: ['a-'], expected: 'a-format' },
{ data: 'format', parameters: ['a-', '-b'], expected: 'a-format-b' },
]);
}));
PipeTestBed
import(..)
Same as options imports but with chaining methods.
Example :
describe('AppPipe', () => {
const tb = pipeTestBed(AppPipe)
.import(SharedModule)
.import([ThirdPartyModule, MaterialModule]);
});
provide(..)
Same as options providers but with chaining methods.
Example :
describe('AppPipe', () => {
const tb = pipeTestBed(AppPipe)
.provide(NotifService)
.provide([StoreService, { provide: MY_TOKEN, useValue: mockValue }]);
});
inject(..)
Links an injected instance to a key and retrieve it into the enhanced tools by autocompletion.
describe('AppPipe', () => {
const tb = pipeTestBed(AppPipe)
.inject('auth', AuthService);
it('should ', tb(({ injected: { auth } }) => {
// ... expectations
}));
});
setup(..)
Setups extra action using the enhanced tools.
Works only for beforeEach and afterEach.
Example :
describe('AppPipe', () => {
const tb = pipeTestBed(AppPipe);
beforeEach(tb.setup(({ pipe }) => {
pipe.foo = true;
}));
});
compile()
To be used when you need to do third party setups before compiling the custom test bed.
It has to be used into beforeEach(..) and autoCompile must be set to false.
describe('AppPipe', () => {
const tb = pipeTestBed(AppPipe, { autoCompile: false });
beforeEach(async () => {
// ... third party setup
await tb.compile();
});
});
itShouldCreatePipe(..)
Create the "should create" for the described pipe.
To be used when there are no noticeable or relevant tests to be performed.
describe('AppPipe', () => {
itShouldCreatePipe(AppPipe);
});