Skip to content

feat: add createRootOptions to ComponentRenderOptions#53

Merged
sheremet-va merged 3 commits into
vitest-community:mainfrom
nstepien:root-options
Apr 5, 2026
Merged

feat: add createRootOptions to ComponentRenderOptions#53
sheremet-va merged 3 commits into
vitest-community:mainfrom
nstepien:root-options

Conversation

@nstepien

@nstepien nstepien commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

This allows passing options to React's createRoot

closes #52

This allows passing options to React's `createRoot`

closes vitest-community#52
Comment thread src/pure.tsx
container?: HTMLElement
baseElement?: HTMLElement
wrapper?: React.JSXElementConstructor<{ children: React.ReactNode }>
createRootOptions?: RootOptions

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you add some jsdoc? Can leave others empty but just for this one for now.

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.

Done

@hi-ogawa hi-ogawa changed the title Add createRootOptions to ComponentRenderOptions feat: add createRootOptions to ComponentRenderOptions Apr 3, 2026

@hi-ogawa hi-ogawa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!

@hi-ogawa hi-ogawa requested a review from sheremet-va April 3, 2026 03:01
@sheremet-va

Copy link
Copy Markdown
Member

react-testing-library accepts some of these options directly: https://testing-library.com/docs/react-testing-library/api . Should we do the same? What are your thoughts, @hi-ogawa, @nstepien? It might just be outdated, and we can improve here or follow the same pattern

Documentation at vitest.dev should also be updated: https://github.com/vitest-dev/vitest/blob/main/docs/api/browser/react.md

@hi-ogawa

hi-ogawa commented Apr 4, 2026

Copy link
Copy Markdown
Contributor

Ah good catch. I didn't check what testing library did. I vote for aligning with them if no obvious reason to diverge.

@nstepien

nstepien commented Apr 4, 2026

Copy link
Copy Markdown
Contributor Author

RTL only supports onCaughtError and onRecoverableError (source), so there's some divergence already.

I think a separate object is better:

  • it avoids conflicting names if vitest-browser-react or React add more options in the future
  • it's obvious which options control vitest-browser-react, and which options control React
  • it helps discover what options are offered IMO
  • it's easier to add more options for other stuff that render handles for us
    • for example if React adds new props to StrictMode, it'd be trivial to add { strictModeProps }
    • otherwise destructuring all the options would need to be done carefully, it'd add to the lib's maintenance, and users couldn't make use of new React options as soon as they're available

Those are my thoughts, I can flatten the options if you insist. 👍

Tangentially, maybe vitest-browser-react will allow testing SSR via hydrateRoot in the future.
hydrateRoot has the same options, at least as of writing.
Would the API be similar?

await page.hydrate(<div />, {
  hydrateRootOptions: {}
})

@hi-ogawa hi-ogawa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the thoughts. Now I'm re-approving 😄

@sheremet-va sheremet-va merged commit 8803d53 into vitest-community:main Apr 5, 2026
4 checks passed
@nstepien nstepien deleted the root-options branch April 5, 2026 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pass options to createRoot

3 participants