|
1 | | -# cypress-react-unit-test [](https://travis-ci.org/bahmutov/cypress-react-unit-test) [](https://dashboard.cypress.io/#/projects/z9dxah) [![renovate-app badge][renovate-badge]][renovate-app] [](https://percy.io/bahmutov/cypress-react-unit-test) |
| 1 | +# cypress-react-unit-test [](https://circleci.com/gh/bahmutov/cypress-react-unit-test/tree/master) [](https://dashboard.cypress.io/#/projects/z9dxah) [![renovate-app badge][renovate-badge]][renovate-app] [](https://percy.io/bahmutov/cypress-react-unit-test) |
2 | 2 |
|
3 | 3 | > A little helper to unit test React components in the open source [Cypress.io](https://www.cypress.io/) E2E test runner **ALPHA** |
4 | 4 |
|
|
16 | 16 |
|
17 | 17 | ## Install |
18 | 18 |
|
19 | | -Requires [Node](https://nodejs.org/en/) version 6 or above. |
| 19 | +Requires [Node](https://nodejs.org/en/) version 8 or above. |
20 | 20 |
|
21 | 21 | ```sh |
22 | 22 | npm install --save-dev cypress cypress-react-unit-test |
@@ -63,79 +63,75 @@ describe('HelloState component', () => { |
63 | 63 |
|
64 | 64 |  |
65 | 65 |
|
66 | | -## Configuration |
| 66 | +### styles |
67 | 67 |
|
68 | | -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: |
| 68 | +You can add individual style to the mounted component by passing its text as an option |
69 | 69 |
|
70 | | -```json |
71 | | -{ |
72 | | - "env": { |
73 | | - "cypress-react-unit-test": { |
74 | | - "react": "node_modules/react/umd/react.development.js", |
75 | | - "react-dom": "node_modules/react-dom/umd/react-dom.development.js" |
| 70 | +```js |
| 71 | +it('can be passed as an option', () => { |
| 72 | + const style = ` |
| 73 | + .component-button { |
| 74 | + display: inline-flex; |
| 75 | + width: 25%; |
| 76 | + flex: 1 0 auto; |
76 | 77 | } |
77 | | - } |
78 | | -} |
79 | | -``` |
80 | | - |
81 | | -## Transpilation |
82 | 78 |
|
83 | | -How can we use features that require transpilation? Using [@cypress/webpack-preprocessor](https://github.com/cypress-io/cypress-webpack-preprocessor#readme). You can use [cypress/plugins/index.js](cypress/plugins/index.js) to configure any transpilation plugins you need. |
| 79 | + .component-button.orange button { |
| 80 | + background-color: #F5923E; |
| 81 | + color: white; |
| 82 | + } |
| 83 | + ` |
| 84 | + cy.mount(<Button name='Orange' orange />, null, { style }) |
| 85 | + cy.get('.orange button') |
| 86 | + .should('have.css', 'background-color', 'rgb(245, 146, 62)') |
| 87 | +}) |
| 88 | +``` |
84 | 89 |
|
85 | | -For example, to enable class properties: |
| 90 | +Often your component rely on global CSS style imported from the root `index.js` or `app.js` file |
86 | 91 |
|
87 | 92 | ```js |
88 | | -// cypress/plugins/index.js |
89 | | -const webpack = require('@cypress/webpack-preprocessor') |
90 | | -const webpackOptions = { |
91 | | - module: { |
92 | | - rules: [ |
93 | | - { |
94 | | - test: /\.(js|jsx|mjs)$/, |
95 | | - loader: 'babel-loader', |
96 | | - options: { |
97 | | - presets: ['@babel/preset-env', '@babel/preset-react'], |
98 | | - plugins: ['@babel/plugin-proposal-class-properties'], |
99 | | - }, |
100 | | - } |
101 | | - ] |
102 | | - } |
103 | | -} |
| 93 | +// index.js |
| 94 | +import './styles.css' |
| 95 | +// bootstrap application |
| 96 | +``` |
104 | 97 |
|
105 | | -const options = { |
106 | | - // send in the options from your webpack.config.js, so it works the same |
107 | | - // as your app's code |
108 | | - webpackOptions, |
109 | | - watchOptions: {} |
110 | | -} |
| 98 | +You can read the CSS file and pass it as `style` option yourself |
111 | 99 |
|
112 | | -module.exports = on => { |
113 | | - on('file:preprocessor', webpack(options)) |
114 | | -} |
| 100 | +```js |
| 101 | +cy.readFile('cypress/integration/Button.css') |
| 102 | + .then(style => { |
| 103 | + cy.mount(<Button name='Orange' orange />, null, { style }) |
| 104 | + }) |
115 | 105 | ``` |
116 | 106 |
|
117 | | -Install dev dependencies |
| 107 | +You can even let Cypress read the file and inject the style |
118 | 108 |
|
119 | | -```shell |
120 | | -npm i -D @cypress/webpack-preprocessor \ |
121 | | - babel-loader @babel/preset-env @babel/preset-react \ |
122 | | - @babel/plugin-proposal-class-properties |
| 109 | +```js |
| 110 | +const cssFile = 'cypress/integration/Button.css' |
| 111 | +cy.mount(<Button name='Orange' orange />, null, { cssFile }) |
123 | 112 | ``` |
124 | 113 |
|
125 | | -And write a component using class properties |
| 114 | +See [cypress/integration/inject-style-spec.js](cypress/integration/inject-style-spec.js) for more examples. |
126 | 115 |
|
127 | | -```js |
128 | | -import React from 'react' |
| 116 | +## Configuration |
129 | 117 |
|
130 | | -export class Transpiled extends React.Component { |
131 | | - state = { |
132 | | - count: 0 |
133 | | - } |
| 118 | +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: |
134 | 119 |
|
135 | | - // ... |
| 120 | +```json |
| 121 | +{ |
| 122 | + "env": { |
| 123 | + "cypress-react-unit-test": { |
| 124 | + "react": "node_modules/react/umd/react.development.js", |
| 125 | + "react-dom": "node_modules/react-dom/umd/react-dom.development.js" |
| 126 | + } |
| 127 | + } |
136 | 128 | } |
137 | 129 | ``` |
138 | 130 |
|
| 131 | +## Transpilation |
| 132 | + |
| 133 | +How can we use features that require transpilation? By using [@cypress/webpack-preprocessor](https://github.com/cypress-io/cypress-webpack-preprocessor#readme) - see the plugin configuration in [cypress/plugins/index.js](cypress/plugins/index.js) |
| 134 | + |
139 | 135 | ## Examples |
140 | 136 |
|
141 | 137 | All components are in [src](src) folder. All tests are in [cypress/integration](cypress/integration) folder. |
|
0 commit comments