我在 axios 拦截器中实现了刷新令牌逻辑。简化:
const http = axios.CreateInstance();
http.interceptors.response.use(_ => _, async (error) => {
try {
return await http(error.config);
} catch (e) {
// this is not called in my test
location.href = '/logout';
return Promise.reject(e);
}
});
在我的测试中,我打电话给
axiosMock.onGet('/token').reply(401);
axiosMock.onGet('/test', undefined, {}).reply(200, {});
try {
sut.get('/test');
} catch (e) {
expect(location.href).toBe('/logout');
}
问题是代码并没有在测试中抛出异常,而是在真实代码中。所以测试从http(error.config);