【发布时间】:2020-01-04 22:15:07
【问题描述】:
我刚刚发现了这种使用 jest 模拟 axios 的有用方法,但是,如果我多次调用具有不同 url 的 axios,我如何指定 url 和根据 url 返回的值? 有什么方法可以在不使用 3rd 方库的情况下做到这一点?
谢谢
// users.test.js
import axios from 'axios';
import Users from './users';
jest.mock('axios');
test('should fetch users', () => {
const users = [{name: 'Bob'}];
const resp = {data: users};
axios.get.mockResolvedValue(resp);
// or you could use the following depending on your use case:
// axios.get.mockImplementation(() => Promise.resolve(resp))
return Users.all().then(data => expect(data).toEqual(users));
});
【问题讨论】:
标签: javascript unit-testing jestjs axios