ctimmerm / axios-mock-adapter

Axios adapter that allows to easily mock requests
MIT License
3.44k stars 244 forks source link

abort pending axios request #282

Open brucejcw opened 4 years ago

brucejcw commented 4 years ago

in my testcase, i rendered a react component called AdLog, which will do axios query in componentDidMount.
i have wrote several testcases,

  1. Should render agent log success => not dealing with the response
  2. Should render m log success' => not dealing with the response
  3. 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",