Skip to content

[lexical-website] Documentation Update: Rewrite testing guide#8811

Merged
etrepum merged 6 commits into
facebook:mainfrom
mayrang:docs/testing-guide
Jul 9, 2026
Merged

[lexical-website] Documentation Update: Rewrite testing guide#8811
etrepum merged 6 commits into
facebook:mainfrom
mayrang:docs/testing-guide

Conversation

@mayrang

@mayrang mayrang commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Description

The existing testing page was a stub with outdated npm run commands and no guidance on how to actually write tests. The most common question from contributors — "why doesn't userEvent.type() work?" — wasn't addressed anywhere.

This rewrites the page to cover the three test tiers (unit/browser/e2e) with working code examples, explains the jsdom contentEditable limitation, and documents practical patterns like {discrete: true}, editor cleanup with using, and the compose() helper for IME browser tests.

Test plan

  • npx docusaurus build — clean, no errors.
  • Dev server at localhost:3010/docs/testing — renders correctly, admonition displays, code blocks highlighted, table formatted.
  • Sidebar placement between Collaboration and FAQ confirmed.
  • All code examples verified against the actual codebase: imports resolve, API signatures match, compose() call matches LexicalComposition.test.ts usage.
  • $getRoot().selectEnd() + insertText() and CONTROLLED_TEXT_INSERTION_COMMAND tested in actual vitest runs.

@vercel

vercel Bot commented Jul 8, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Jul 8, 2026 11:22pm
lexical-playground Ready Ready Preview Jul 8, 2026 11:22pm

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 8, 2026

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth adding some vitest browser tests to one of the examples (maybe one of the more modern ones) so that people have a demo that can be extracted


:::info

jsdom does not implement `contentEditable` editing. In a real browser, a keystroke inside a `contentEditable` element goes through the browser's native editing engine, which modifies the DOM and fires `beforeinput`/`input` events that Lexical observes. jsdom has no such engine, so dispatching synthetic `InputEvent`s does nothing — there is nothing to actually insert the text into the DOM.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be a great idea, once the input handling is refactored, to put together some more precise documentation for how this all works (on another page)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do — I'll plan a dedicated input handling page once the refactoring settles.

### What you cannot test in jsdom

We use [Playwright](https://playwright.dev/) for running E2E tests in Chromium, Firefox and WebKit. Before running these tests, make sure to install the necessary browsers using the following command:
- Typing by dispatching `KeyboardEvent` or `InputEvent` on the DOM

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the caveat to this is that we probably do want a KeyboardEvent for the "non-text-input" events that get processed as KEY_ENTER_COMMAND, KEY_TAB_COMMAND, the arrow keys, shortcuts, etc. Whether they start as DOM dispatch or editor.dispatchCommand.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a paragraph after the "cannot test" list clarifying that KeyboardEvent dispatch works for non-text-input events (KEY_ENTER_COMMAND, KEY_TAB_COMMAND, KEY_ARROW_* commands, shortcuts).


### Editor cleanup with `using`

`buildEditorFromExtensions` returns an editor with a `dispose()` method and `Symbol.dispose` support. Declaring it with `using` automatically cleans up registrations at the end of the block:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be worth noting that this is only applicable to unit tests, anything that runs in a browser can not use this syntax yet because it's not available in all of the supported engines

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated — the section now says using only works in unit tests (jsdom) and points to editor.dispose() / onTestFinished for browser tests.

mayrang added 2 commits July 8, 2026 14:57
…sting guide

- Add KeyboardEvent caveat for non-text-input commands in jsdom
- Note that `using` syntax is browser-limited; recommend onTestFinished
- Add vitest browser test setup to website-toolbar example
- Fix npx → pnpm exec for playwright install
- Trim redundant catch-all bullet and formulaic closer
@mayrang

mayrang commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

Added vitest browser tests to website-toolbar — it has vitest.config.mts with Playwright browser mode and 4 tests (DOM reconciliation, selection API text insertion, FORMAT_TEXT_COMMAND with DOM verification, getBoundingClientRect layout). The doc links to it as "a complete working setup with vitest browser mode configured from scratch."

etrepum
etrepum previously approved these changes Jul 8, 2026
@etrepum etrepum dismissed their stale review July 8, 2026 13:21

It looks like the integration tests aren’t recognizing that this needs a playwright install now

The website-toolbar example now includes vitest browser tests that
require Playwright. The integration test workflow was missing the
setup-playwright step, causing CI failure.
@etrepum

etrepum commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

playwright install needs to be run from the example, there should be some code to do this because other examples have playwright tests. afk so hard for me to look into exactly what the right change is

@etrepum etrepum added this pull request to the merge queue Jul 9, 2026
Merged via the queue into facebook:main with commit 74aa98f Jul 9, 2026
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants