-
Notifications
You must be signed in to change notification settings - Fork 843
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
EuiFormRow does not work with react-testing-library #4408
Comments
Hey @thomheymann, The purpose of mocking You can re-mock jest.mock('@elastic/eui/lib/services/accessibility/html_id_generator', () => ({
...jest.requireActual('@elastic/eui/lib/services/accessibility/html_id_generator'),
htmlIdGenerator: () => () => `id-${Math.random()}`,
})); |
Thanks for explanation and fix Greg! I'm happy to use this for the time being but am a bit worried about having two different approaches in Kibana now (auto mocking and manual mocking). We're not using auto-mocking elsewhere as far as I'm aware. It took me quite a while to debug this issue and I'm worried about other dev's potentially wasting time on this as well. Jest has the ability to define static mocks. We could provide one for the
Alternatively we could create a custom jest serialiser that replaces id attributes that have been generated using |
TIL about Branching a bit, is there something we could do for our other mocked modules (which are done for non-snapshot reasons) that would make it obvious they differ from the non-test implementation? Perhaps a |
+1 to |
Rereading this and responding to the more fundamental point
EUI has a separate build that was created specifically for the Kibana Jest testing environment, and is consumed via
Yep, we have some docs in EUI (linked above), but docs in Kibana are lacking. We can do better there and the point about "make it obvious they differ from the non-test implementation" is a good idea. |
Awesome, happy to go with the serializer approach! To be honest I'm not sure a When developing in Kibana there are a lot of services and contract that have to be mocked for testing irregardless so I don't think it's too much of an ask to have to mock time / random based functions, especially if we make it easy by providing mock implementations. |
I might be missing some context but one other issue I thought about when discussing this with my team is that we might be trying to solve a problem here that doesn't exists. When writing snapshot tests we should be shallow rendering the component in which case the id wouldn't even get generated. If we mount the entire component tree then we're not talking about unit tests anymore but "component integration" tests (for the lack of a better word) in which case snapshots are not a good fit (they would grow out of hand and change with every update to a dependent component). The use case for mounting components are interaction based tests for which we'd use jsdom (enzyme or react testing library). |
I think you're correct on all those points. Writing tests in Kibana, especially as they relate to EUI components/internals, has been something of an ongoing discussion and one that we plan on continuing this year. There are some good ideas in this thread to bring into the fold. |
👋 Hey there. This issue hasn't had any activity for 180 days. We'll automatically close it if that trend continues for another week. If you feel this issue is still valid and needs attention please let us know with a comment. |
👋 Hey hey! Just wanted to drop my 2c in here. I did a quick timeboxed investigation into Jest's snapshot serializers and unfortunately I'm concerned by the complexity/level of effort it would take to write one. Jest's serialize API is complex and most of their examples involve basic iterables and primitives like objects and arrays. The closest current snapshot plugins I could find that is close to doing what we want (finding an attribute and replacing it) is coincidentally those of CSS-in-JS libraries, that replace randomized CSS classNames with static ones. Examples: However, just looking at the amount of code and complexity required to accomplish what they want gives a hint at how much extra work this would be. The issue is that when it comes to serializing JSX/components, what we would have to do is:
Basically, what I'm trying to say is that to write a snapshot serializer for our generated IDs would be close in terms of annoyance to trying to regex html, and certainly very similar to trying to find a needle in a haystack. I'm not convinced this is worth the extra time and effort to do so vs. our current Also, I definitely could be missing something or haven't investigated deeply enough, and would love other opinions! |
@constancecchen Amazing, thanks for looking into this! We currently have to manually overwrite the EUI mock in every unit test to create a unique id. I still think EUI should not mock this function at all since snapshot tests use shallow render and other unit tests are better off using the actual implementation. |
Alright, that's super good to know! It sounds like the extra work would be worth the effort, although it will take a while to get to it.
For Jest/Enzyme this isn't quite true; we take plenty of mounted/rendered snapshot tests in our EUI codebase at least (e.g. DataGrid). Of course, that's internal within the EUI repo, I wonder what the effect would be on the Kibana repo if we removed the |
👋 Hi there - this issue hasn't had any activity in 6 months. If the EUI team has not explicitly expressed that this is something on our roadmap, it's unlikely that we'll pick this issue up. We would sincerely appreciate a PR/community contribution if this is something that matters to you! If not, and there is no further activity on this issue for another 6 months (i.e. it's stale for over a year), the issue will be auto-closed. |
Commenting to remove the stale check. This is something the EUI team wants to own as part of writing a custom snapshot serializer and reducing snapshot churn in Kibana: |
My 2 cents: at this point RTL (the future of UI testing) is more important than supporting UI snapshots (questionable value). |
I 100% agree, but the unfortunate tech-debt-laden reality is that Kibana continues to have many snapshots on Enzyme. Us recently removing Testing tech debt (converting all EUI enzyme tests to RTL, etc) is my/EUI's next big priority after the Emotion migration and I hope to be able to sit down and resolve issues like this then - but unfortunately that won't be for a few months, unless I can invent a time machine 🤞 |
👋 Hi there - this issue hasn't had any activity in 6 months. If the EUI team has not explicitly expressed that this is something on our roadmap, it's unlikely that we'll pick this issue up. We would sincerely appreciate a PR/community contribution if this is something that matters to you! If not, and there is no further activity on this issue for another 6 months (i.e. it's stale for over a year), the issue will be auto-closed. |
EuiFormRow
automatically generates an id to link together field label with field input.This works fine in production and dev. However, in a test environment this functionality is disabled and instead a static string is returned for every field: https://github.com/elastic/eui/blob/master/src/services/accessibility/html_id_generator.testenv.ts
This means each input element in a form with multiple fields now has the same id during testing which is invalid. It also breaks expected behaviour since clicking any field label will now focus the same input element (whichever is the first one in the form) instead of the actual input element wrapped by the
EuiFormRow
.This behaviour stops us from using
react-testing-library
which approach is to force developers to query elements by role or label text instead of DOM elements to ensure accessibility requirements are met:What is the purpose of the static
generated-id
ids during testing?Is there a way to disable this behaviour?
My only option right now seems to be to manually set each field's
id
, which defeats the purpose of having automatic id generation in the first place, and runs the danger of clashing with other ids on the page.It would be great if this could be opt-in via a mock so that devs who require static ids (e.g. for snapshot testing) can easily opt into this behaviour without breaking other tests.
The text was updated successfully, but these errors were encountered: