Skip to main content

Interceptor TestBed

Custom test bed for testing Interceptor.

info

Works for function (HttpInterceptorFn) and class (that implements HttpInterceptor) interceptor.

Quick example

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).

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​

describe('appInterceptor', () => {
const tb = interceptorTestBed(appInterceptor(), {} /* 👈 here */);

it('should ', tb(() => {
// ... expectations
}));
});

imports​

Default : []

Imports dependencies for the described interceptor.

Example :

const tb = interceptorTestBed(appInterceptor(), {
imports: [SharedModule, MaterialModule],
});

providers​

Default : []

List of providers to be available during tests for the described interceptor.

Example :

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.

describe('appInterceptor', () => {
const tb = interceptorTestBed(appInterceptor());

it('should ', tb((tools /* 👈 here */) => {
// ... expectations
}));
});

InterceptorTools extends BaseTools and HttpTestingTools.

info

The provided http tools includes the described interceptor into its interceptors.

interceptor​

The described interceptor instance.

info

The instance is typed according to the passed interceptor in interceptorTestBed(..).

Example :

it('should ', tb(({ interceptor }) => {
interceptor(mockReq, mockNext).subscribe(..);
}));

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.

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 :

describe('appInterceptor', () => {
const tb = interceptorTestBed(appInterceptor())
.import(SharedModule)
.import([ThirdPartyModule, MaterialModule]);
});

provide(..)​

Same as options providers but with chaining methods.

Example :

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.

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 :

describe('appInterceptor', () => {
const tb = interceptorTestBed(appInterceptor())
inject('store', StoreService);

beforeEach(tb.setup(({ injected: { store } }) => {
store.foo = true;
}));
});

compile()​

To be used when you need to do third party setups before compiling the custom test bed.

warning

It has to be used into beforeEach(..) and autoCompile must be set to false.

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.

describe('appInterceptor', () => {
itShouldCreateInterceptor(appInterceptor());
});