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

Commit 1f3b024

Browse files
authored
add visual testing example (#194)
1 parent 0e49585 commit 1f3b024

File tree

15 files changed

+756
-519
lines changed

15 files changed

+756
-519
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# cypress-react-unit-test [![CircleCI](https://circleci.com/gh/bahmutov/cypress-react-unit-test/tree/master.svg?style=svg)](https://circleci.com/gh/bahmutov/cypress-react-unit-test/tree/master) [![Cypress.io tests](https://img.shields.io/badge/cypress.io-tests-green.svg?style=flat-square)](https://dashboard.cypress.io/#/projects/z9dxah) [![renovate-app badge][renovate-badge]][renovate-app] [![This project is using Percy.io for visual regression testing.](https://percy.io/static/images/percy-badge.svg)](https://percy.io/bahmutov/cypress-react-unit-test)
1+
# cypress-react-unit-test [![CircleCI](https://circleci.com/gh/bahmutov/cypress-react-unit-test/tree/master.svg?style=svg)](https://circleci.com/gh/bahmutov/cypress-react-unit-test/tree/master) [![Cypress.io tests](https://img.shields.io/badge/cypress.io-tests-green.svg?style=flat-square)](https://dashboard.cypress.io/#/projects/z9dxah) [![renovate-app badge][renovate-badge]][renovate-app]
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
@@ -155,6 +155,7 @@ Folder Name | Description
155155
--- | ---
156156
[react-scripts](examples/react-scripts) | A project using `react-scripts` with component tests in `src` folder
157157
[react-scripts-folder](examples/react-scripts-folder) | A project using `react-scripts` with component tests in `cypress/component`
158+
[visual-testing](examples/visual-testing) | [Visual testing](https://on.cypress.io/visual-testing) for components using 3rd party service Percy
158159
[webpack-options](examples/webpack-options) | Using the default Webpack options from `@cypress/webpack-preprocessor` to transpile JSX specs
159160
<!-- prettier-ignore-end -->
160161

circle.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ workflows:
6262
command: npm test
6363
store_artifacts: true
6464

65+
- cypress/run:
66+
name: Example Visual Testing
67+
executor: cypress/base-12
68+
requires:
69+
- Install
70+
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
71+
install-command: npm install
72+
verify-command: echo 'Already verified'
73+
no-workspace: true
74+
working_directory: examples/visual-testing
75+
# run Percy agent and then run Cypress component tests
76+
# https://docs.percy.io/docs/cypress
77+
command: npx percy exec -- npm test
78+
store_artifacts: true
79+
6580
- cypress/run:
6681
name: Test
6782
executor: cypress/base-12
@@ -95,6 +110,7 @@ workflows:
95110
- Example React Scripts
96111
- Example Component Folder
97112
- Example Webpack options
113+
- Example Visual Testing
98114
install-command: echo 'Already installed'
99115
verify-command: echo 'Already verified'
100116
no-workspace: true

examples/visual-testing/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

examples/visual-testing/.percy.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# https://docs.percy.io/docs/sdk-configuration
2+
version: 1
3+
snapshot:
4+
# use same width as cypress.json
5+
widths: [500]

examples/visual-testing/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# example: visual-testing [![This project is using Percy.io for visual regression testing.](https://percy.io/static/images/percy-badge.svg)](https://percy.io/bahmutov/cypress-react-unit-test)
2+
3+
Uses [@percy/cypress](https://github.com/percy/percy-cypress) to run visual tests for a demo of Material UI date pickers from https://material-ui.com/components/pickers/. Every pull request shows the visual diff (if any) and allows us to confirm that styles and layouts have not changed.
4+
5+
The project was set up using Percy documentation (see support and plugins files). The example spec [src/DatePicker.cy.spec.js](src/DatePicker.cy.spec.js) runs `cy.percySnapshot` after confirming the DOM has updated.
6+
7+
```js
8+
mount(<MaterialUIPickers />)
9+
// confirm the DOM has rendered the widget
10+
cy.get('#date-picker-inline').should('have.value', '08/18/2014')
11+
// then take visual snapshot
12+
cy.percySnapshot('Datepicker initial')
13+
14+
cy.get('button[aria-label="change date"]').click()
15+
// confirm the DOM has rendered the widget
16+
cy.get('.MuiPickersBasePicker-container').should('be.visible')
17+
// then take visual snapshot
18+
cy.percySnapshot('Datepicker opened')
19+
```
20+
21+
### New screenshot
22+
23+
![Percy dashboard](images/new-image.png)
24+
25+
### Visual change
26+
27+
**Tip:** you can use any [visual testing](https://on.cypress.io/visual-testing) plugin with component testing.
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": "src"
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference types="cypress" />
2+
describe('integration spec', () => {
3+
it('works', () => {
4+
expect(1).to.equal(1)
5+
})
6+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const percyHealthCheck = require('@percy/cypress/task')
2+
const preprocessor = require('../../../../plugins/react-scripts')
3+
module.exports = (on, config) => {
4+
on('task', percyHealthCheck)
5+
preprocessor(on, config)
6+
// IMPORTANT to return the config object
7+
// with the any changed environment variables
8+
return config
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require('@percy/cypress')
2+
require('cypress-react-unit-test/dist/hooks')
3+
require('@cypress/code-coverage/support')
188 KB
Loading

0 commit comments

Comments
 (0)