cypress 9.7.0 → 10.0.2
Sign up to get free protection for your applications and to get access to all the features.
- package/index.mjs +15 -0
- package/lib/cli.js +72 -23
- package/lib/errors.js +16 -1
- package/lib/exec/open.js +45 -10
- package/lib/exec/run.js +17 -10
- package/lib/exec/shared.js +30 -9
- package/lib/exec/spawn.js +4 -0
- package/lib/exec/xvfb.js +1 -0
- package/lib/util.js +10 -3
- package/mount-utils/CHANGELOG.md +20 -0
- package/mount-utils/README.md +14 -0
- package/mount-utils/dist/index.d.ts +54 -0
- package/mount-utils/dist/index.js +134 -0
- package/mount-utils/package.json +31 -0
- package/package.json +39 -4
- package/react/CHANGELOG.md +373 -0
- package/react/README.md +414 -0
- package/react/dist/cypress-react.browser.js +497 -0
- package/react/dist/cypress-react.cjs.js +495 -0
- package/react/dist/cypress-react.esm-bundler.js +467 -0
- package/react/dist/getDisplayName.d.ts +8 -0
- package/react/dist/index.d.ts +2 -0
- package/react/dist/mount.d.ts +143 -0
- package/react/dist/mountHook.d.ts +11 -0
- package/react/package.json +105 -0
- package/types/bluebird/index.d.ts +18 -4
- package/types/cypress-npm-api.d.ts +4 -10
- package/types/cypress.d.ts +172 -120
- package/types/minimatch/index.d.ts +15 -5
- package/vue/CHANGELOG.md +380 -0
- package/vue/README.md +678 -0
- package/vue/dist/cypress-vue.cjs.js +13535 -0
- package/vue/dist/cypress-vue.esm-bundler.js +13511 -0
- package/vue/dist/index.d.ts +56 -0
- package/vue/package.json +86 -0
- package/vue2/CHANGELOG.md +5 -0
- package/vue2/README.md +693 -0
- package/vue2/dist/cypress-vue2.browser.js +20191 -0
- package/vue2/dist/cypress-vue2.cjs.js +20188 -0
- package/vue2/dist/cypress-vue2.esm-bundler.js +20179 -0
- package/vue2/dist/index.d.ts +171 -0
- package/vue2/package.json +59 -0
package/react/README.md
ADDED
@@ -0,0 +1,414 @@
|
|
1
|
+
> A little helper to unit test React components in the open source [Cypress.io](https://www.cypress.io/) test runner **v7.0.0+**
|
2
|
+
|
3
|
+
**Jump to:** [Comparison](#comparison), [Blog posts](#blog-posts), [Install](#install), Examples: [basic](#basic-examples), [advanced](#advanced-examples), [full](#full-examples), [external](#external-examples), [Style options](#options), [Code coverage](#code-coverage), [Visual testing](#visual-testing), [Common problems](#common-problems), [Chat](#chat)
|
4
|
+
|
5
|
+
## TLDR
|
6
|
+
|
7
|
+
- What is this? This package allows you to use [Cypress](https://www.cypress.io/) 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.
|
8
|
+
|
9
|
+
![Example component test](images/dynamic.gif)
|
10
|
+
|
11
|
+
- How is this different from [Enzyme](https://github.com/airbnb/enzyme) or [RTL](https://testing-library.com/docs/react-testing-library/intro)? It is similar in functionality BUT runs the component in the real browser with full power of Cypress E2E test runner: [live GUI, full API, screen recording, CI support, cross-platform](https://www.cypress.io/features/), and [visual testing](https://on.cypress.io/visual-testing).
|
12
|
+
- If you like using `@testing-library/react`, you can use `@testing-library/cypress` for the same `findBy`, `queryBy` commands, see one of the examples in the list below
|
13
|
+
- Read [My Vision for Component Tests in Cypress](https://glebbahmutov.com/blog/my-vision-for-component-tests/) by Gleb Bahmutov
|
14
|
+
|
15
|
+
## Comparison
|
16
|
+
|
17
|
+
<!-- prettier-ignore-start -->
|
18
|
+
Feature | Jest / Enzyme / RTL | Cypress + `@cypress/react`
|
19
|
+
--- | --- | ---
|
20
|
+
Test runs in real browser | ❌ | ✅
|
21
|
+
Supports shallow mount | ✅ | ❌
|
22
|
+
Supports full mount | ✅ | ✅
|
23
|
+
Test speed | 🏎 | [as fast as the app works in the browser](#fast-enough)
|
24
|
+
Test can use additional plugins | maybe | use any [Cypress plugin](https://on.cypress.io/plugins)
|
25
|
+
Test can interact with component | synthetic limited API | use any [Cypress command](https://on.cypress.io/api)
|
26
|
+
Test can be debugged | via terminal and Node debugger | use browser DevTools
|
27
|
+
Built-in time traveling debugger | ❌ | Cypress time traveling debugger
|
28
|
+
Re-run tests on file or test change | ✅ | ✅
|
29
|
+
Test output on CI | terminal | terminal, screenshots, videos
|
30
|
+
Tests can be run in parallel | ✅ | ✅ via [parallelization](https://on.cypress.io/parallelization)
|
31
|
+
Test against interface | if using `@testing-library/react` | ✅ and can use `@testing-library/cypress`
|
32
|
+
Spying and stubbing methods | Jest mocks | [Sinon library](https://on.cypress.io/stubs-spies-and-clocks)
|
33
|
+
Stubbing imports | ✅ | ✅
|
34
|
+
Stubbing clock | ✅ | ✅
|
35
|
+
Code coverage | ✅ | ✅
|
36
|
+
<!-- prettier-ignore-end -->
|
37
|
+
|
38
|
+
If you are coming from Jest + RTL world, read [Test The Interface Not The Implementation](https://glebbahmutov.com/blog/test-the-interface/).
|
39
|
+
|
40
|
+
If you are coming from Enzyme world, check out the [enzyme](cypress/component/basic/enzyme) example.
|
41
|
+
|
42
|
+
## Blog posts
|
43
|
+
|
44
|
+
- [My Vision for Component Tests in Cypress](https://glebbahmutov.com/blog/my-vision-for-component-tests/)
|
45
|
+
- [Unit Testing React components with Cypress](https://itnext.io/unit-testing-react-components-with-cypress-4d4cf8cd59a0)
|
46
|
+
- [Test React Component with @cypress/react Example](https://dev.to/bahmutov/test-react-component-with-cypress-react-unit-test-example-4d99)
|
47
|
+
- [Tic-Tac-Toe Component Tests](https://glebbahmutov.com/blog/tic-tac-toe-component-tests/)
|
48
|
+
- [Using .env and .env.test from React component tests](https://medium.com/@bahmutov/using-env-and-env-test-from-react-component-tests-c11aa2040bc8)
|
49
|
+
- [Visual testing for React components using open source tools](https://glebbahmutov.com/blog/open-source-visual-testing-of-components/)
|
50
|
+
- [12 Recipes for testing React applications using @cypress/react](https://dev.to/bahmutov/12-recipes-for-testing-react-applications-using-cypress-react-unit-test-46g6) (compare to [12 Recipes for testing React applications using Testing Library](https://dev.to/jooforja/12-recipes-for-testing-react-applications-using-testing-library-1bh2#portal))
|
51
|
+
- [Cypress Unit Testing React Components With TypeScript](https://medium.com/swlh/cypress-unit-testing-react-components-with-typescript-77b38e5043b3)
|
52
|
+
- [Test The Interface Not The Implementation](https://glebbahmutov.com/blog/test-the-interface/) compares Jest + React Testing Library to @cypress/react + Cypress Testing Library
|
53
|
+
- [Components People Test](https://glebbahmutov.com/blog/components-people-test/) about testing a component inside a Next.js-powered blog
|
54
|
+
|
55
|
+
## Install
|
56
|
+
|
57
|
+
Requires [Node](https://nodejs.org/en/) version 12 or above.
|
58
|
+
|
59
|
+
```sh
|
60
|
+
npm install --save-dev cypress @cypress/react @cypress/webpack-dev-server
|
61
|
+
```
|
62
|
+
|
63
|
+
## Init
|
64
|
+
|
65
|
+
You can use our command line wizard to give you instructions on configuring this plugin. It will try to determine which framework or bundling tool you are using and give you instructions on right configuration.
|
66
|
+
|
67
|
+
```sh
|
68
|
+
npx create-cypress-tests --component-tests
|
69
|
+
```
|
70
|
+
|
71
|
+
Or continue with manual installation in the plugin file
|
72
|
+
|
73
|
+
1. 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:
|
74
|
+
|
75
|
+
```js
|
76
|
+
// cypress/plugins/index.js
|
77
|
+
module.exports = (on, config) => {
|
78
|
+
require('@cypress/react/plugins/react-scripts')(on, config)
|
79
|
+
// IMPORTANT to return the config object
|
80
|
+
// with the any changed environment variables
|
81
|
+
return config
|
82
|
+
}
|
83
|
+
```
|
84
|
+
|
85
|
+
See [Recipes](./docs/recipes.md) for more examples.
|
86
|
+
|
87
|
+
2. You can specify a glob to locate component spec files. For example, to have them located in `src` folder use:
|
88
|
+
|
89
|
+
```json
|
90
|
+
{
|
91
|
+
"component": {
|
92
|
+
"specPattern": "src/**/*.cy.jsx"
|
93
|
+
}
|
94
|
+
}
|
95
|
+
```
|
96
|
+
|
97
|
+
## API
|
98
|
+
|
99
|
+
- `mount` is the most important function, allows to mount a given React component as a mini web application and interact with it using Cypress commands
|
100
|
+
- `createMount` factory function that creates new `mount` function with default options
|
101
|
+
- `unmount` removes previously mounted component, mostly useful to test how the component cleans up after itself
|
102
|
+
- `mountHook` mounts a given React Hook in a test component for full testing, see `hooks` example
|
103
|
+
|
104
|
+
## Examples
|
105
|
+
|
106
|
+
```js
|
107
|
+
import React from 'react'
|
108
|
+
import { mount } from '@cypress/react'
|
109
|
+
import { HelloWorld } from './hello-world.jsx'
|
110
|
+
describe('HelloWorld component', () => {
|
111
|
+
it('works', () => {
|
112
|
+
mount(<HelloWorld />)
|
113
|
+
// now use standard Cypress commands
|
114
|
+
cy.contains('Hello World!').should('be.visible')
|
115
|
+
})
|
116
|
+
})
|
117
|
+
```
|
118
|
+
|
119
|
+
Look at the examples in [cypress/component](cypress/component) folder. Here is the list of examples showing various testing scenarios.
|
120
|
+
|
121
|
+
### Basic examples
|
122
|
+
|
123
|
+
<!-- prettier-ignore-start -->
|
124
|
+
Spec | Description
|
125
|
+
--- | ---
|
126
|
+
[alias](cypress/component/basic/alias) | Retrieve mounted component by its name or alias
|
127
|
+
[alert-spec.js](cypress/component/basic/alert-spec.js) | Component tries to use `window.alert`
|
128
|
+
[before-hook](cypress/component/basic/before-hook) | Mount the component from `before` hook to run multiple tests against it
|
129
|
+
[counter-set-state](cypress/component/basic/counter-set-state) | Counter component that uses `this.state`
|
130
|
+
[counter-use-hooks](cypress/component/basic/counter-use-hooks) | Counter component that uses `useState` hook
|
131
|
+
[document-spec](cypress/component/basic/document) | Checks `document` dimensions from the component
|
132
|
+
[enzyme](cypress/component/basic/enzyme) | Several specs showing how to recreate Enzyme's `setProps`, `setState`, and `setContext` methods.
|
133
|
+
[emotion-spec.js](cypress/component/basic/emotion-spec.js) | Confirms the component is using `@emotion/core` and styles are set
|
134
|
+
[error-boundary-spec.js](cypress/component/basic/error-boundary-spec.js) | Checks if an error boundary component works
|
135
|
+
[fails-correctly](cypress/component/basic/fails-correctly) | Cypress test fails correctly when interacting with disabled elements
|
136
|
+
[pure-component-spec.js](cypress/component/basic/pure-component.spec.js) | Tests stateless component
|
137
|
+
[stateless-spec.js](cypress/component/basic/stateless-spec.js) | Passes Cypress stub to the component, confirms the component calls it on click
|
138
|
+
[window-spec.js](cypress/component/basic/window-spec.js) | In the component test, the spec `window` and the application's `window` where the component is running should be the same object
|
139
|
+
[css](cypress/component/basic/css) | Shows that component with `import './Button.css'` works
|
140
|
+
[css modules](cypress/component/basic/css-modules) | Shows that component that using css modules styles works
|
141
|
+
[network](cypress/component/basic/network) | Confirms we can use `cy.route` to stub / spy on component's network calls
|
142
|
+
[no-visit](cypress/component/basic/no-visit) | Component specs cannot call `cy.visit`
|
143
|
+
[rerender](cypress/component/basic/rerender) | Checking how the component re-renders when its props change
|
144
|
+
[react-book-by-chris-noring](cypress/component/basic/react-book-by-chris-noring) | Copied test examples from [React Book](https://softchris.github.io/books/react) and adapted for Cypress component tests
|
145
|
+
[react-tutorial](cypress/component/basic/react-tutorial) | Tests from official [ReactJS tutorial](https://reactjs.org/tutorial/tutorial.html) copied and adapted for Cypress component tests
|
146
|
+
[stub-example](cypress/component/basic/stub-example) | Uses `cy.stub` as component props
|
147
|
+
[styles](cypress/component/basic/styles) | Add extra styles to the component during testing using `style`, `cssFile` or `stylesheets` mount options
|
148
|
+
[toggle-example](cypress/component/basic/toggle-example) | Testing a toggle component using Cypress DOM commands
|
149
|
+
[typescript](cypress/component/basic/typescript) | A spec written in TypeScript
|
150
|
+
[unmount](cypress/component/basic/unmount) | Verifies the component's behavior when it is unmounted from the DOM
|
151
|
+
[use-lodash-fp](cypress/component/basic/use-lodash-fp) | Imports and tests methods from `lodash/fp` dependency
|
152
|
+
[styled-components](cypress/component/basic/styled-components) | Test components that use [styled-components](https://www.styled-components.com/)
|
153
|
+
<!-- prettier-ignore-end -->
|
154
|
+
|
155
|
+
plus a few smaller sanity specs in [cypress/component/basic](cypress/component/basic) folder.
|
156
|
+
|
157
|
+
### Advanced examples
|
158
|
+
|
159
|
+
<!-- prettier-ignore-start -->
|
160
|
+
Spec | Description
|
161
|
+
--- | ---
|
162
|
+
[api-test](cypress/component/advanced/api-test) | Mix [REST api tests](https://glebbahmutov.com/blog/api-testing-with-sever-logs/) that use [cy-api](https://github.com/bahmutov/cy-api) with component tests
|
163
|
+
[app-action-example](cypress/component/advanced/app-action-example) | App actions against components
|
164
|
+
[context](cypress/component/advanced/context) | Confirms components that use React context feature work
|
165
|
+
[custom-command](cypress/component/advanced/custom-command) | Wraps `mount` in a custom command for convenience
|
166
|
+
[forward-ref](cypress/component/advanced/forward-ref) | Tests a component that uses a forward ref feature
|
167
|
+
[hooks](cypress/component/advanced/hooks) | Tests several components that use React Hooks like `useState`, `useCallback`
|
168
|
+
[lazy-loaded](cypress/component/advanced/lazy-loaded) | Confirms components that use `React.lazy` and dynamic imports work
|
169
|
+
[material-ui-example](cypress/component/advanced/material-ui-example) | Large components demos from [Material UI](https://material-ui.com/)
|
170
|
+
[mobx-v6](cypress/component/advanced/mobx-v6) | Test components with MobX v6 observable
|
171
|
+
[mock-fetch](cypress/component/advanced/mock-fetch) | Test stubs `window.fetch` used by component in `useEffect` hook
|
172
|
+
[mocking-axios](cypress/component/advanced/mocking-axios) | Stubbing methods from a 3rd party component like `axios`
|
173
|
+
[mocking-component](cypress/component/advanced/mocking-component) | Replaced a child component with dummy component during test
|
174
|
+
[mocking-imports](cypress/component/advanced/mocking-imports) | Stub a named ES6 import in various situations
|
175
|
+
[react-router-v6](cypress/component/advanced/react-router-v6) | Example testing a [React Router v6](https://github.com/ReactTraining/react-router). Both browser and in memory routers
|
176
|
+
[renderless](cypress/component/advanced/renderless) | Testing a component that does not need to render itself into the DOM
|
177
|
+
[set-timeout-example](cypress/component/advanced/set-timeout-example) | Control the clock with `cy.tick` and test loading components that use `setTimeout`
|
178
|
+
[test-retries](cypress/component/advanced/test-retries) | This component is compatible with [Cypress Test Retries](https://github.com/cypress-io/cypress/pull/3968)
|
179
|
+
[testing-lib-example](cypress/component/advanced/testing-lib-example) | A spec adopted from [@testing-library/react](https://testing-library.com/docs/react-testing-library/example-intro) that uses [@testing-library/cypress](https://testing-library.com/docs/cypress-testing-library/intro)
|
180
|
+
[timers](cypress/component/advanced/timers) | Testing components that set timers, adopted from [ReactJS Testing recipes](https://reactjs.org/docs/testing-recipes.html#timers)
|
181
|
+
[tutorial](cypress/component/advanced/tutorial) | A few tests adopted from [ReactJS Tutorial](https://reactjs.org/tutorial/tutorial.html), including Tic-Tac-Toe game
|
182
|
+
[use-local-storage](cypress/component/advanced/use-local-storage) | Use hooks to load and save items into `localStorage`
|
183
|
+
[portal](cypress/component/advanced/portal) | Component test for `ReactDOM.createPortal` feature
|
184
|
+
[radioactive-state](cypress/component/advanced/radioactive-state) | Testing components that use [radioactive-state](https://github.com/MananTank/radioactive-state) library
|
185
|
+
[react-bootstrap](cypress/component/advanced/react-bootstrap) | Confirms [react-bootstrap](https://react-bootstrap.github.io/) components are working
|
186
|
+
[select React component](cypress/component/advanced/react-book-example/src/components/ProductsList.spec.js) | Uses [cypress-react-selector](https://github.com/abhinaba-ghosh/cypress-react-selector) to find DOM elements using React component name and state values
|
187
|
+
[lazy-loaded](cypress/component/advanced/lazy-loaded-suspense) | Uses multiple chunks and async components with `React.lazy` + `React.Suspense`.
|
188
|
+
[i18n](cypress/component/advanced/i18n) | Uses[react-i18next](https://react.i18next.com/) for localizaiton.
|
189
|
+
[framer-motion](cypress/component/advanced/framer-motion) | Uses [framer motion](https://www.framer.com/motion/) for javascript-based animation.
|
190
|
+
<!-- prettier-ignore-end -->
|
191
|
+
|
192
|
+
### Full examples
|
193
|
+
|
194
|
+
We have several subfolders in [examples](examples) folder that have complete projects with just their dependencies installed in the root folder.
|
195
|
+
|
196
|
+
<!-- prettier-ignore-start -->
|
197
|
+
Folder Name | Description
|
198
|
+
--- | ---
|
199
|
+
[a11y](examples/a11y) | Testing components' accessibility using [cypress-axe](https://github.com/avanslaars/cypress-axe)
|
200
|
+
[react-scripts](examples/react-scripts) | A project using `react-scripts` with component tests in `src` folder, including the `.env` files demo.
|
201
|
+
[react-scripts-folder](examples/react-scripts-folder) | A project using `react-scripts` with component tests in `cypress/component`
|
202
|
+
[tailwind](examples/tailwind) | Testing styles built using [Tailwind CSS](https://tailwindcss.com/)
|
203
|
+
[sass-and-ts](examples/sass-and-ts) | Example with Webpack, Sass and TypeScript
|
204
|
+
[snapshots](examples/snapshots) | Component HTML and JSON snapshots using [cypress-plugin-snapshots](https://github.com/meinaart/cypress-plugin-snapshots)
|
205
|
+
[visual-sudoku](examples/visual-sudoku) | [Visual testing](#visual-testing) for components using open source plugin [cypress-image-snapshot](https://github.com/palmerhq/cypress-image-snapshot). For larger example with an hour long list of explanation videos, see [bahmutov/sudoku](https://github.com/bahmutov/sudoku).
|
206
|
+
[visual-testing-with-percy](examples/visual-testing-with-percy) | [Visual testing](#visual-testing) for components using 3rd party service [Percy.io](https://percy.io/)
|
207
|
+
[visual-testing-with-happo](examples/visual-testing-with-happo) | [Visual testing](#visual-testing) for components using 3rd party service [Happo](https://happo.io/)
|
208
|
+
[visual-testing-with-applitools](examples/visual-testing-with-applitools) | [Visual testing](#visual-testing) for components using 3rd party service [Applitools.com](https://applitools.com/)
|
209
|
+
[using-babel](examples/using-babel) | Bundling specs and loaded source files using project's existing `.babelrc` file
|
210
|
+
[webpack-file](examples/rollup) | Bundle component and specs using [rollup](http://rollupjs.org/guide/en/).
|
211
|
+
[webpack-file](examples/webpack-file) | Load existing `webpack.config.js` file
|
212
|
+
[webpack-options](examples/webpack-options) | Using the default Webpack options from `@cypress/webpack-preprocessor` to transpile JSX specs
|
213
|
+
<!-- prettier-ignore-end -->
|
214
|
+
|
215
|
+
### External examples
|
216
|
+
|
217
|
+
This way of component testing has been verified in a number of forked 3rd party projects.
|
218
|
+
|
219
|
+
<!-- prettier-ignore-start -->
|
220
|
+
Repo | Description
|
221
|
+
--- | ---
|
222
|
+
[try-cra-with-unit-test](https://github.com/bahmutov/try-cra-with-unit-test) | Hello world initialized with CRAv3
|
223
|
+
[try-cra-app-typescript](https://github.com/bahmutov/try-cra-app-typescript) | Hello world initialized with CRAv3 `--typescript`
|
224
|
+
[react-todo-with-hooks](https://github.com/bahmutov/react-todo-with-hooks) | Modern web application using hooks
|
225
|
+
[test-redux-examples](https://github.com/bahmutov/test-redux-examples) | Example apps copies from official Redux repo and tested as components
|
226
|
+
[test-react-hooks-animations](https://github.com/bahmutov/test-react-hooks-animations) | Testing React springs fun blob animation
|
227
|
+
[test-mdx-example](https://github.com/bahmutov/test-mdx-example) | Example testing MDX components using Cypress
|
228
|
+
[test-apollo](https://github.com/bahmutov/test-apollo) | Component testing an application that uses Apollo GraphQL library
|
229
|
+
[test-xstate-react](https://github.com/bahmutov/test-xstate-react) | XState component testing using Cypress
|
230
|
+
[test-react-router-v5](https://github.com/bahmutov/test-react-router-v5) | A few tests of React Router v5
|
231
|
+
[test-material-ui](https://github.com/bahmutov/test-material-ui) | Testing Material UI components: date pickers, lists, autocomplete
|
232
|
+
[test-d3-react-gauge](https://github.com/bahmutov/test-d3-react-gauge) | Testing React D3 gauges
|
233
|
+
[storybook-code-coverage](https://github.com/bahmutov/storybook-code-coverage) | Example app where we get 100% code coverage easily with a single integration spec and a few component specs, replacing [several tools](https://dev.to/penx/combining-storybook-cypress-and-jest-code-coverage-4pa5)
|
234
|
+
[react-loading-skeleton](https://github.com/bahmutov/react-loading-skeleton) | One to one Storybook tests for React skeleton components. Uses local `.babelrc` settings without Webpack config
|
235
|
+
[test-swr](https://github.com/bahmutov/test-swr) | Component test for [Zeit SWR](https://github.com/zeit/swr) hooks for remote data fetching
|
236
|
+
[emoji-search](https://github.com/bahmutov/emoji-search) | Quick component test for a fork of emoji-search
|
237
|
+
[test-custom-error-boundary](https://github.com/bahmutov/test-custom-error-boundary) | Play with a component that implements error boundary
|
238
|
+
[Jscrambler-Webpack-React](https://github.com/bahmutov/Jscrambler-Webpack-React) | Example project with its own Webpack config file
|
239
|
+
[bahmutov/integration-tests](https://github.com/bahmutov/integration-tests) | Example based on blog post [React Integration Testing: Greater Coverage, Fewer Tests](https://css-tricks.com/react-integration-testing-greater-coverage-fewer-tests/)
|
240
|
+
[mobx-react-typescript-boilerplate](https://github.com/bahmutov/mobx-react-typescript-boilerplate) | Fork of the official Mobx example, shows clock control
|
241
|
+
[bahmutov/test-react-hook-form](https://github.com/bahmutov/test-react-hook-form) | Testing forms created using [react-hook-form](https://github.com/react-hook-form/react-hook-form)
|
242
|
+
[bahmutov/testing-react-example](https://github.com/bahmutov/testing-react-example) | Described in blog post [Test React Component with @cypress/react Example](https://dev.to/bahmutov/test-react-component-with-@cypress/react-example-4d99)
|
243
|
+
[ejected-react-scripts-example](https://github.com/bahmutov/ejected-react-scripts-example) | Using component testing after ejecting `react-scripts`
|
244
|
+
[tic-tac-toe](https://github.com/bahmutov/react-tic-tac-toe-example) | Component and unit tests for Tic-Tac-Toe, read [Tic-Tac-Toe Component Tests](https://glebbahmutov.com/blog/tic-tac-toe-component-tests/)
|
245
|
+
[react-hooks-file-upload](https://github.com/bahmutov/react-hooks-file-upload) | Upload a file from the component while stubbing the server
|
246
|
+
[react-query-example](https://github.com/bahmutov/react-query-example) | Quick test example for components that use `react-query` with mock clock control
|
247
|
+
<!-- prettier-ignore-end -->
|
248
|
+
|
249
|
+
To find more examples, see GitHub topic [@cypress/react-example](https://github.com/topics/@cypress/react-example)
|
250
|
+
|
251
|
+
## Options
|
252
|
+
|
253
|
+
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:
|
254
|
+
|
255
|
+
```js
|
256
|
+
// src/Footer.spec.js
|
257
|
+
import './styles/main.css'
|
258
|
+
import Footer from './Footer'
|
259
|
+
it('looks right', () => {
|
260
|
+
// styles are applied
|
261
|
+
mount(<Footer />)
|
262
|
+
})
|
263
|
+
```
|
264
|
+
|
265
|
+
### Extra styles
|
266
|
+
|
267
|
+
You can pass additional styles, css files and external stylesheets to load, see [docs/styles.md](./docs/styles.md) for the full list of options.
|
268
|
+
|
269
|
+
```js
|
270
|
+
const todo = {
|
271
|
+
id: '123',
|
272
|
+
title: 'Write more tests',
|
273
|
+
}
|
274
|
+
mount(<Todo todo={todo} />, {
|
275
|
+
stylesheets: [
|
276
|
+
'https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.css',
|
277
|
+
],
|
278
|
+
})
|
279
|
+
```
|
280
|
+
|
281
|
+
<details>
|
282
|
+
<summary>Additional configuration</summary>
|
283
|
+
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:
|
284
|
+
|
285
|
+
```json
|
286
|
+
{
|
287
|
+
"env": {
|
288
|
+
"@cypress/react": {
|
289
|
+
"react": "node_modules/react/umd/react.development.js",
|
290
|
+
"react-dom": "node_modules/react-dom/umd/react-dom.development.js"
|
291
|
+
}
|
292
|
+
}
|
293
|
+
}
|
294
|
+
```
|
295
|
+
|
296
|
+
</details>
|
297
|
+
|
298
|
+
You may also specify the `ReactDOM` package to use. This can be useful in complex monorepo setups that have different versions of React and React DOM installed. If you see an error relating to [mismatching versions of React or React DOM](https://reactjs.org/warnings/invalid-hook-call-warning.html#mismatching-versions-of-react-and-react-dom), this may be the solution. You can do this using the `ReactDom` option:
|
299
|
+
|
300
|
+
```jsx
|
301
|
+
// if you have multiple versions of ReactDom in your monorepo
|
302
|
+
import ReactDom from 'react-dom'
|
303
|
+
|
304
|
+
mount(<Todo todo={todo} />, {
|
305
|
+
stylesheets: [
|
306
|
+
'https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.css',
|
307
|
+
],
|
308
|
+
ReactDom
|
309
|
+
})
|
310
|
+
```
|
311
|
+
|
312
|
+
## Code coverage
|
313
|
+
|
314
|
+
In order to use code coverage you can follow the instructions from [docs](https://github.com/cypress-io/code-coverage). In most of cases you need to install 2 dependencies:
|
315
|
+
|
316
|
+
```
|
317
|
+
npm i @cypress/code-coverage babel-plugin-istanbul
|
318
|
+
|
319
|
+
yarn add @cypress/code-coverage babel-plugin-istanbul
|
320
|
+
```
|
321
|
+
|
322
|
+
If you are using [plugins/cra-v3](plugins/cra-v3) it instruments the code on the fly using `babel-plugin-istanbul` and generates report using dependency [cypress-io/code-coverage](https://github.com/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
|
323
|
+
|
324
|
+
```json
|
325
|
+
{
|
326
|
+
"env": {
|
327
|
+
"coverage": false
|
328
|
+
}
|
329
|
+
}
|
330
|
+
```
|
331
|
+
|
332
|
+
## Visual testing
|
333
|
+
|
334
|
+
You can use any Cypress [Visual Testing plugin](https://on.cypress.io/plugins#visual-testing) to perform [visual testing](https://on.cypress.io/visual-testing) from the component tests. This repo has several example projects, see [visual-sudoku](examples/visual-sudoku), [visual-testing-with-percy](examples/visual-testing-with-percy), [visual-testing-with-happo](examples/visual-testing-with-happo), and [visual-testing-with-applitools](examples/visual-testing-with-applitools).
|
335
|
+
|
336
|
+
For a larger Do-It-Yourself example with an hour long list of explanation videos, see [bahmutov/sudoku](https://github.com/bahmutov/sudoku) repository. I explain how to write visual testing using open source tools in this [blog post](https://glebbahmutov.com/blog/open-source-visual-testing-of-components/), [video talk](https://www.youtube.com/watch?v=00BNExlJUU8), and [slides](https://slides.com/bahmutov/i-see-what-is-going-on).
|
337
|
+
|
338
|
+
## Common problems
|
339
|
+
|
340
|
+
<details id="fast-enough">
|
341
|
+
<summary>Slower than Jest</summary>
|
342
|
+
|
343
|
+
When you use `cypress-X-unit-test` for component testing, you might notice the tests are slower than using Jest to test the same components. Yes, that's true. A test runner could be made _extremely_ fast if it did nothing, just check out the [auchenberg/volkswagen](https://github.com/auchenberg/volkswagen) test runner - it is blazing on CI 😉. Of course, Jest does do things, just not inside the real browser environment.
|
344
|
+
|
345
|
+
Testing using Jest with its jsdom browser is faster than starting the real browser, loading all libraries, mounting the component and then waiting for the component to actually perform its work in response to the test's actions. But do those tests give you a true confidence that the component is working?
|
346
|
+
|
347
|
+
Try this test 🙈
|
348
|
+
|
349
|
+
Spoiler: it fails, [proof](https://codesandbox.io/s/react-testing-library-demo-forked-z7l2o?file=/src/__tests__/components.js).
|
350
|
+
|
351
|
+
```js
|
352
|
+
const mock = jest.fn()
|
353
|
+
// render a component that does NOT allow any click events
|
354
|
+
// using pointerEvents: "none" style
|
355
|
+
const { getByRole } = render(
|
356
|
+
<button style={{ pointerEvents: 'none' }} onClick={mock}>
|
357
|
+
text
|
358
|
+
</button>,
|
359
|
+
)
|
360
|
+
// Jest happily clicks
|
361
|
+
fireEvent.click(getByRole('button'))
|
362
|
+
expect(mock).not.toBeCalled()
|
363
|
+
```
|
364
|
+
|
365
|
+
Cypress test on the other hand [fails correctly](cypress/component/basic/fails-correctly).
|
366
|
+
|
367
|
+
We think that using `cypress-X-unit-test` runs tests as _fast as your application code is_, and often you need to think how to _slow down_ the Cypress Test Runner so it does not run away from the component's code, just see our blog posts dealing with [test flake](https://cypress.io/blog/tag/flake/).
|
368
|
+
|
369
|
+
From the developer's perspective I would ask myself: which tests do I _write faster_? What happens when a test fails and I need to debug the failure: which test runner allows me to _debug a failed test quicker_? While I am partial, I have to say, realistic Cypress tests are easier to write and debug.
|
370
|
+
|
371
|
+
Finally, when running tests on the continuous integration service, the true test speed up comes from properly configuring [dependencies caching](https://on.cypress.io/caching) and running [tests in parallel](https://on.cypress.io/parallelization) - something we have extensively documented and consider a solved problem.
|
372
|
+
|
373
|
+
</details>
|
374
|
+
|
375
|
+
## Context Provider usage
|
376
|
+
|
377
|
+
React context provider usage and API described in [./docs/providers-and-composition.md](./docs/providers-and-composition.md)
|
378
|
+
|
379
|
+
## Chat
|
380
|
+
|
381
|
+
Come chat with us [on discord](https://discord.gg/7ZHYhZSW) in the #component-testing channel.
|
382
|
+
|
383
|
+
## Development
|
384
|
+
|
385
|
+
See [docs/development.md](./docs/development.md)
|
386
|
+
|
387
|
+
## Debugging
|
388
|
+
|
389
|
+
You can see verbose logs from this plugin by running with environment variable
|
390
|
+
|
391
|
+
```
|
392
|
+
DEBUG=@cypress/react
|
393
|
+
```
|
394
|
+
|
395
|
+
Because finding and modifying Webpack settings while running this plugin is done by [find-webpack](https://github.com/bahmutov/find-webpack) module, you might want to enable its debug messages too.
|
396
|
+
|
397
|
+
```
|
398
|
+
DEBUG=@cypress/react,find-webpack
|
399
|
+
```
|
400
|
+
|
401
|
+
## Changelog
|
402
|
+
|
403
|
+
[Changelog](./CHANGELOG.md)
|
404
|
+
|
405
|
+
## Related tools
|
406
|
+
|
407
|
+
Same feature for unit testing components from other frameworks using Cypress
|
408
|
+
|
409
|
+
- [@cypress/vue](https://github.com/cypress-io/cypress/tree/develop/npm/vue)
|
410
|
+
- [cypress-cycle-unit-test](https://github.com/bahmutov/cypress-cycle-unit-test)
|
411
|
+
- [cypress-svelte-unit-test](https://github.com/bahmutov/cypress-svelte-unit-test)
|
412
|
+
- [@cypress/angular](https://github.com/bahmutov/@cypress/angular)
|
413
|
+
- [cypress-hyperapp-unit-test](https://github.com/bahmutov/cypress-hyperapp-unit-test)
|
414
|
+
- [cypress-angularjs-unit-test](https://github.com/bahmutov/cypress-angularjs-unit-test)
|