Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled promise rejection in unit tests #1140

Open
Xin00163 opened this issue May 4, 2023 · 1 comment
Open

Unhandled promise rejection in unit tests #1140

Xin00163 opened this issue May 4, 2023 · 1 comment

Comments

@Xin00163
Copy link

Xin00163 commented May 4, 2023

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

  • OS: [e.g. Windows / Linux / macOS / iOS / Android]
  • Browser: [e.g. Chrome, Safari]
  • Autocomplete version: [e.g. 1.0.0]
@Haroenv
Copy link
Contributor

Haroenv commented May 22, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants