Skip to content

Adding Cypress for browser-based component unit testing - #5042

Merged
chandlerprall merged 17 commits into
masterfrom
cypress
Oct 6, 2021
Merged

chandlerprall merged 17 commits into
masterfrom
cypress

Conversation

@chandlerprall

@chandlerprall chandlerprall commented Aug 17, 2021 •

Copy link
Copy Markdown
Contributor

Why

Some things are incredibly hard to write jest tests for, especially focus management or other low-level browser things. .innerText vs .innerContent is another good example, which we deal with in inner_text.tsx and account for via interesting ways in that component's unit tests.

Questions/Explorations

  • Visual regression testing maybe in the future
  • Establish/document when to use cypress vs jest
  • Do we want integration testing? no / not yet
  • Weren't you opening this as a discussion? Yes but then I opted for a PR so it includes the diff
  • Integrate with CI - added to the existing test yarn script which runs in CI, we'll see if jenkins falls over or not

Cypress links

Support landing page
Overview, guides
API docs
FAQ

Use, how to run

component test

yran test-cypress to run component tests headless (no window appears)
yarn test-cypress-dev launches a chrome window controlled by cypress, which lists out the discovered tests (only focus_test.spec.js at the moment) and allows executing/interacting from that window.

Changes

  • adds cypress & related packages
  • includes a sample integration test (cypress/integration/focus_trap.js) against the focus trap's docs page
  • includes a sample component test (src/components/focus_trap/focus_trap.spec.js) which re-implements the skipped jest test
  • new cypress.json file added for basic configuration
  • cypress/plugins/index.js modified to use our webpack config
  • src/webpack.config.js modified to not exclude externals, which are only useful for the dist bundle/build anyway (a quick stop-gap to get this running)
  • added to test yarn script

Checklist

- [ ] Check against all themes for compatibility in both light and dark modes
- [ ] Checked in mobile
- [ ] Checked in Chrome, Safari, Edge, and Firefox
- [ ] Props have proper autodocs and playground toggles

  • Added documentation
    - [ ] Checked Code Sandbox works for the any docs examples
  • Added or updated jest tests
  • Checked for breaking changes and labeled appropriately
    - [ ] Checked for accessibility including keyboard-only and screenreader modes
    - [ ] A changelog entry exists and is marked appropriately

…rap as well as a component test example for the same
@cee-chen

cee-chen commented Aug 17, 2021 •

Copy link
Copy Markdown
Contributor

Visual regression testing (do we want this?)

I'd vote no for now/for the initial Cypress setup, and investigate future extra Cypress plugins as we decide we need them. Cypress can do a lot of cool things, but I generally prefer adding things incrementally and as there's a strong use case for it, rather than all at once.

As another example of something Cypress could maybe someday do: there's an existing cypress-axe plugin that could we could opt to switch to if we prefer having all our E2E tests in one place and using one test framework, or if we prefer its violation reporting/debugging process. But it should definitely be evaluated on a case-by-case basis.

Do we want integration testing? (tests against any docs pages, instead of specifically against a component)

I would vote against setting up standard E2E integration tests for now and for solely using the component test runner to start with. As someone who has spent an unfortunately high amount of time debugging varying Selenium or Cypress flakes - it's the nature/terrain of E2E testing that you're going to run into bizarre and hard-to-replicate intermittent failures (partially due to 3rd party deps/load times, and partially due to how insanely fast the automatic test runner goes compared to a real human person).

The component test runner however looks like an amazing way to mitigate that and isolate/sandbox some amount of flaking, and is agruably much more effective for EUI/any component library. For an actual solution definitely they'd want standard E2E tests, for us, the CTR couldn't have come out at a more opportune time.

The most likely scenario I can think of that we would potentially want automated E2E tests for (based on what I've heard during onboarding/roadmap discussions) is if we're testing how EUI integrates into Kibana, Cloud, or other platforms. IIRC we're still figuring out how that would work in terms of CI and so forth, so that might be a ways off yet, and would likely need more discussion to figure out the exact goals (what regressions/breakages can we hope to catch?)

Establish/document when to use cypress vs jest

+++, would love to see this documented in the testing wiki. In terms of developer experience and speed, my vote personally would be to default to jest/unit tests first and only write Cypress component tests for functionality that jsdom/enzyme can't easily mock (focus, portals, 3rd party dependencies, native DOM behaviors, etc. - a comprehensive list would be fantastic if we can brainstorm one).

I also think whether we prefer Cypress vs. Jest will depend on exactly what kind of bugs, issues, or regressions we're attempting to catch (or even which come up organically over time). If we end up seeing more bizarre browser-level bugs/interactions that jsdom can't realistically replicate, I think we'll naturally end up with more Cypress component tests over time (e.g., this recent PR from Greg would have been a great candidate for a Cypress component test).

(As an aside, I want to note that my preference/vote on this is based on EUI's role as a component library - for a fully-fledged app/solution, I would recommend E2E tests instead as basic smoke/regression tests and not necessarily for edge cases).

Oh, and BTW - I've had to write Cypress docs a few separate times for Enterprise Search (😅) and would be happy to donate some of the debugging tips I've already written. (It's not plagiarizing if you're stealing from yourself, right?)

@cee-chen

cee-chen commented Aug 17, 2021 •

Copy link
Copy Markdown
Contributor

Oh, one more possible consideration - something to keep an eye on with Cypress/E2E tests is the amount of time tests will take to run in CI. Currently EUI CI seem super fast per PR (are they running all jest tests? I haven't actually checked 🙈) and adding a few Cypress tests here or there probably won't add to that significantly to start. It ramps up pretty quickly though and I could see CI starting to take 30min+ if (for example) every single component had its own Cypress test.

TBF my barometer for CI times is way off since I got used to Kibana's 2hr+ runs, and EntSearch also similarly used to take around 30 mins as well, so that's definitely not the worst thing in the world - just something to keep in mind and maybe re-evaluate goal-wise if we start running into long CI times.

@chandlerprall

Copy link
Copy Markdown
Contributor Author

Visual regression testing (do we want this?)

I'd vote no for now/for the initial Cypress setup, and investigate future extra Cypress plugins as we decide we need them

Motion [to not do this] carried!

Do we want integration testing? (tests against any docs pages, instead of specifically against a component)

I would vote against setting up standard E2E integration tests for now and for solely using the component test runner to start

Motion [to not do this] carried!

Establish/document when to use cypress vs jest

+++, would love to see this documented in the testing wiki

+++

Oh, and BTW - I've had to write Cypress docs a few separate times for Enterprise Search (😅) and would be happy to donate

🎉 yes please - let's add a new .md file with this PR in wiki with any helpful cypress thoughts

something to keep an eye on with Cypress/E2E tests is the amount of time tests will take to run in CI

Absolutely, thanks for calling it out! I feel our CI time is already on the long end and tidying that up has been on my personal backlog; there's probably a decent amount of cleanup/optimization with the webpack config to speed this up too

@cee-chen

Copy link
Copy Markdown
Contributor

🎉 yes please - let's add a new .md file with this PR in wiki with any helpful cypress thoughts

Sweet! Is this something you'd like me to contribute now, or wait until you're more done tweaking Cypress config/setup etc. first?

@chandlerprall chandlerprall changed the title [WIP] Adding Cypress for e2e & browser-based testing Adding Cypress for browser-based component unit testing Aug 26, 2021
@chandlerprall

Copy link
Copy Markdown
Contributor Author

Sweet! Is this something you'd like me to contribute now, or wait until you're more done tweaking Cypress config/setup etc. first?

Contributing into this PR would be great 👍

@chandlerprall chandlerprall added the skip-changelog Use on PRs to skip changelog requirement (Don't delete - used for automation) label Aug 26, 2021
Comment thread cypress.json Resolved
+ update a few current testing docs to point to new Cypress doc
@cee-chen

Copy link
Copy Markdown
Contributor

👋 I added a start at Cypress documentation! Some of it I yolo-guessed at so definitely feel free to fix/update anything I was way off-base about. The debugging section has 2 sets of TODOs around where we store failure screenshots locally and on CI - if we're not running Cypress on CI just yet feel free to delete that section

Comment thread wiki/cypress-testing.md Resolved
@chandlerprall

Copy link
Copy Markdown
Contributor Author

Docker container works 🎉

14:08:00   EuiFocusTrap
14:08:00     behavior
14:08:00       focus
14:08:01         ✓ is set on the first focusable element by default (301ms)
14:08:01         ✓ will not take focus when `autoFocus` is disabled (38ms)
14:08:01         ✓ is set on the element identified by `data-autofocus` (41ms)
14:08:01       clickOutsideDisables
14:08:01         ✓ trap remains enabled when false (130ms)
14:08:01         ✓ trap remains enabled after internal clicks (122ms)
14:08:01         ✓ trap remains enabled after internal portal clicks (118ms)
14:08:01         ✓ trap becomes disabled on outside clicks (130ms)
14:08:01 
14:08:01 
14:08:01   7 passing (924ms)
14:08:01 
14:08:01 
14:08:01   (Results)
14:08:01 
14:08:01   ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
14:08:01   │ Tests:        7                                                                                │
14:08:01   │ Passing:      7                                                                                │
14:08:01   │ Failing:      0                                                                                │
14:08:01   │ Pending:      0                                                                                │
14:08:01   │ Skipped:      0                                                                                │
14:08:01   │ Screenshots:  0                                                                                │
14:08:01   │ Video:        false                                                                            │
14:08:01   │ Duration:     0 seconds                                                                        │
14:08:01   │ Spec Ran:     components/focus_trap/focus_trap.spec.tsx                                        │
14:08:01   └────────────────────────────────────────────────────────────────────────────────────────────────┘

Unfortunately another build step is still on the wrong node version, looking at that next

@kibanamachine

Copy link
Copy Markdown
Contributor

@chandlerprall
chandlerprall marked this pull request as ready for review September 27, 2021 20:46
@cee-chen

This comment has been minimized.

Comment thread scripts/test-docker.js Resolved
Comment thread package.json Resolved
Comment thread cypress/support/index.js Resolved
Comment thread cypress/support/commands.js Resolved
@kibanamachine

Copy link
Copy Markdown
Contributor

@chandlerprall
chandlerprall requested a review from cee-chen October 5, 2021 17:44
@kibanamachine

Copy link
Copy Markdown
Contributor

@cee-chen cee-chen 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.

Looking and working great for me locally! Wahoo!! 🚀 🚀 🚀

@kibanamachine

Copy link
Copy Markdown
Contributor

@chandlerprall
chandlerprall merged commit 326e8c3 into master Oct 6, 2021
@cee-chen
cee-chen deleted the cypress branch October 6, 2021 22:52
ym pushed a commit to ym/eui that referenced this pull request Oct 29, 2021
* Added Cypress and added an example e2e integration test for EuiFocusTrap as well as a component test example for the same

* cypress.json

* Removed some default cypress files, cleaned up the related configurations

* converting cypress spec to typescript but have some issues

* Add documentation

+ update a few current testing docs to point to new Cypress doc

* newline nit

* bump node version, update docker image

* It is nice to run the things you meant to run

* set a specific OS for CI jobs

* Use modern node version in the deploy docs CI job

* Use the CI docker image in the build_docs job too

* PR feedback

Co-authored-by: Constance Chen <[email protected]>
Co-authored-by: Constance <[email protected]>
@thompsongl thompsongl mentioned this pull request Dec 6, 2021
14 of 24 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-changelog Use on PRs to skip changelog requirement (Don't delete - used for automation)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants