可以使用async/await或者.then()方法来处理构造函数的返回值。
示例代码:
// 使用async/await处理MockAdapter构造函数返回的promise import axios from 'axios' import MockAdapter from 'axios-mock-adapter'
describe('test mock axios', () => { let mock
beforeEach(() => { mock = new MockAdapter(axios) })
afterEach(() => { mock.restore() })
it('should mock axios', async () => { mock.onGet('http://test.com').reply(200, { message: 'mocked' })
const res = await axios.get('http://test.com')
expect(res.data.message).toBe('mocked')
}) })
// 使用.then()方法处理MockAdapter构造函数返回的promise import axios from 'axios' import MockAdapter from 'axios-mock-adapter'
describe('test mock axios', () => { let mock
beforeEach(() => { mock = new MockAdapter(axios) })
afterEach(() => { mock.restore() })
it('should mock axios', () => { mock.onGet('http://test.com').reply(200, { message: 'mocked' })
axios
.get('http://test.com')
.then(res => {
expect(res.data.message).toBe('mocked')
})
.catch(err => {
console.log(err)
})
}) })