UnhandledPromiseRejectionWarning: TypeError: Caught error after test environment was torn down

Viewed 10476

Get Error when test faild case in post request image for Error when run test

Using Reactjs framework version 16

axios-hooks for api requests

react-testing-library for test

UnhandledPromiseRejectionWarning: TypeError: Caught error after test environment was torn down

Cannot read property 'nodeType' of null (node:171) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)

test code

test('If user enters an invalid email or password', async () => {
  const { getByText, getByLabelText, getByTestId } = render(
    <Provider store={mockStore}>
      <Login />
    </Provider>
  )
  fireEvent.change(getByLabelText('Email'), {
    target: { value: 'test@emailing.com' }
  })
  fireEvent.change(getByLabelText('Password'), {
    target: { value: 'password' }
  })

  getByText('Sign in').click()
  await waitForDomChange()
  await act(async () => {
    try {
      await MockAxios.mockError({
        code: 400,
        data: {
          response: {
            data: {
              message: "This password is incorrect. Please re-enter your password."
            }
          }
        }

      })
    } catch (error) {
      console.log(error)
    }
  })
  await waitForDomChange()
  expect(MockAxios).toHaveBeenCalledTimes(1)
  await expect(getByTestId('error-msg')).toBeInTheDocument()
})

(code is tested) Using axios hooks make request then send error to form

 const [{ data, loading, error }, exec] = useAxios(
    {
      method: 'POST',
      url: 'oauth/token/',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    },
    { manual: true }
  )  

return (
    <>
      {(loading || userDataLoading) && <Loader />}
      <Form
        onSubmit={onSubmit}
        error={error}
      />
    </>
  )
0 Answers
Related