axios-mock-adapter
axios-mock-adapter copied to clipboard
abort pending axios request
in my testcase, i rendered a react component called AdLog, which will do axios query in componentDidMount.
i have wrote several testcases,
- Should render agent log success => not dealing with the response
- Should render m log success' => not dealing with the response
- Should render log table success => using axios-mock-adapter to mock the response.
the test result is failing due to axios.post will throw exception(e is undefined).
if i remove testcase 1 & 2, or add mockAxios.onAny().reply(200, adData)
in testcase 1 & 2, then the file test will pass.
looks like the request in testcase 1 & 2 are pending, and somehow fired in the 3th case.
I've checked the reset
function, it will clean resetHandlers and resetHistory correctly.
i got confused, is it a bug ?
afterEach(() => {
mockAxios.reset()
})
describe('test', () => {
it('Should render agent log success', () => {
const { queryByLabelText } = render(<AdLog />)
expect(queryByLabelText('Operator Id')).toBeTruthy()
})
it('Should render m log success', () => {
const { queryByLabelText } = render(<AdLog from="m" />)
expect(queryByLabelText('Operator Id')).toBeNull()
})
it('Should render log table success', async () => {
mockAxios.onAny().reply(200, adData)
const { queryByText, getByText, getByPlaceholderText } = render(<AdLog />)
expect(queryByText('No Data')).toBeTruthy()
const searchBtn = getByText('Search', { selector: 'button > span' })
fireEvent.click(searchBtn)
await sleep(100)
expect(queryByText('No Data')).toBeFalsy()
....
"axios-mock-adapter": "^1.18.2",
"@testing-library/react": "^11.0.2",