[英]How to return custom 401 error status in axios mock adapter
I am mocking my login api call with jest and axios-mock-adapter plugin. 我在用jest和axios-mock-adapter插件嘲笑我的登录api调用。
Here is my simple login api mocking code 这是我简单的登录api模拟代码
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import Config from 'react-native-config';
test('It return error', async (done) => {
// setup
const formdata = { _username: 'test', _password: 'test', _submit: 'Log in' };
const mock = new MockAdapter(axios);
const mockData = { code: 401, message: "Bad credentials" };
//mocking
mock
.onPost(Config.loginURL, formdata)
.reply(401, mockData);
//expect
const response = await login(formdata);
done();
expect(response.status).toBe(401);
});
This code works fine when I return 200 code as reply but not working when I try 401 or 500 as error status and returns 当我返回200条代码作为答复,但当我尝试401或500条错误状态并返回时,此代码无法正常工作
Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error: I tried to increase the timeout but nothing happens. 超时-在jest.setTimeout.Error指定的5000ms超时内未调用异步回调:我试图增加超时,但没有任何反应。
Need help. 需要帮忙。
Oh my bad I forgot how async await works :) Here is the code 哦,我不好,我忘了异步等待如何工作:)这是代码
//expect
let response = {};
try {
response = await login(formdata);
} catch (error) {
response = error;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.