I laid My Vision for Component Tests in Cypress in Cypress a long time ago. The first public presentation about End-to-End + Component test combination was made in the presentation The Shape Of Testing Pyramid at AssertJS in February of 2018, ughh 4 years ago. Where did the time go? Where is Cypress v10 (aka Cypress X) with full production component testing? I don't know.
But here is a user asking about it on Cypress Discord channel
Let me answer this question in the strongest way - but creating a working example.
🎁 You can find my source code in the repository bahmutov/click-returns-true.
I have created a new project using the following commands:
1 | # make a new folder |
I added Cypress and Prettier as dev dependencies. I love using Prettier. Note: the coming Cypress v10 release might change how the application looks and runs, this blog post uses v9. I hope the post shows the main principles that still apply.
Ok, we need to execute a React component. So I will add React, Read DOM, and react-scripts dependencies.
1 | $ npm i -S react@17 react-dom@17 react-scripts |
I add browserslist
field to the package.json
file
1 | { |
Question: why did I not use Yarn? In this example it does not matter if I use npm
or yarn
to install NPM dependencies.
Now I need to follow the Cypress component testing guide to install specific dependencies for mounting React components inside Cypress. I suggest following the examples in cypress-io/cypress-component-testing-examples. In particular, I installed two dev dependencies:
1 | $ npm i -D @cypress/react @cypress/webpack-dev-server |
Super, and here is my modified plugin file that uses the React dev server provided by the react-scripts
1 | const injectDevServer = require('@cypress/react/plugins/react-scripts') |
Now let's test our React component. I write the following component spec file
1 | import React from 'react' |
Let's open the Cypress in component testing mode
1 | $ npx cypress open-ct |
Click on the "click.spec.ct.js" name shown in the browser file explorer. You see the button component running as a mini React web application.
A cool thing about React component testing in Cypress - the React DevTools are mounted automatically to make inspecting the complex components a breeze.
The stub
Wait, did we just pass a stub reference from the spec code to the component? Yes we did
1 | <Button onClick={cy.stub().as('click')}>Click me</Button> |
This is the most powerful feature in the Cypress test runner. In the E2E and component tests, you can access the actual application and spy / stub its methods, or browser APIs, because the test spec is running inside the browser itself, and not just sending browser automation commands. Read my blog post Cypress vs Other Test Runners for more information.
Ok, let's click on the button. There is cy.click command, and all Cypress commands used normally during E2E tests work in the component tests (except the cy.visit
, since you don't really visit an URL in a component test)
1 | import React from 'react' |
Yup, this React component really calls the prop onClick
when the user clicks the HTML element button
.
What happens if the HTML button is disabled? Does the onClick
event handler fire? Let's try it out. By default cy.click requires the button to be actionable - being visible, enabled, and that sort of thing. Thus we need to use .click({ force: true })
to skip those checks.
1 | import React from 'react' |