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

Vitest browser mode userEvent type not recognizing special chars #6871

Closed
6 tasks done
Seboran opened this issue Nov 6, 2024 · 4 comments · Fixed by #6873
Closed
6 tasks done

Vitest browser mode userEvent type not recognizing special chars #6871

Seboran opened this issue Nov 6, 2024 · 4 comments · Fixed by #6873
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

@Seboran
Copy link

Seboran commented Nov 6, 2024

Describe the bug

When using vitest browser mode, the following test fails:

import { expect, test } from 'vitest'
import { render } from 'vitest-browser-vue'
import HelloWorld from './HelloWorld.vue'
import { userEvent } from '@vitest/browser/context'


test('renders name', async () => {
  const { getByText, getByRole } = render(HelloWorld, {
    props: { name: 'Vitest' },
  })
  await userEvent.type(getByRole('textbox'), "éèù")
})

Fails with Error: keyboard.down: Unknown key: "é"

é is a valid key in AZERTY french keyboards. I cannot use userEvent.fill because I want to test the specific press of special char keys.

With playwright I can type the é key with their equivalent method.

Reproduction

clone https://github.com/Seboran/special-chars-vitest

pnpm i
pnpm test:browser

get error message Error: keyboard.down: Unknown key: "é"

System Info

System:
    OS: macOS 15.1
    CPU: (10) arm64 Apple M2 Pro
    Memory: 796.86 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.5.0 - ~/.nvm/versions/node/v20.5.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v20.5.0/bin/yarn
    npm: 10.1.0 - ~/.nvm/versions/node/v20.5.0/bin/npm
    pnpm: 9.12.1 - ~/Library/pnpm/pnpm
    bun: 1.1.29 - ~/.bun/bin/bun
  Browsers:
    Safari: 18.1
  npmPackages:
    @vitejs/plugin-vue: ^5.1.4 => 5.1.4 
    @vitest/browser: ^2.1.4 => 2.1.4 
    @vitest/eslint-plugin: 1.1.7 => 1.1.7 
    vite: ^5.4.10 => 5.4.10 
    vitest: ^2.1.4 => 2.1.4

Used Package Manager

pnpm

Validations

@Seboran
Copy link
Author

Seboran commented Nov 6, 2024

it looks like it's a duplicate of microsoft/playwright#7396

but it puzzles me a bit why it does work with page.keyboard.type from playwright experimental ct, and not userEvent.type from playwright...

This works for example:

import { test, expect } from '@playwright/experimental-ct-react'

test('test', async ({ mount, page }) => {
/** ... **/
const text = 'éè'
  for (const c of text) {
    await page.keyboard.type(c)
  }
/** ... **/

it makes me wonder how could I achieve the same "keypress by keypress" test using userEvent from vitest?

@hi-ogawa hi-ogawa added the feat: browser Issues and PRs related to the browser runner label Nov 7, 2024
@hi-ogawa
Copy link
Contributor

hi-ogawa commented Nov 7, 2024

Vitest's userEvent.type is implemented based on Playwright's page.keyboard.down, but probably they have a different implementation for page.keyboard.type, which works somehow. It looks like they have test cases like this https://github.com/microsoft/playwright/blob/50775698ae13642742f2a1e8983d1d686d7f192d/tests/page/page-keyboard.spec.ts#L291-L306

Okay, maybe this is a magic we need to copy. It looks like they fallback to insertText when not in usKeyboardLayout
https://github.com/microsoft/playwright/blob/50775698ae13642742f2a1e8983d1d686d7f192d/packages/playwright-core/src/server/input.ts#L87-L98

@hi-ogawa hi-ogawa added the p3-minor-bug An edge case that only affects very specific usage (priority) label Nov 7, 2024
@hi-ogawa
Copy link
Contributor

hi-ogawa commented Nov 7, 2024

@Seboran Can you try this pre-release #6873 (comment)? I'm not exactly sure what Playwright's insertText does in terms of keydown/keyup events. It would be useful to know if this works for your use case.

@Seboran
Copy link
Author

Seboran commented Nov 7, 2024

@hi-ogawa i confirm this fixes the bug! 🎉 the Textbox do get the typed keys and I can test my "key by key" behaviour.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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
Development

Successfully merging a pull request may close this issue.

2 participants