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

[test] Use vitest's userEvent implementation in browser tests #1314

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

michaldudak
Copy link
Member

Vitest docs don't recommend using React Testing Library's userEvent in browser tests (see the warning box in https://vitest.dev/guide/browser/#examples).

This PR changes the imported library depending on the environment in which the tests run.

Copy link

netlify bot commented Jan 9, 2025

Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 22d74a2
🔍 Latest deploy log https://app.netlify.com/sites/base-ui/deploys/677f8c04f15e560008bce556
😎 Deploy Preview https://deploy-preview-1314--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@@ -65,15 +65,15 @@ describe('<Accordion.Root />', () => {
expect(trigger).to.have.attribute('aria-expanded', 'false');
expect(queryByText(PANEL_CONTENT_1)).to.equal(null);

await user.pointer({ keys: '[MouseLeft]', target: trigger });
await user.click(trigger);
Copy link
Member Author

Choose a reason for hiding this comment

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

Vitest doesn't have the pointer method but clicks should work the same way in these cases

<Dialog.Trigger>Trigger 1</Dialog.Trigger>
<Dialog.Portal>
<Dialog.Popup data-testid="popup1">
<button onClick={() => setShowNested(!showNested)}>toggle</button>
Copy link
Member Author

Choose a reason for hiding this comment

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

Vitest functions are more strict about visibility and accessibility of the element to interact with.

const result = await originalRender(element, options);
await flushMicrotasks();
return result;
});

if (!isJSDOM) {
const { userEvent: vitestUserEvent } = await import('@vitest/browser/context');
Copy link
Member Author

Choose a reason for hiding this comment

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

This must be imported dynamically as the static import fails when executed outside of the browser environment.

}

return async (...args: unknown[]) => {
await act(async () => target[prop](...args));
Copy link
Member Author

Choose a reason for hiding this comment

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

This isn't ideal. Perhaps we could change the implementation of act to no-op when running in the browser and get rid of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant