cypress-react-unit-test
A little helper to unit test React components in the open source Cypress.io E2E test runner v4.5.0+
Jump to: Comparison, Blog posts, Install, Examples: basic, advanced, full, external, Style options, Code coverage, Visual testing, Common problems, Chat
Survey
Hi there! We are trying to collect feedback from Cypress users who need component testing. Answer a few questions in this survey about component testing to help us 🙏
TLDR
- What is this? This package allows you to use Cypress test runner to unit test your React components with zero effort. Here is a typical component testing, notice there is not external URL shown, since it is mounting the component directly.
Comparison
Feature | Jest / Enzyme / RTL | Cypress + cypress-react-unit-test |
---|
Test runs in real browser | ❌ | ✅ |
Supports shallow mount | ✅ | ❌ |
Supports full mount | ✅ | ✅ |
Test speed | 🏎 | as fast as the app works in the browser |
Test can use additional plugins | maybe | use any Cypress plugin |
Test can interact with component | synthetic limited API | use any Cypress command |
Test can be debugged | via terminal and Node debugger | use browser DevTools |
Built-in time traveling debugger | ❌ | Cypress time traveling debugger |
Re-run tests on file or test change | ✅ | ✅ |
Test output on CI | terminal | terminal, screenshots, videos |
Tests can be run in parallel | ✅ | ✅ via parallelization |
Test against interface | if using @testing-library/react | ✅ and can use @testing-library/cypress |
Spying and stubbing methods | Jest mocks | Sinon library |
Stubbing imports | ✅ | ✅ |
Stubbing clock | ✅ | ✅ |
Code coverage | ✅ | ✅ |
If you are coming from Jest + RTL world, read Test The Interface Not The Implementation.
If you are coming from Enzyme world, check out the enzyme example.
Blog posts
Known problems
See issues labeled v4
Install
Requires Node version 8 or above.
npm install --save-dev cypress cypress-react-unit-test
- Include this plugin from your project's
cypress/support/index.js
require('cypress-react-unit-test/support')
- Tell Cypress how your React application is transpiled or bundled (using Webpack), so Cypress can load your components. For example, if you use
react-scripts
(even after ejecting) do:
module.exports = (on, config) => {
require('cypress-react-unit-test/plugins/react-scripts')(on, config)
return config
}
See Recipes for more examples.
- ⚠️ Turn the experimental component support on in your
cypress.json
. You can also specify where component spec files are located. For example, to have them located in src
folder use:
{
"experimentalComponentTesting": true,
"componentFolder": "src"
}
API
mount
is the most important function, allows to mount a given React component as a mini web application and interact with it using Cypress commandscreateMount
factory function that creates new mount
function with default optionsunmount
removes previously mounted component, mostly useful to test how the component cleans up after itselfmountHook
mounts a given React Hook in a test component for full testing, see hooks
example
Examples
import React from 'react'
import { mount } from 'cypress-react-unit-test'
import { HelloWorld } from './hello-world.jsx'
describe('HelloWorld component', () => {
it('works', () => {
mount(<HelloWorld />)
cy.contains('Hello World!').should('be.visible')
})
})
Look at the examples in cypress/component folder. Here is the list of examples showing various testing scenarios.
Basic examples
plus a few smaller sanity specs in cypress/component/basic folder.
Advanced examples
Full examples
We have several subfolders in examples folder that have complete projects with just their dependencies installed in the root folder.
External examples
This way of component testing has been verified in a number of forked 3rd party projects.
To find more examples, see GitHub topic cypress-react-unit-test-example
Options
In most cases, the component already imports its own styles, thus it looks "right" during the test. If you need another CSS, the simplest way is to import it from the spec file:
import './styles/main.css'
import Footer from './Footer'
it('looks right', () => {
mount(<Footer />)
})
You can pass additional styles, css files and external stylesheets to load, see docs/styles.md for the full list of options.
const todo = {
id: '123',
title: 'Write more tests',
}
mount(<Todo todo={todo} />, {
stylesheets: [
'https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.css',
],
})
Additional configuration
If your React and React DOM libraries are installed in non-standard paths (think monorepo scenario), you can tell this plugin where to find them. In `cypress.json` specify paths like this:
{
"env": {
"cypress-react-unit-test": {
"react": "node_modules/react/umd/react.development.js",
"react-dom": "node_modules/react-dom/umd/react-dom.development.js"
}
}
}
Code coverage
If you are using plugins/cra-v3 it instruments the code on the fly using babel-plugin-istanbul
and generates report using dependency cypress-io/code-coverage (included). If you want to disable code coverage instrumentation and reporting, use --env coverage=false
or CYPRESS_coverage=false
or set in your cypress.json
file
{
"env": {
"coverage": false
}
}
Visual testing
You can use any Cypress Visual Testing plugin to perform visual testing from the component tests. This repo has several example projects, see visual-sudoku, visual-testing-with-percy, visual-testing-with-happo, and visual-testing-with-applitools.
For a larger Do-It-Yourself example with an hour long list of explanation videos, see bahmutov/sudoku repository. I explain how to write visual testing using open source tools in this blog post, video talk, and slides.
Common problems
Node Sass
When using Node Sass styles, tell Cypress to use the system NodeJS rather than its bundled version. In cypress.json
set option:
{
"nodeVersion": "system"
}
Find full example in sass-and-ts folder.
Slow bundling
When you bundle spec file, you are now bundling React, Read DOM and other libraries, which is might be slow. For now, you can disable inline source maps by adding to your Webpack config settings (if available) the following:
const webpackOptions = {
devtool: false,
}
Keep your eye on issue #156 for more information.
Missing code coverage
If you are using your custom Webpack, this plugin might be missing code coverage information because the code was not instrumented. We try to insert the babel-plugin-istanbul
plugin automatically, but your bundling might not use Babel, or configure it differently, preventing plugin insertion. Please let us know by opening an issue with full reproducible details.
See related issue #141. You can also debug the plugin's behavior by running it with DEBUG
environment variable, see #debugging section.
Next.js projects not supported
Currently, this project cannot find Webpack settings used by Next.js, thus it cannot bundle specs and application code correctly. Keep an eye on #155
Gatsby.js projects not supported
Currently, this project cannot find Webpack settings used by Gatsby.js, thus it cannot bundle specs and application code correctly. Keep an eye on #307
Chat
We have a chat workspace at https://component-testing.slack.com/, you are welcome to join us.
Development
See docs/development.md
Debugging
You can see verbose logs from this plugin by running with environment variable
DEBUG=cypress-react-unit-test
Because finding and modifying Webpack settings while running this plugin is done by find-webpack module, you might want to enable its debug messages too.
DEBUG=cypress-react-unit-test,find-webpack
Migration guide
From v3 to v4
The old v3 main
branch is available as branch v3
- the
cy.mount
is now simply import { mount } from 'cypress-react-unit-test'
- the support file is simply
require('cypress-react-unit-test/support')
Related tools
Same feature for unit testing components from other frameworks using Cypress