algolia / autocomplete

🔮 Fast and full-featured autocomplete library
https://alg.li/autocomplete
MIT License
5.06k stars 331 forks source link

Unhandled promise rejection in unit tests #1140

Open Xin00163 opened 1 year ago

Xin00163 commented 1 year ago

Description

Hi all, I have a question about this custom react renderer example: examples/react-renderer/src/Autocomplete.tsx.

In out jest unit tests, we got the below error even if the tests passed. Screenshot 2023-05-04 at 11 52 45

Reproduction

it('should render results when doing a search', async () => {
    const { getByTestId, asFragment } = render(Autocomplete, props)
    fireEvent.change(getByTestId('autocomplete-input'), {
      target: { value: 'a' },
    })
    const searchButton = getByTestId('search-button')
    await waitFor(() => {
      fireEvent.click(searchButton)
    })
    expect(asFragment()).toMatchSnapshot()
  })

Even if it's just

const { getByTestId } = render(Autocomplete, props)
const autocomplete = getByTestId('autocomplete-input')
autocomplete.focus()

You will get the below error:

node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

[UnhandledPromiseRejection: 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(). The promise rejected with the reason "[object Object]".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

One way to fix this is to add a setTiemout

setTimeout(function () {
      autocomplete.focus()
    }, 100)

but this is not ideal.

Could you please advise what is the best way to test?

Environment

Haroenv commented 1 year ago

There's no autocomplete-input testid in that example, do you have a full reproduction?