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

userEvent.keyboard always releases pressed key with playwright provider #6082

Open
6 tasks done
edoardocavazza opened this issue Jul 10, 2024 · 6 comments · Fixed by #6083 · May be fixed by #6088
Open
6 tasks done

userEvent.keyboard always releases pressed key with playwright provider #6082

edoardocavazza opened this issue Jul 10, 2024 · 6 comments · Fixed by #6083 · May be fixed by #6088
Labels
feat: browser Issues and PRs related to the browser runner p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@edoardocavazza
Copy link

Describe the bug

I am trying to use the new Interactive API to test the behavior of a button.
It seems that every keyboard command is automatically released even when using the {Enter>} syntax.

Reproduction

https://github.com/edoardocavazza/vitest-keyboard

System Info

System:
    OS: macOS 14.2.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 261.33 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.19.0 - ~/.nvm/versions/node/v18.19.0/bin/node
    Yarn: 1.22.22 - /opt/homebrew/bin/yarn
    npm: 10.2.3 - ~/.nvm/versions/node/v18.19.0/bin/npm
    pnpm: 8.6.3 - ~/Library/pnpm/pnpm
    bun: 1.0.30 - ~/.bun/bin/bun
  Browsers:
    Chrome: 126.0.6478.127
    Safari: 17.2.1
  npmPackages:
    @vitest/browser: ^2.0.1 => 2.0.1 
    vitest: ^2.0.1 => 2.0.1

Used Package Manager

npm

Validations

@edoardocavazza edoardocavazza changed the title userEvent.keyboard command always release pressed keys with playwright provider userEvent.keyboard always releases pressed key with playwright provider Jul 10, 2024
@sheremet-va
Copy link
Member

sheremet-va commented Jul 10, 2024

Hmm, you are right. It should only be released in userEvent.type:

https://github.com/testing-library/user-event/blob/main/src/utility/type.ts#L40

@sheremet-va
Copy link
Member

Interestingly enough, your code also doesn't work with userEvent (you can try preview provider which uses unmodified @testing-library/user-event library).

@edoardocavazza
Copy link
Author

Hi @sheremet-va, thanks for the fix!
I have investigated why the second part of the test wont work even with the original user-event library.
I have found out that using the direct api (e.g. userEvent.keyboard()) will create a new context for each call, so it loses the pressed keys state.

It works as expected when using the setup() method:

const device = userEvent.setup();
await userEvent.keyboard('{Enter>}');
// triggers the keyup event
await userEvent.keyboard('{Enter/}');

Any plan to support the setup method?

@edoardocavazza
Copy link
Author

Alternatively, IMHO it would be better if vitest kept the state of pressed keys. Since the interaction using playwright/webdriver is not emulated, the key actually reamains unreleased forever 😅

@sheremet-va
Copy link
Member

Interesting! I didn't know how userEvent keeps this under the hood, but it might be hard to replicate this specific behavior since it's done server side (maybe we can return the state somehow).

I agree it should work more like userEvent.setup().keyboard()

Any plan to support the setup method?

Currently setup just returns the same userEvent, not sure how we can support it fully and if we really need to.

@sheremet-va sheremet-va reopened this Jul 11, 2024
@sheremet-va sheremet-va added feat: browser Issues and PRs related to the browser runner p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Jul 11, 2024
@sheremet-va
Copy link
Member

I think what happens with .setup is that this line is called:

https://github.com/testing-library/user-event/blob/d0362796a33c2d39713998f82ae309020c37b385/src/keyboard/index.ts#L30

In Vitest currently it's scoped to the same keyboard call:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: browser Issues and PRs related to the browser runner p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
2 participants