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

[code-infra] Optimize @testing-library/user-event #43804

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Janpot
Copy link
Member

@Janpot Janpot commented Sep 18, 2024

See this thread

I've seen this reduce the time of running userEvent.keyboard by an order of magnitude, eliminating the motivation to avoid using this library. It comes at the cost of slightly changed semantics. But even with these different semantics, we'd be better off compared to the way we currently test.

It would be best for us to primarily optimize our tests for refactorability. Tests that require updates on every implementation detail change miss the mark. We should aim at tests that maximize interfacing with public API and minimize reliance on implementation details. Therefore, unless we specifically want to test an implementation detail, we should prefer userEvent.

Closes mui/mui-x#14668

@Janpot Janpot changed the title Optimize user-event [code-infra] Optimize @testing-library/user-event Sep 18, 2024
@mui-bot
Copy link

mui-bot commented Sep 18, 2024

Netlify deploy preview

https://deploy-preview-43804--material-ui.netlify.app/

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against 9aaeecb

@Janpot Janpot marked this pull request as ready for review September 18, 2024 09:22
@@ -301,7 +301,7 @@ function render(
);
const result: MuiRenderResult = {
...testingLibraryRenderResult,
user: userEvent.setup(),
user: userEvent.setup({ delay: null }),
Copy link
Member

@oliviertassinari oliviertassinari Sep 18, 2024

Choose a reason for hiding this comment

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

Going off topic, but would this works instead?

Suggested change
user: userEvent.setup({ delay: null }),
export const userEventInstance = userEvent.setup({ delay: null });

Copy link
Member Author

@Janpot Janpot Sep 18, 2024

Choose a reason for hiding this comment

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

I think you want to initialize it per test. At least that's seems to be recommended on their docs. I understand the instance hold state and you can avoid interference between parallel tests this way.

We discourage rendering or using any userEvent functions outside of the test itself - e.g. in a before/after hook

Copy link
Member

@oliviertassinari oliviertassinari Sep 18, 2024

Choose a reason for hiding this comment

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

This is what I'm not clear about. In the source, I didn't see logic that required it to be initialized once for all the tests, this would save CI time for each test and make the DX simpler as well.

Copy link
Member

Choose a reason for hiding this comment

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

Not sure I follow @oliviertassinari. I think we should just follow the Testing Lib recommendation here i.e. setup userEvent once per test.

Copy link
Member

@oliviertassinari oliviertassinari Sep 18, 2024

Choose a reason for hiding this comment

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

I have found testing-library/user-event#1036 (comment) about this. It could be ok

You should use userEvent.setup() after creating the document and before you render() your component. [...] if you hit one of the rare edge cases, it might be hard to debug - especially since most environments don't create a new document for each test.

https://github.com/testing-library/user-event/blob/d0362796a33c2d39713998f82ae309020c37b385/src/setup/setup.ts#L82.

So I don't really see why it's needed in our case since we share the same document between tests.

But no hard point of view. Just that if we want to be greedy, there is maybe an opportunity, but maybe not.

Copy link
Member

@oliviertassinari oliviertassinari Sep 19, 2024

Choose a reason for hiding this comment

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

With Vitest, the need to have a document always setup for each test could become an issue, depending on how parallelism is implemented. I imagine that it's OK, but it might not.

Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

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

Can this break MUI X tests? Do we need to release this in a way that allows some tests to configure this behavior?

Copy link
Contributor

@sai6855 sai6855 left a comment

Choose a reason for hiding this comment

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

To add more context in terms of perfomance improvements from this thread, previously await user.keyboard('{Escape}') took 20ms (+/- 5ms). Now with this change, it's taking 10ms (+/- 4ms)

image

@Janpot
Copy link
Member Author

Janpot commented Sep 18, 2024

Can this break MUI X tests?

Technically yes, but unlikely. We could try it out before merging.

Do we need to release this in a way that allows some tests to configure this behavior?

We could add a parameter to createRenderer, but you can also just set up your own const user = userEvent.setup() instance and use that for a specific test. IMO over time we should work towards moving things out of createRenderer and make the wrapper unnecessary.

Copy link
Member

@aarongarciah aarongarciah left a comment

Choose a reason for hiding this comment

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

Not a single test failed, which is nice. Looks good but as Olivier said maybe we want to test this in MUI X before merging.

@Janpot
Copy link
Member Author

Janpot commented Sep 19, 2024

🙂 Yep, trying this in X breaks the tests, but looks like it's not caused by this change, see mui/mui-x#14671

Edit: Looks like it now fails consistently

Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

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

Let's hold off merging until we figure out what are the issues in X.

@@ -301,7 +301,7 @@ function render(
);
const result: MuiRenderResult = {
...testingLibraryRenderResult,
user: userEvent.setup(),
user: userEvent.setup({ delay: null }),
Copy link
Member

Choose a reason for hiding this comment

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

I've looked into failing tests on mui/mui-x#14668 and I'm not sure we should move forward with this. 🤔
Is it really such a big problem to keep with the existing setTimeout wrapping for each userEvent call? 🤔
I wouldn't like to significantly degrade the DX (I wasn't able to fix all the failing tests without resetting delay to 0 on some of them) of writing tests for a negligible performance difference. 🙈 🤷

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we could let this PR rest until we've

  • made all act async
  • refactor fake timers to scope to single tests only
  • migrated to vitest

and then re-evaluate how broken it is.

Copy link
Member

Choose a reason for hiding this comment

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

It makes sense, let's go with it. 👍

Copy link
Member

@oliviertassinari oliviertassinari Oct 21, 2024

Choose a reason for hiding this comment

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

Is it really such a big problem to keep with the existing setTimeout wrapping for each userEvent call?

@LukasTy I think that it boils down to this: #43757 (comment). If moving from fireEvent to userEvent x2 the CI run time (I don't know if true), then IMHO, we shouldn't. Feedback time on PR is already painfully slow, it feels more important.

I wasn't able to fix all the failing tests without resetting delay to 0 on some of them

Could you pinpoint to why? I mean Is there a technical reason why those setTimeout should be needed for tests to pass?

If the why this is needed is "race conditions", it sounds like the right move is to have { delay: null } in as many places as possible in the codebase as soon as possible to avoid regressions.

then re-evaluate how broken it is

This can work as well.

Copy link
Member

Choose a reason for hiding this comment

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

Could you pinpoint to why? I mean Is there a technical reason why those setTimeout should be needed for tests to pass?

The main problem is the TransitionGroup on Pickers tests.
There are errors thrown about mutating state, because of it.

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

Successfully merging this pull request may close these issues.

7 participants