Unit testing interceptors
See original GitHub issueSo, I have added this response interceptor that would basically just clear out the localstorage items if the status is 401. I haven’t have found luck with unit testing it:
axios.interceptors.response.use((response) => {
if (response.status === 401) {
storage.clear();
}
return response;
});
I have tried moxios but haven’t been lucky with it when it comes to interceptors.
Issue Analytics
- State:
- Created 7 years ago
- Comments:14 (2 by maintainers)
Top Results From Across the Web
How to unit test an HTTP interceptor in Angular using Jasmine
How to unit test an HTTP interceptor in Angular using Jasmine · Execute request using httpMock and inspect actual request/response. angular.io/ ...
Read more >How to write Unit Test Cases for Angular HTTP Interceptor
In this article we are going write unit test cases for an interceptor (HttpConfigInterceptor) which will add required header configuration to ...
Read more >How to Test Angular Response Interceptors - Embrace IT
Testing Angular interceptors is easy - once you know how. Find out everything you need to start unit testing your interceptors right away....
Read more >How to test a http interceptor in Angular application - ng-mocks
To test an interceptor we need the interceptor itself and its module. Please consider refactoring if the interceptor is defined by useValue ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
To be more specific, if I have the following interceptor:
This is how I test it with jest:
I hope this is still useful, even if the original issue is dated almost a year ago 😃
Hi @okovalov, I think the issue is because of mocking
axiosmodule.with mocking
output:
without mocking
output:
here is the working example
cc - @rs-8