Let's say I have a thunk:
import fetchSomeData from '/path/to/fetchSomeData'
const myAction = () => async dispatch => {
...
const data = await dispatch(fetchSomeData()) // `fetchSomeData` is itself a thunk
...
}
And in my test file, I want to specify what the data is I want the fetchSomeData function to return:
import fetchSomeData from '/path/to/fetchSomeData'
import myAction from '/path/to/myAction'
jest.mock('/path/to/fetchSomeData')
describe('test function', () => {
beforeEach(() => { jest.clearAllMocks() })
it('tests the function', async () => {
const dispatchMock = jest.fn()
fetchSomeData.mockResolvedValueOnce({ value: 'some dummy data' })
await myAction()(dispatchMock)
expect(dispatchMock).toHaveBeenCalledTimes(1)
})
})
However, when doing this, the data that I'm expecting to be my dummy data, always comes back as undefined.
What am I doing wrong?
In our package.json, we have jest 27.5.1 and babel-jest 27.1.0
Because the
dispatchfunction is mocked. You should use the real implementation ofstore.dispatch.e.g.
fetchSomeData.js:index.js:index.test.js:Test result:
package versions: