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

Commit 28a929d

Browse files
authored
feat: add component testing folder to react-scripts Babel transpile list (#186)
If you use `react-scripts`, you can have your component tests in `src` or in any other folder like `cypress/component` and they will be transpiled using the same settings.
1 parent b6fce40 commit 28a929d

34 files changed

+469
-8
lines changed

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
> A little helper to unit test React components in the open source [Cypress.io](https://www.cypress.io/) E2E test runner **v4.5.0+**
44
5+
**Jump to:** [Comparison](#comparison), [Install](#install), Examples: [basic](#basic-examples), [advanced](#advanced-examples), [full](#full-examples), [external](#external-examples), [Options](#options), [Code coverage](#code-coverage), [Visual testing](#visual-testing), [Commoon problems](#common-problems)
6+
57
## TLDR
68

79
- 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.
@@ -64,8 +66,6 @@ module.exports = (on, config) => {
6466

6567
See [Recipes](./docs/recipes.md) for more examples.
6668

67-
**⚠️ Note:** when using `react-scripts` you must place component specs in the `src` folder too, otherwise they won't be transpiled correctly.
68-
6969
3. ⚠️ 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:
7070

7171
```json
@@ -145,7 +145,18 @@ Spec | Description
145145
[react-bootstrap](cypress/component/advanced/react-bootstrap) | Confirms [react-bootstrap](https://react-bootstrap.github.io/) components are working
146146
<!-- prettier-ignore-end -->
147147

148-
### Large examples
148+
### Full examples
149+
150+
We have several subfolders in [examples](examples) folder that have complete projects with just their dependencies installed in the root folder.
151+
152+
<!-- prettier-ignore-start -->
153+
Folder Name | Description
154+
--- | ---
155+
[react-scripts](examples/react-scripts) | A project using `react-scripts` with component tests in `src` folder
156+
[react-scripts-folder](examples/react-scripts-folder) | A project using `react-scripts` with component tests in `cypress/component`
157+
<!-- prettier-ignore-end -->
158+
159+
### External examples
149160

150161
This way of component testing has been verified in a number of forked 3rd party projects.
151162

circle.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ workflows:
3333
command: npm test
3434
store_artifacts: true
3535

36+
- cypress/run:
37+
# react-scripts example with component tests not in "src" folder
38+
# but in "cypress/component" folder
39+
name: Example Component Folder
40+
requires:
41+
- Install
42+
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
43+
install-command: npm install
44+
verify-command: echo 'Already verified'
45+
no-workspace: true
46+
working_directory: examples/react-scripts-folder
47+
command: npm test
48+
store_artifacts: true
49+
3650
- cypress/run:
3751
name: Test
3852
parallelism: 4
@@ -63,6 +77,7 @@ workflows:
6377
- Install
6478
- Test
6579
- Example React Scripts
80+
- Example Component Folder
6681
install-command: echo 'Already installed'
6782
verify-command: echo 'Already verified'
6883
no-workspace: true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# example: react-scripts-folder
2+
3+
A typical project using `react-scripts` with components in the [src](src) folder and component tests inside [cypress/component](cypress/component) folder. Cypress automatically finds Webpack settings used by `react-scripts` and inserts the component folder name allowing to transpile the component specs the same way the `src` code is transpiled.
4+
5+
Note: run `npm install` in this folder to symlink `cypress-react-unit-test` dependency.
6+
7+
```shell
8+
npm run cy:open
9+
# or just run headless tests
10+
npm test
11+
```
12+
13+
![App test](images/app-test.png)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"fixturesFolder": false,
3+
"testFiles": "**/*cy-spec.js",
4+
"viewportWidth": 500,
5+
"viewportHeight": 500,
6+
"experimentalComponentTesting": true,
7+
"componentFolder": "cypress/component"
8+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference types="cypress" />
2+
// compare to App.test.js
3+
import React from 'react'
4+
import App from '../../src/App'
5+
import { mount } from 'cypress-react-unit-test'
6+
7+
describe('App', () => {
8+
it('renders learn react link', () => {
9+
expect(1).to.equal(1)
10+
mount(<App />)
11+
cy.contains(/Learn React/)
12+
})
13+
14+
it('renders inline component', () => {
15+
mount(<div>JSX</div>)
16+
cy.contains('JSX')
17+
})
18+
})
File renamed without changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const preprocessor = require('../../../../plugins/react-scripts')
2+
module.exports = (on, config) => {
3+
preprocessor(on, config)
4+
// IMPORTANT to return the config object
5+
// with the any changed environment variables
6+
return config
7+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require('cypress-react-unit-test/support')
2+
require('@cypress/code-coverage/support')
287 KB
Loading

0 commit comments

Comments
 (0)