question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Typings] improve userEvent.type typings to only return a Promise when using delay is set

See original GitHub issue

userEvent.type always returns a promise, but we only need to await it if we’re using the { delay: number } as the third argument, according to the docs

// this returns a Promise, although it is not used
userEvent.type(getByRole('button'))

In js that’s fine, but in TS, the compiler or eslint might warn you about having floating promises.

that could force the TS users to always use await even though it might not be necessary - it’s different from js where we could just not await it.

A possible solution could be to use type overloading when defining the function type, so it returns Promise<void> if { delay: number } is passed, but no value is provided, return void

I think that could be achieved with the following code

type TypeWithoutDelay = (element: TargetElement, text: string, userOpts?: Omit<ITypeOpts, 'delay'> ) => void
type TypeWithDelay = (element: TargetElement, text: string, userOpts: Omit<ITypeOpts, 'delay'> & { delay: number }) => Promise<void>
// then on userEvent.type
declare const userEvent: {
  //...
  type: TypeWithDelay & TypeWithoutDelay
  //...
}

That should allow the inference to work

skipping options image

not passing delay image image

using delay: number (which gives us the Promise as expected) image image

While this works, I wanted to read your thoughts as the code does return a Promise (instead of void), just that it is not required to be awaited, so it could be misleading, or perhaps it’s not worth the effort. I could do the PR as it is just as simple as updating the typings with that option.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

github_iconTop Results From Across the Web

userEvent.type's delay hangs forever · Issue #565 - GitHub
Each block of events for a character is delayed per setTimeout . You don't need to use real timers. You just can't await...
Read more >
user-event v13 - Testing Library
Note: All userEvent methods are synchronous with one exception: when delay option used with userEvent.type as described below.
Read more >
Why clear method not exist on @testing-library/user-event
But the documentation points out to clear() method. Doc => Doc. This is first time I use this library. Anyone knows what the...
Read more >
Play function - Storybook - JS.ORG
Play functions are small snippets of code executed after the story renders. Enabling you to interact with your components and test scenarios that...
Read more >
How to Use Promise.all() - Dmitri Pavlutin
all(). Promise.all() is a built-in helper that accepts an array of promises (or generally an iterable). The function returns ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found