Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

Use Cypress commands and aliases, version bumps and module loading tweaks #37

Merged
merged 7 commits into from
Feb 17, 2019

Conversation

RcKeller
Copy link

I'm absolutely loving this outside-the-box usage of Cypress! In using this in a few different applications (some w/ TS, some not), I found a few points that could be improved:

  • Aliasing components instead of tracking them in Cypress.component, for consistency w/ the rest of the API.
  • Making cy.mount a command because of how often it is used. Added JSDocs for our non-TS users
  • Cleaner logging to indicate setup steps, useful for triaging performance issues.
  • Adding an Error Boundary example to demonstrate more complex use cases.
  • Loading React/ReactDOM via the UMD bundles included in node_modules for exact version matching
  • Version bump to @babel/core@7, adjustments to babel setup as needed.

I've updated the README to reflect these changes. Thanks!

@bahmutov
Copy link
Contributor

@RcKeller this is 100% 🔥 nice job! Let me and @amirrustam read and discuss this at Cypress, we were looking for answers like this to how to go about component testing.

name: 'React'
})
// mounted component is aliased as @Component
cy.get('@Component')
Copy link
Contributor

Choose a reason for hiding this comment

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

or even get it by component itself like

cy.get(<HelloState />)
// finds previously mounted <HelloState />

Also maybe as alternative find by camel case name?

cy.get('@HelloState')

// Yeah, due to how Cy resolves promises under the hood, a closure works but an @aliased cached asset doesn't
return cy
.log('Injecting ReactDOM for Unit Testing')
.readFile('node_modules/react/umd/react.development.js', { log: false }).then(file => packages.React = file)
Copy link
Contributor

Choose a reason for hiding this comment

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

for performance reasons you might read these two files once and just keep them in memory

@param {string} [Component] alias
**/
Cypress.Commands.add('mount', (jsx, alias = 'Component') => {
cy
Copy link
Contributor

Choose a reason for hiding this comment

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

we need to throw an error here if the user tries to mount more than once

cy.mount(...) // ok
cy.mount(...) // error

Copy link
Contributor

@bahmutov bahmutov left a comment

Choose a reason for hiding this comment

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

We love it, this is really really nice direction for this package. Thank you for taking the time to make such great PR. I will merge it and we will see where to go from here. There were a couple of things we would like to tackle in 2.1.0 (see individual comments), and we will work on it, or feel free to open another PR once this gets released and you have the chance to play with the new API syntax a little bit more.

@bahmutov
Copy link
Contributor

Also, I need to update the readme, since now this plugin has to be loaded from cypress/support/index with other custom commands

@bahmutov
Copy link
Contributor

also, I need to add TypeScript type file to add command mount to global cy object

@RcKeller
Copy link
Author

RcKeller commented Feb 4, 2019

Fantastic! @bahmutov I've made the requested changes on another branch, keeping them separate so you can cherry pick them. Here you go: RcKeller#1

  • Added a modules.json fixture to specify modules & their load order.
  • Modules are now read once in a beforeAll hook and kept in memory.
  • Some tweaks to use Cypress instead of cy when appropriate.
  • Overrode cy.get() to support three different selectors: @DisplayName, <JSX /> or the React.Component itself. Did this in a way so that you didn't have to load React into memory for typechecking component instances.

@bahmutov bahmutov merged commit b8ecd2f into cypress-io:master Feb 17, 2019
dmtrKovalenko pushed a commit to cypress-io/cypress that referenced this pull request Oct 1, 2020
…ding tweaks (cypress-io/cypress-react-unit-test#37)

BREAKING CHANGE: how components are mounted and accessed

* Fixed users spec - had race condition

* Drafted cypress commands

* Lib functions as commands. Use node_modules for loading React

* Switch over to using cy.mount. Docs updated

* Version bumping incl. babel@7

* Added error boundaries - good example component

* Line about err boundary component
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants