test.js
const fn = require("./module.js")
jest.mock('axios', () => ({
create: () => {
return {
get() {
return true
}
}
},
get: () => true
}))
it('should be mocked', async () => {
const res = await fn()
expect(res).toEqual(true)
})
module.js
const axios = require('axios')
exports.default = () => {
const agent = axios.create();
// Network error here because it is attempting to reach an endpoint
// that is not valid.
return agent.get()
}
总而言之,我收到来自axios请求的网络错误,因为它是无效的网络调用,我正在尝试通过模拟服务来防止此错误。 Axios拒绝被嘲笑。