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

Install React Testing Library and export several RTL test helpers #6091

Merged
merged 12 commits into from
Aug 2, 2022

Conversation

cee-chen
Copy link
Member

@cee-chen cee-chen commented Jul 28, 2022

Summary

Meta issue: #5596

Our first baby steps into the eventual conversion of Enzyme tests into RTL. This PR was born more out of the need to figure out why the heck Kibana's RTL tests are flipping their lids over some of our recent changes to EuiPopover/EuiPortal.

This PR:

  1. Adds React Testing Library dev dependencies pinned at the same versions that Kibana uses
  2. Adds a few basic EuiPopover RTL tests to get used to RTL's API/syntax and confirm that EuiPopover testing works in RTL
  3. Adds new RTL ByDataTestSubj utilities for targeting EUI's data-test-subj attributes. These can be accessed by importing custom RTL render and screen utils from @elastic/eui/lib/test/rtl.
  4. Adds new React Testing Library EuiPopover helpers, waitForEuiPopoverOpen and waitForEuiPopoverClose

Checklist

  • Changelog
  • Tested against Create React App without@testing-library/react installed
  • Tested against Kibana with @testing-library/react installed

@@ -0,0 +1,105 @@
/*
Copy link
Member Author

Choose a reason for hiding this comment

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

RTL tests should just be .test.tsx but since I'm not migrating 100% of EuiPopover's tests currently I just did rtl.test.tsx. End goal everything should be just .test.tsx once we're out of Enzyme.

@cee-chen
Copy link
Member Author

@chandlerprall Does this technically close #5597?

@cee-chen
Copy link
Member Author

The good news is that RTL works great with base EuiPopover

The bad news is that I then have no idea why Kibana is throwing so many pointer events errors :/

Comment on lines +27 to +28
// NOTE: Using baseElement instead of container is required for components that use portals
expect(baseElement).toMatchSnapshot();
Copy link
Member Author

Choose a reason for hiding this comment

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

Just want everyone to share the pain I had of figuring this out in a 3 year old github thread when google wasn't returning any relevant results 💀

@cee-chen cee-chen marked this pull request as ready for review July 28, 2022 21:18
@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6091/

@chandlerprall
Copy link
Contributor

Does this technically close #5597?

it does! 🎉

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6091/

@chandlerprall
Copy link
Contributor

By commenting out blocks, I found the mocks fail when rtl_custom_render.ts imports from ../components.

Fix

scripts/jest/setup/config.json

  "setupFiles": [
    "<rootDir>/scripts/jest/setup/enzyme.js",
    "<rootDir>/scripts/jest/setup/throw_on_console_error.js",
+    "<rootDir>/scripts/jest/setup/mocks.js"
  ],
  "setupFilesAfterEnv": [
-    "<rootDir>/scripts/jest/setup/mocks.js",
    "<rootDir>/scripts/jest/setup/polyfills.js",
    "<rootDir>/scripts/jest/setup/unmount_enzyme.js"
  ],

Why

Jest loads the snapshot serializers before setupFilesAfterEnv, and scripts/jest/setup/emotion.js imports from src/test kicking off the import chain: src/test -> rtl_custom_render -> ../components thus pre-requiring the entirety of EUI's components before the mock code loads. During execution, the pre-warmed file cache is apparently preferred to going to the defined mocks.

We should probably also move unmount_enzyme.js to setupFiles as it does a mock. polyfills.js should probably stay in setupFilesAfterEnv

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6091/

@cee-chen
Copy link
Member Author

cee-chen commented Jul 30, 2022

By commenting out blocks, I found the mocks fail when rtl_custom_render.ts imports from ../components.

Whoa, thank you for the debugging!! I thought I tried that but Jest took so long when I moved that file to setupFiles that I thought it was broken 😅

Out of curiosity, would it also work to import EuiProvider from src/components/provider/provider directly instead of src/components?

EDIT: Looks like it doesn't, womp womp.

@cee-chen
Copy link
Member Author

cee-chen commented Aug 1, 2022

We should probably also move unmount_enzyme.js to setupFiles as it does a mock. polyfills.js should probably stay in setupFilesAfterEnv

Unfortunately if I move unmount_enzyme to setupFiles I get the following error:

ReferenceError: afterAll is not defined

Moving mocks.js worked GREAT though, thank you so much for figuring that out! 🙇

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6091/

…test/`

- Since RTL is a devDependency not a peerDependency, we can't assume consumers will have the library installed, and need to make reaching for it optional
@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6091/

1 similar comment
@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6091/

Copy link
Member Author

@cee-chen cee-chen left a comment

Choose a reason for hiding this comment

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

I'm definitely not in love with the manual .d.ts files workaround, but I guess it works 🙈 I think this is ready for Kibana prime time @thompsongl @chandlerprall!

scripts/jest/config.json Show resolved Hide resolved
@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6091/

Copy link
Contributor

@thompsongl thompsongl left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks, as always, for all the tests

@cee-chen cee-chen changed the title Install React Testing Library Install React Testing Library and export several RTL test helpers Aug 2, 2022
Copy link
Contributor

@chandlerprall chandlerprall left a comment

Choose a reason for hiding this comment

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

:shipit: 🦑

@cee-chen cee-chen merged commit ca34f24 into elastic:main Aug 2, 2022
@cee-chen cee-chen deleted the react-testing-library branch August 2, 2022 16:53
cee-chen pushed a commit that referenced this pull request Aug 2, 2022
)

* Add RTL dependencies (at the same versions that Kibana uses)

* Add basic EuiPopover RTL tests

* consolidate react types yarn.lock entries

* Add custom RTL queries and render/screen

* consolidate more yarn.lock entries

* Fix custom RTL render ignoring testenv mocks

* Add RTL helpers for waiting for an EuiPopover to open/close

* [Misc] Tweak typing by using correct get (vs query/find) API

* Move RTL helpers to `src/test/rtl` instead of being exported by `src/test/`

- Since RTL is a devDependency not a peerDependency, we can't assume consumers will have the library installed, and need to make reaching for it optional

* Add Typescript definitions for helpers

* Add unit tests for new generic test helpers

+ helps with quick type checking as well

* Changelog/consuming documentation

Co-authored-by: Greg Thompson <thompson.glowe@gmail.com>
Co-authored-by: Chandler Prall <chandler.prall@gmail.com>
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.

4 participants