我想用自定义错误对象来模拟axios的post请求。
获取令牌.ts
const GetToken = async (params:ITokenParams):Promise<ITokenResult | IError>=>{
try{
const api = axios.create({
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json",
},
});
const {data} = await api.post<ITokenResult>("https://someURL", { ...params });
return data;
}
catch(e){
let err:IError={
error:true,
module:path.basename(__filename),
method:GetToken.name
};
if(e instanceof AxiosError && e.response?.data?.errors){
let errorsArray:IApiError[]=e.response?.data?.errors;
err.message=errorsArray.length>0?errorsArray[0].detail : e.message;
}
else if (e instanceof Error){
err.message=e.message
}
return err;
}
}
export default GetToken;
自定义错误对象是 mockResponse。我想返回一个状态为401的响应。在 response 中,数据对象应该包括 errors 数组。
获取令牌.测试.ts
import {axiosInstance} from '../../utils/axios-instance';
import GetToken from '@App/api/oauth/getToken';
jest.mock('../../utils/axios-instance.ts');
describe('Token from OAuth', () => {
test('Return error for expired code', async () => {
const mockResponse = {
data:{},
status: 401,
statusText:"Unauthorized",
headers: {},
config: {} ,
response:{
data: { errors: [{ detail: 'a' }] }
}
};
const response = {
error: true,
module: 'getToken.ts',
method: 'GetToken',
message: 'a'
};
const mockedAxiosInstance = axiosInstance as jest.MockedFunction<typeof axiosInstance>;
(mockedAxiosInstance.post as jest.Mock).mockRejectedValueOnce(mockResponse);
const result= await GetToken({
client_id:process.env.APPLICATION_ID,
client_secret:process.env.APPLICATION_SECRET,
code:"some-codes",
grant_type:"authorization_code"
});
expect(result).toEqual(response);
})
})
** axios -示例.ts**
import axios from "axios";
export const axiosInstance = axios.create({
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json",
}
})
测试未通过。
Object {
"error": true,
- "message": "a",
+ "message": "Request failed with status code 400",
"method": "GetToken",
"module": "getToken.ts",
}
当我检查catch语句中的代码时,这个块运行
else if (e instanceof Error){
err.message=e.message
}
如何返回我的自定义错误对象?
1条答案
按热度按时间mpbci0fu1#
是的,我解决了2个不同的选项.
由于@jonrsharpe,axios示例应该与. test. ts和. ts文件相同。
不要干扰Axios
安装软件包
npm安装msw--保存设备