testing-library / testing-library/user-event

`keyboard()` omits first key when focus changes programmatically

Open
#1,129 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs assessment
Dominant language
TypeScript
Stars
2.3k
Forks
258
PR merge metrics
No merged PRs in 30d

Description

Reproduction example

https://codesandbox.io/p/sandbox/cocky-ardinghelli-pzvt0v

Prerequisites

Test expected behavior in browser

  1. Click checkbox so it gets focus
  2. Type "foo"
  3. See focused input field reading "foo"
Expected behavior

The test should be green because it works in the browser. The first letter "f" should be handled even if focus is changed to the input field programatically while it gets typed.

Actual behavior

The test fails with this result:

Expected the element to have value:
  foo
Received:
  oo

The letter "f" is not registered correctly like it would in the browser.

User-event version

14.4.3

Environment

package.json in codesandbox:

    "jsdom": "^22.0.0",
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/user-event": "^14.4.3",
    "@testing-library/vue": "^7.0.0",
Additional context

I don't know if this is also an issue in React and other frameworks because I am mainly using Vue at the moment.

The actual complete component is a combobox where I want to be able to search from anywhere within the component without having to manually focus the input field for faster UX.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked CodeSandbox reproduction and the package's keyboard() behavior, checking how focus changes while the first key is typed. Compare the simulated result with the browser, then add coverage showing that typing "foo" after programmatic focus records all three characters; done means the regression test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.