【发布时间】:2020-03-09 18:25:04
【问题描述】:
我正在为我的异步操作创建者编写测试,但是当我的测试运行时出现超时错误。我试过了
- 莫西奥斯
- 增加笑话超时
- 模拟适配器
我目前正在再次尝试 Moxios。
myTest(已编辑以反映更改):
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import moxios from 'moxios'
import * as actions from './actions'
import mockData from './mockData'
const middleware = [thunk]
const mockStore = configMockStore(middleware)
describe('action tests: ', () => {
beforeEach(function() {
moxios.install()
}
afterEach(function() {
moxios.uninstall()
}
it('should get data', async done => {
moxios.stubRequest('/data/*', { status: 201, response: mockData })
const expectedActions = [{ type: actions.dataActions.RECEIVED_DATA }]
const store = mockStore({ data: [] })
await store.dispatch(actions.getData()).then(() => {
expect(store.getActions()).toEqual(expectedActions)
})
done();
}, 10000)
})
我得到的错误是:Timeout - Async callback was not invoked within the 10000ms timeout...
通过查看我的代码和覆盖率,我可以看到 promise 没有被使用,因为 .then() 部分没有根据我的代码覆盖率执行。那么我的测试用例做错了吗?我开玩笑地查看了其他人对 Axios 的实现,但我总是收到这个超时错误。
正在测试的代码:
export cosnt getData= () => {
return dispatch => {
dispatch(isFetching())
return dataApi.retrieveData().then(res => {
dispatch(updateData(res))
})
}
}
【问题讨论】:
-
你能发布你正在测试的代码,以便我们看看出了什么问题
-
@RedBaron,我添加了正在测试的代码。我也应该添加 API 函数吗?它是 axios 的标准 GET
标签: reactjs unit-testing react-redux jestjs axios