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

Commit d7c34f6

Browse files
committed
Merge branch 'master' into main
2 parents 9eb68c1 + 2a4a546 commit d7c34f6

File tree

13 files changed

+294
-46
lines changed

13 files changed

+294
-46
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Code coverage | ✅ | ✅
4747
- [Unit Testing React components with Cypress](https://itnext.io/unit-testing-react-components-with-cypress-4d4cf8cd59a0)
4848
- [Test React Component with cypress-react-unit-test Example](https://dev.to/bahmutov/test-react-component-with-cypress-react-unit-test-example-4d99)
4949
- [Tic-Tac-Toe Component Tests](https://glebbahmutov.com/blog/tic-tac-toe-component-tests/)
50+
- [Using .env and .env.test from React component tests](https://medium.com/@bahmutov/using-env-and-env-test-from-react-component-tests-c11aa2040bc8)
51+
- [Visual testing for React components using open source tools](https://glebbahmutov.com/blog/open-source-visual-testing-of-components/)
5052

5153
## Known problems
5254

@@ -218,6 +220,7 @@ Repo | Description
218220
[ejected-react-scripts-example](https://github.com/bahmutov/ejected-react-scripts-example) | Using component testing after ejecting `react-scripts`
219221
[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/)
220222
[react-hooks-file-upload](https://github.com/bahmutov/react-hooks-file-upload) | Upload a file from the component while stubbing the server
223+
[react-query-example](https://github.com/bahmutov/react-query-example) | Quick test example for components that use `react-query` with mock clock control
221224
<!-- prettier-ignore-end -->
222225

223226
To find more examples, see GitHub topic [cypress-react-unit-test-example](https://github.com/topics/cypress-react-unit-test-example)

examples/a11y/README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,33 @@
22

33
> Testing component accessibility
44
5-
```shell
5+
## Usage
6+
7+
1. Make sure the root project has been built .
8+
9+
```bash
10+
# in the root of the project
11+
npm install
12+
npm run build
13+
```
14+
15+
2. Run `npm install` in this folder to symlink the `cypress-react-unit-test` dependency.
16+
17+
```bash
18+
# in this folder
619
npm install
20+
```
21+
22+
3. Start Cypress
23+
24+
```bash
725
npm run cy:open
826
# or just run headless tests
927
npm test
1028
```
1129

30+
## Example
31+
1232
Testing components following the [React accessibility guide](https://reactjs.org/docs/accessibility.html) using [cypress-axe](https://github.com/avanslaars/cypress-axe) plugin.
1333

1434
See the spec file [cypress/component/spec.js](cypress/component/spec.js). For example, an `<input>` without a label is caught:

examples/react-scripts-folder/README.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
# example: react-scripts-folder
22

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.
3+
> A typical project using `react-scripts` with components in the [src](src) folder and component tests inside [cypress/component](cypress/component) folder.
44
5-
Note: run `npm install` in this folder to symlink `cypress-react-unit-test` dependency.
5+
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.
66

7-
```shell
7+
![App test](images/app-test.png)
8+
9+
## Usage
10+
11+
1. Make sure the root project has been built .
12+
13+
```bash
14+
# in the root of the project
15+
npm install
16+
npm run build
17+
```
18+
19+
2. Run `npm install` in this folder to symlink the `cypress-react-unit-test` dependency.
20+
21+
```bash
22+
# in this folder
23+
npm install
24+
```
25+
26+
3. Start Cypress
27+
28+
```bash
829
npm run cy:open
930
# or just run headless tests
1031
npm test
1132
```
12-
13-
![App test](images/app-test.png)

examples/react-scripts/README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
# example: react-scripts
22

3-
A typical project using `react-scripts` with components and matching component tests residing in the [src](src) folder.
3+
> A typical project using `react-scripts` with components and matching component tests residing in the [src](src) folder.
44
5-
Note: run `npm install` in this folder to symlink the `cypress-react-unit-test` dependency.
5+
## Usage
66

7-
```shell
7+
1. Make sure the root project has been built .
8+
9+
```bash
10+
# in the root of the project
11+
npm install
12+
npm run build
13+
```
14+
15+
2. Run `npm install` in this folder to symlink the `cypress-react-unit-test` dependency.
16+
17+
```bash
18+
# in this folder
819
npm install
20+
```
21+
22+
3. Start Cypress
23+
24+
```bash
925
npm run cy:open
1026
# or just run headless tests
1127
npm test
1228
```
1329

30+
## Notes
31+
1432
![App test](images/app-test.png)
1533

1634
The spec [src/Logo.cy-spec.js](src/Logo.cy-spec.js) directly imports SVG into React spec file. The spec [src/resources.cy-spec.js](src/resources.cy-spec.js) confirm that static resources like SVG and fonts load correctly.

examples/snapshots/README.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,38 @@
22

33
> Component testing with snapshots using [cypress-plugin-snapshots](https://github.com/meinaart/cypress-plugin-snapshots)
44
5-
Note: run `npm install` in this folder to symlink `cypress-react-unit-test` dependency.
5+
Example component taken from [Snapshot Testing React Components with Jest](https://semaphoreci.com/community/tutorials/snapshot-testing-react-components-with-jest)
6+
7+
![Snapshot test](images/snapshot-test.png)
8+
9+
See [cypress/component/PositiveCounter-spec.js](cypress/component/PositiveCounter-spec.js) and saved snapshots in [cypress/component/**snapshots**/](cypress/component/__snapshots__/PositiveCounter-spec.js.snap).
610

7-
```shell
11+
## Usage
12+
13+
1. Make sure the root project has been built .
14+
15+
```bash
16+
# in the root of the project
817
npm install
9-
npm run cy:open
10-
# or just run headless tests
11-
npm test
18+
npm run build
1219
```
1320

14-
Example component taken from [Snapshot Testing React Components with Jest](https://semaphoreci.com/community/tutorials/snapshot-testing-react-components-with-jest)
21+
2. Run `npm install` in this folder to symlink the `cypress-react-unit-test` dependency.
1522

16-
![Snapshot test](images/snapshot-test.png)
23+
```bash
24+
# in this folder
25+
npm install
26+
```
1727

18-
See [cypress/component/PositiveCounter-spec.js](cypress/component/PositiveCounter-spec.js) and saved snapshots in [cypress/component/**snapshots**/](cypress/component/__snapshots__/PositiveCounter-spec.js.snap).
28+
3. Start Cypress
29+
30+
```bash
31+
npm run cy:open
32+
# or just run headless tests
33+
npm test
34+
```
1935

20-
## Failing test
36+
## Failing tests
2137

2238
If the snapshot contents does not match the saved snapshot file, the test fails. You can click on the snapshot command, review the difference, and if the difference is expected, update the snapshot from the Test Runner's GUI.
2339

examples/tailwind/README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
# example: tailwind
22

3-
Component testing when using [Tailwind CSS](https://tailwindcss.com/), example created following the blog post [Using Tailwind CSS With React](https://medium.com/codingthesmartway-com-blog/using-tailwind-css-with-react-ced163d0e9e9) and Tailwind's own documentation.
3+
> Component testing when using [Tailwind CSS](https://tailwindcss.com/), example created following the blog post [Using Tailwind CSS With React](https://medium.com/codingthesmartway-com-blog/using-tailwind-css-with-react-ced163d0e9e9) and Tailwind's own documentation.
44
5-
## Installation
5+
## Usage
66

7-
Run `npm install` in this folder to symlink `cypress-react-unit-test` dependency first.
7+
1. Make sure the root project has been built .
88

9-
```shell
9+
```bash
10+
# in the root of the project
11+
npm install
12+
npm run build
13+
```
14+
15+
2. Run `npm install` in this folder to symlink the `cypress-react-unit-test` dependency.
16+
17+
```bash
18+
# in this folder
1019
npm install
1120
```
1221

22+
3. Start Cypress
23+
24+
```bash
25+
npm run cy:open
26+
# or just run headless tests
27+
npm test
28+
```
29+
1330
## Run tests
1431

1532
You can execute `npm run build:css` to let Tailwind generate `src/styles/main.generated.css` or run Cypress, since this step is set as a pre-test step inside [package.json](package.json).

examples/using-babel/README.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
# example: using-babel
22

3-
Component testing for projects using Babel config
3+
> Component testing for projects using Babel config
44
5-
Note: run `npm install` in this folder to symlink `cypress-react-unit-test` dependency.
5+
![Example component test](images/dynamic.gif)
6+
7+
## Usage
8+
9+
1. Make sure the root project has been built .
10+
11+
```bash
12+
# in the root of the project
13+
npm install
14+
npm run build
15+
```
16+
17+
2. Run `npm install` in this folder to symlink the `cypress-react-unit-test` dependency.
618

7-
```shell
19+
```bash
20+
# in this folder
21+
npm install
22+
```
23+
24+
3. Start Cypress
25+
26+
```bash
827
npm run cy:open
9-
# or run headless tests
28+
# or just run headless tests
1029
npm test
1130
```
1231

13-
![Example component test](images/dynamic.gif)
14-
1532
## Specs
1633

1734
See spec files [src/\*.spec.js](src). The specs are bundled using [.babelrc](.babelrc) settings via [cypress/plugins/index.js](cypress/plugins/index.js) file that includes file preprocessor

examples/visual-sudoku/README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
# example: visual tests for Sudoku
22

3-
This React Sudoku app was cloned from [raravi/sudoku](https://github.com/raravi/sudoku).
3+
> This React Sudoku app was cloned from [raravi/sudoku](https://github.com/raravi/sudoku).
44
55
Visual testing uses open source [palmerhq/cypress-image-snapshot](https://github.com/palmerhq/cypress-image-snapshot) plugin.
66

7-
Note: run `npm install` in this folder to symlink `cypress-react-unit-test` dependency.
7+
## Usage
8+
9+
1. Make sure the root project has been built .
10+
11+
```bash
12+
# in the root of the project
13+
npm install
14+
npm run build
15+
```
16+
17+
2. Run `npm install` in this folder to symlink the `cypress-react-unit-test` dependency.
18+
19+
```bash
20+
# in this folder
21+
npm install
22+
```
23+
24+
3. Start Cypress
25+
26+
```bash
27+
npm run cy:open
28+
# or just run headless tests
29+
npm test
30+
```
31+
32+
## Running Tests
833

934
Example failing test [src/App.cy-spec.js](src/App.cy-spec.js)
1035

examples/visual-testing-with-applitools/README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
11
# example: visual testing using Applitools
22

3-
Shows how to use Applitools to visually check React charts made with [Victory](https://formidable.com/open-source/victory/) library.
3+
> An example using Applitools to visually check React charts made with the [Victory](https://formidable.com/open-source/victory/) library.
44
55
![Screenshot](images/eyes.png)
66

77
See [src/Chart.spec.js](src/Chart.spec.js)
88

9-
Note: run `npm install` in this folder to symlink `cypress-react-unit-test` dependency.
9+
## Usage
10+
11+
1. Make sure the root project has been built .
12+
13+
```bash
14+
# in the root of the project
15+
npm install
16+
npm run build
17+
```
18+
19+
2. Run `npm install` in this folder to symlink the `cypress-react-unit-test` dependency.
20+
21+
```bash
22+
# in this folder
23+
npm install
24+
```
25+
26+
3. Start Cypress
27+
28+
```bash
29+
npm run cy:open
30+
# or just run headless tests
31+
npm test
32+
```
33+
34+
## Notes
1035

1136
The `cy.eyes*` commands are ignored during local interactive run. On CI, the `cy.eyes*` send the snapshots to Applitools servers to be analyzed and compared against the baseline images.
1237

examples/visual-testing-with-happo/README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,49 @@
66

77
> Visual component testing using [Happo.io](https://docs.happo.io/docs/cypress)
88
9-
Note: run `npm install` in this folder to symlink `cypress-react-unit-test` dependency.
9+
## Usage
1010

11-
Example PR with code a change where we forgot to update the styles with additional classes: [#210](https://github.com/bahmutov/cypress-react-unit-test/pull/210)
11+
1. Make sure the root project has been built .
12+
13+
```bash
14+
# in the root of the project
15+
npm install
16+
npm run build
17+
```
18+
19+
2. Run `npm install` in this folder to symlink the `cypress-react-unit-test` dependency.
20+
21+
```bash
22+
# in this folder
23+
npm install
24+
```
25+
26+
3. Start Cypress
27+
28+
```bash
29+
npm run cy:open
30+
# or just run headless tests
31+
npm test
32+
```
33+
34+
## Notes
35+
36+
Here's an example PR with code a change where we forgot to update the styles with additional classes: [#210](https://github.com/bahmutov/cypress-react-unit-test/pull/210)
1237

1338
Notice that CircleCI passes the test, because functionally it worked, but the Happo CI check shows a failure:
1439

1540
![CI checks](images/ci.png)
1641

1742
Happo report shows 3 views:
1843

19-
## Side by side
44+
### Side by side
2045

2146
![Side by side view](images/side-by-side.png)
2247

23-
## Diff
48+
### Diff
2449

2550
![Diff view](images/diff.gif)
2651

27-
## Swipe
52+
### Swipe
2853

2954
![Swipe view](images/swipe.gif)

0 commit comments

Comments
 (0)