Interceptor TestBed
Custom test bed for testing Interceptor.
Works for function (HttpInterceptorFn
) and class (that implements HttpInterceptor
) interceptor.
Quick example
- function
- Class
describe('appInterceptor', () => {
const tb = interceptorTestBed(appInterceptor());
it('should set custom header before send', tb(({ inspect, rx }, done) => {
const req = new HttpRequest('GET', '/test');
expect(req.headers.has('x-custom-header')).toBeFalse();
rx.remind = inspect.request(req).subscribe({
next: (interceptedReq) => {
expect(interceptedReq.headers.has('x-custom-header')).toBeTrue();
done();
},
});
}));
});
describe('AppInterceptor', () => {
const tb = interceptorTestBed(AppInterceptor);
it('should set custom header before send', tb(({ inspect, rx }, done) => {
const req = new HttpRequest('GET', '/test');
expect(req.headers.has('x-custom-header')).toBeFalse();
rx.remind = inspect.request(req).subscribe({
next: (interceptedReq) => {
expect(interceptedReq.headers.has('x-custom-header')).toBeTrue();
done();
},
});
}));
});
interceptorTestBed(..)
Creates a specific test bed for interceptor.
It returns a function to be used to wrap it
's callback and from which you access tools (check InterceptorTools).
- function
- Class
describe('appInterceptor', () => {
const tb = interceptorTestBed(appInterceptor());
it('should ', tb((tools) => { // 👈 tb function used here
// ... expectations
}));
});
tb
function supports the jasmine DoneFn
and async/await notation.
describe('appInterceptor', () => {
const tb = interceptorTestBed(appInterceptor());
it('should ', tb(async (tools) => {
// ... async expectations
}));
it('should ', tb((tools, done) => {
// ... expectations
done();
}));
});
describe('AppInterceptor', () => {
const tb = interceptorTestBed(AppInterceptor);
it('should ', tb((tools) => { // 👈 tb function used here
// ... expectations
}));
});
tb
function supports the jasmine DoneFn
and async/await notation.
describe('AppInterceptor', () => {
const tb = interceptorTestBed(AppInterceptor);
it('should ', tb(async (tools) => {
// ... async expectations
}));
it('should ', tb((tools, done) => {
// ... expectations
done();
}));
});
TestBed Options
- function
- Class
describe('appInterceptor', () => {
const tb = interceptorTestBed(appInterceptor(), {} /* 👈 here */);
it('should ', tb(() => {
// ... expectations
}));
});
describe('AppInterceptor', () => {
const tb = interceptorTestBed(AppInterceptor, {} /* 👈 here */);
it('should ', tb(() => {
// ... expectations
}));
});
imports
Default : []
Imports dependencies for the described interceptor.
Example :
- function
- Class
const tb = interceptorTestBed(appInterceptor(), {
imports: [SharedModule, MaterialModule],
});
const tb = interceptorTestBed(AppInterceptor, {
imports: [SharedModule, MaterialModule],
});
providers
Default : []
List of providers to be available during tests for the described interceptor.
Example :
- function
- Class
const tb = interceptorTestBed(appInterceptor(), {
providers: [AuthService, { provide: StoreService, useClass: MockStoreService }],
});
const tb = interceptorTestBed(AppInterceptor, {
providers: [AuthService, { provide: StoreService, useClass: MockStoreService }],
});
verifyHttp
Default : true
When enabled, each assertion will end by HttpTestingController.verify()
.
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 InterceptorTools
.
- function
- Class
describe('appInterceptor', () => {
const tb = interceptorTestBed(appInterceptor());
it('should ', tb((tools /* 👈 here */) => {
// ... expectations
}));
});
describe('AppInterceptor', () => {
const tb = interceptorTestBed(AppInterceptor);
it('should ', tb((tools /* 👈 here */) => {
// ... expectations
}));
});
InterceptorTools
extends BaseTools and HttpTestingTools.
The provided http tools includes the described interceptor into its interceptors.
interceptor
The described interceptor instance.
The instance is typed according to the passed interceptor in interceptorTestBed(..)
.
Example :
- function
- Class
it('should ', tb(({ interceptor }) => {
interceptor(mockReq, mockNext).subscribe(..);
}));
it('should ', tb(({ interceptor }) => {
expect(interceptor.prop).toEqual('foo');
}));
inspect
Tools to inspect outgoing requests and incoming responses.
request(..)
Inspect the passed request into the described interceptor.
Example :
it('should set custom header before send', tb(({ inspect, rx }, done) => {
const req = new HttpRequest('GET', '/test');
expect(req.headers.has('x-custom-header')).toBeFalse();
rx.remind = inspect.passRequest(req).subscribe({
next: (interceptedReq) => {
expect(interceptedReq.headers.has('x-custom-header')).toBeTrue();
done();
},
});
}));
successResponse(..)
Inspect the passed http response into the described interceptor.
Example :
it('should ', tb(({ inspect, rx }, done) => {
const mockRes = new HttpResponse({ body: {} });
rx.remind = inspect.successResponse(mockRes).subscribe({
next: (res) => {
// ... expectations
done();
},
});
}));
errorResponse(..)
Inspect the passed http error response into the described interceptor.
Example :
it('should ', tb(({ inspect, rx }, done) => {
const mockErr = new HttpErrorResponse({ error: 'Error' });
rx.remind = inspect.errorResponse(mockErr).subscribe({
error: (err) => {
// ... expectations
done();
},
});
}));
Assertion options
For specific test, you enable/disable options that override the test bed options.
- function
- Class
describe('appInterceptor', () => {
const tb = interceptorTestBed(appInterceptor());
it('should ', tb((tools) => {
// ... expectations
}, {} /* 👈 here */));
});
describe('AppInterceptor', () => {
const tb = interceptorTestBed(AppInterceptor);
it('should ', tb((tools) => {
// ... expectations
}, {} /* 👈 here */));
});
verifyHttp
Same as options verifyHttp but only for the current assertion.
InterceptorTestBed
import(..)
Same as options imports but with chaining methods.
Example :
- function
- Class
describe('appInterceptor', () => {
const tb = interceptorTestBed(appInterceptor())
.import(SharedModule)
.import([ThirdPartyModule, MaterialModule]);
});
describe('AppInterceptor', () => {
const tb = interceptorTestBed(AppInterceptor)
.import(SharedModule)
.import([ThirdPartyModule, MaterialModule]);
});
provide(..)
Same as options providers but with chaining methods.
Example :
- function
- Class
describe('appInterceptor', () => {
const tb = interceptorTestBed(appInterceptor())
.provide(NotifService)
.provide([StoreService, { provide: MY_TOKEN, useValue: mockValue }]);
});
describe('AppInterceptor', () => {
const tb = interceptorTestBed(AppInterceptor)
.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.
- function
- Class
describe('appInterceptor', () => {
const tb = interceptorTestBed(appInterceptor())
.inject('auth', AuthService);
it('should ', tb(({ injected: { auth } }) => {
// ... expectations
}));
});
describe('AppInterceptor', () => {
const tb = interceptorTestBed(AppInterceptor)
.inject('auth', AuthService);
it('should ', tb(({ injected: { auth } }) => {
// ... expectations
}));
});
setup(..)
Setups extra action using the enhanced tools.
Works only for beforeEach
and afterEach
.
Example :
- function
- Class
describe('appInterceptor', () => {
const tb = interceptorTestBed(appInterceptor())
inject('store', StoreService);
beforeEach(tb.setup(({ injected: { store } }) => {
store.foo = true;
}));
});
describe('AppInterceptor', () => {
const tb = interceptorTestBed(AppInterceptor);
beforeEach(tb.setup(({ interceptor }) => {
interceptor.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
.
- function
- Class
describe('appInterceptor', () => {
const tb = interceptorTestBed(appInterceptor(), { autoCompile: false });
beforeEach(async () => {
// ... third party setup
await tb.compile();
});
});
describe('AppInterceptor', () => {
const tb = interceptorTestBed(AppInterceptor, { autoCompile: false });
beforeEach(async () => {
// ... third party setup
await tb.compile();
});
});
itShouldCreateInterceptor(..)
Create the "should create" for the described interceptor.
To be used when there are no apparent or relevant tests to be performed.
- function
- Class
describe('appInterceptor', () => {
itShouldCreateInterceptor(appInterceptor());
});
describe('AppInterceptor', () => {
itShouldCreateInterceptor(AppInterceptor);
});