节点Jest Axios没有被模拟

时间:2020-06-16 17:45:29

标签: javascript node.js jestjs axios

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拒绝被嘲笑。

0 个答案:

没有答案
相关问题