-
Notifications
You must be signed in to change notification settings - Fork 42
Description
Hi!
We're using this library in a project built with Create React App, and have followed the async / await testing pattern outlined in #28.
I'm trying to upgrade us to CRA 4, but doing so is breaking all of these tests with Error: No request to respond to!. Bumping major CRA version bumps a load of sub-dependencies, so guessing one of these is the actual cause (jest seems most likely) - I'm happy to check other package versions before/after the upgrade if there are any in particular you think will be relevant!
We were using version 2.3.0 of this library, but can also reproduce the issue on the latest version (4.2.1). I've put together a SRE of three files to demonstrate what's happening.
Our axios instance, defined in axiosInstance.ts
import axios from 'axios'
export const axiosInstance = axios.create({
baseURL: '/api',
timeout: 20000,
withCredentials: true,
headers: { 'X-Csrf-Prevention': 'true' },
})
A fake API to test in sreApi.ts
import { AxiosResponse } from 'axios'
import { axiosInstance } from './axiosInstance'
export const callEndpoint = async (data: string): Promise<AxiosResponse> => {
return axiosInstance.post('/endpoint', data)
}
The test that fails after the upgrade, but passes before in sreApi.test.ts
import { callEndpoint } from './sreApi'
import mockAxios from 'jest-mock-axios'
it('should call the correct endpoint', async () => {
const promise = callEndpoint('some-data')
mockAxios.mockResponse({ data: 'response' }) <-- blows up here
const result = await promise
expect(mockAxios.post).toHaveBeenCalledWith('/endpoint', 'some-data')
expect(result.data).toBe('response')
})
In the above example, the mockResponse line blows up with:
Error: No request to respond to!
at Function.mockResponse (/home/me/sre-package/node_modules/jest-mock-axios/lib/mock-axios.ts:170:15)
at Object.<anonymous> (/home/me/sre-package/src/sre/sreApi.test.ts:6:13)
at Promise.then.completed (/home/me/sre-package/node_modules/jest-circus/build/utils.js:276:28)
at new Promise (<anonymous>)
at callAsyncCircusFn (/home/me/sre-package/node_modules/jest-circus/build/utils.js:216:10)
at _callCircusTest (/home/me/sre-package/node_modules/jest-circus/build/run.js:212:40)
at processTicksAndRejections (internal/process/task_queues.js:85:5)
at _runTest (/home/me/sre-package/node_modules/jest-circus/build/run.js:149:3)
at _runTestsForDescribeBlock (/home/me/sre-package/node_modules/jest-circus/build/run.js:63:9)
at run (/home/me/sre-package/node_modules/jest-circus/build/run.js:25:3)
at runAndTransformResultsToJestFormat (/home/me/sre-package/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:176:21)
Interestingly, reordering the test like this still fails but does pass the call assertion, so mockAxios.post is definitely still being called:
it('should call the correct endpoint', async () => {
const promise = callEndpoint('some-data')
expect(mockAxios.post).toHaveBeenCalledWith('/endpoint', 'some-data')
mockAxios.mockResponse({ data: 'response' }) <-- still blows up here
const result = await promise
expect(result.data).toBe('response')
})
Versions that work:
jest: 24.9.0
react-scripts: 3.4.4
axios: 0.18.1
jest-mock-axios: 4.2.1
Versions that don't work (after upgrade):
jest: 26.6.0
react-scripts: 4.0.1
axios: 0.18.1
jest-mock-axios: 4.2.1