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

Commit 715d9fe

Browse files
authored
Check code coverage for all examples (#255)
1 parent ade6c83 commit 715d9fe

File tree

21 files changed

+88
-16
lines changed

21 files changed

+88
-16
lines changed

circle.yml

+31-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ workflows:
5454
working_directory: examples/react-scripts
5555
command: npm test
5656
store_artifacts: true
57+
post-steps:
58+
- run:
59+
name: Check coverage 📈
60+
command: |
61+
npm run check-coverage
62+
npm run only-covered
63+
working_directory: examples/react-scripts
5764

5865
- cypress/run:
5966
# react-scripts example with component tests not in "src" folder
@@ -69,6 +76,13 @@ workflows:
6976
working_directory: examples/react-scripts-folder
7077
command: npm test
7178
store_artifacts: true
79+
post-steps:
80+
- run:
81+
name: Check coverage 📈
82+
command: |
83+
npm run check-coverage
84+
npm run only-covered
85+
working_directory: examples/react-scripts-folder
7286

7387
- cypress/run:
7488
name: Example Tailwind
@@ -79,8 +93,17 @@ workflows:
7993
verify-command: echo 'Already verified'
8094
no-workspace: true
8195
working_directory: examples/tailwind
82-
command: npm test
96+
command: |
97+
DEBUG=cypress-react-unit-test,find-webpack npm test
8398
store_artifacts: true
99+
post-steps:
100+
- run:
101+
name: Check coverage 📈
102+
command: |
103+
ls -la
104+
npm run check-coverage
105+
npm run only-covered
106+
working_directory: examples/tailwind
84107

85108
- cypress/run:
86109
name: Example Webpack options
@@ -94,6 +117,13 @@ workflows:
94117
working_directory: examples/webpack-options
95118
command: npm test
96119
store_artifacts: true
120+
post-steps:
121+
- run:
122+
name: Check coverage 📈
123+
command: |
124+
npm run check-coverage
125+
npm run only-covered
126+
working_directory: examples/webpack-options
97127

98128
- cypress/run:
99129
name: Example Sass

examples/react-scripts-folder/cypress/plugins/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// load file preprocessor that comes with this plugin
22
// https://github.com/bahmutov/cypress-react-unit-test#install
3-
const preprocessor = require('../../../../plugins/react-scripts')
3+
const preprocessor = require('cypress-react-unit-test/plugins/react-scripts')
44
module.exports = (on, config) => {
55
preprocessor(on, config)
66
// IMPORTANT to return the config object

examples/react-scripts-folder/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"private": true,
55
"scripts": {
66
"test": "../../node_modules/.bin/cypress run",
7-
"cy:open": "../../node_modules/.bin/cypress open"
7+
"cy:open": "../../node_modules/.bin/cypress open",
8+
"check-coverage": "../../node_modules/.bin/check-coverage src/App.js",
9+
"only-covered": "../../node_modules/.bin/only-covered src/App.js"
810
},
911
"devDependencies": {
1012
"cypress-react-unit-test": "file:../.."

examples/react-scripts/cypress/plugins/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// load file preprocessor that comes with this plugin
22
// https://github.com/bahmutov/cypress-react-unit-test#install
3-
const preprocessor = require('../../../../plugins/react-scripts')
3+
const preprocessor = require('cypress-react-unit-test/plugins/react-scripts')
44
module.exports = (on, config) => {
55
preprocessor(on, config)
66
// IMPORTANT to return the config object

examples/react-scripts/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"private": true,
55
"scripts": {
66
"test": "../../node_modules/.bin/cypress run",
7-
"cy:open": "../../node_modules/.bin/cypress open"
7+
"cy:open": "../../node_modules/.bin/cypress open",
8+
"check-coverage": "../../node_modules/.bin/check-coverage src/App.js",
9+
"only-covered": "../../node_modules/.bin/only-covered src/App.js"
810
},
911
"devDependencies": {
1012
"cypress-react-unit-test": "file:../.."

examples/tailwind/cypress.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"experimentalComponentTesting": true,
77
"componentFolder": "src",
88
"env": {
9-
"coverage": false
9+
"coverage": true
1010
}
1111
}

examples/tailwind/cypress/plugins/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// load file preprocessor that comes with this plugin
22
// https://github.com/bahmutov/cypress-react-unit-test#install
3-
const preprocessor = require('../../../../plugins/react-scripts')
3+
const preprocessor = require('cypress-react-unit-test/plugins/react-scripts')
44
module.exports = (on, config) => {
55
preprocessor(on, config)
66
// IMPORTANT to return the config object

examples/tailwind/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
"cy:open": "../../node_modules/.bin/cypress open",
88
"build:css": "../../node_modules/.bin/tailwindcss build src/styles/tailwind.css -o src/styles/main.generated.css",
99
"pretest": "npm run build:css",
10-
"precy:open": "npm run build:css"
10+
"precy:open": "npm run build:css",
11+
"check-coverage": "../../node_modules/.bin/check-coverage src/App.js",
12+
"only-covered": "../../node_modules/.bin/only-covered src/App.js"
1113
},
1214
"devDependencies": {
1315
"cypress-react-unit-test": "file:../.."

examples/visual-sudoku/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ This React Sudoku app was cloned from [raravi/sudoku](https://github.com/raravi/
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.
8+
79
Example failing test [src/App.cy-spec.js](src/App.cy-spec.js)
810

911
![failing test](images/test.png)

examples/visual-sudoku/cypress/plugins/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// load file preprocessor that comes with this plugin
22
// https://github.com/bahmutov/cypress-react-unit-test#install
3-
const preprocessor = require('../../../../plugins/react-scripts')
3+
const preprocessor = require('cypress-react-unit-test/plugins/react-scripts')
44
const { addMatchImageSnapshotPlugin } = require('cypress-image-snapshot/plugin')
55

66
module.exports = (on, config) => {

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

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Shows how to use Applitools to visually check React charts made with [Victory](h
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.
10+
911
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.
1012

1113
![Test result](images/result.png)

examples/visual-testing-with-applitools/cypress/plugins/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// load file preprocessor that comes with this plugin
22
// https://github.com/bahmutov/cypress-react-unit-test#install
3-
const preprocessor = require('../../../../plugins/react-scripts')
3+
const preprocessor = require('cypress-react-unit-test/plugins/react-scripts')
44
module.exports = (on, config) => {
55
preprocessor(on, config)
66
// IMPORTANT to return the config object

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

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
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.
10+
911
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)
1012

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

examples/visual-testing-with-happo/cypress/plugins/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// load file preprocessor that comes with this plugin
22
// https://github.com/bahmutov/cypress-react-unit-test#install
3-
const preprocessor = require('../../../../plugins/react-scripts')
3+
const preprocessor = require('cypress-react-unit-test/plugins/react-scripts')
44
const happoTask = require('happo-cypress/task')
55

66
module.exports = (on, config) => {

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

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
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.
44

5+
Note: run `npm install` in this folder to symlink `cypress-react-unit-test` dependency.
6+
57
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.
68

79
```js

examples/visual-testing-with-percy/cypress/plugins/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// load file preprocessor that comes with this plugin
22
// https://github.com/bahmutov/cypress-react-unit-test#install
33
const percyHealthCheck = require('@percy/cypress/task')
4-
const preprocessor = require('../../../../plugins/react-scripts')
4+
const preprocessor = require('cypress-react-unit-test/plugins/react-scripts')
55
module.exports = (on, config) => {
66
on('task', percyHealthCheck)
77
preprocessor(on, config)

examples/webpack-options/cypress/component/Test.cy-spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import React from 'react'
22
import { mount } from 'cypress-react-unit-test'
3+
import Test from './Test'
34

45
describe('components', () => {
56
it('works', () => {
7+
mount(<Test />)
8+
cy.contains('Text')
9+
})
10+
11+
it('works with plain div', () => {
612
mount(<div>Text</div>)
713
cy.contains('Text')
814
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react'
2+
3+
const Test = () => <div>Text</div>
4+
5+
export default Test

examples/webpack-options/cypress/plugins/index.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,22 @@ const webpackPreprocessor = require('@cypress/webpack-preprocessor')
66
module.exports = (on, config) => {
77
// @ts-ignore
88
const opts = webpackPreprocessor.defaultOptions
9+
const babelLoader = opts.webpackOptions.module.rules[0].use[0]
10+
911
// add React preset to be able to transpile JSX
10-
opts.webpackOptions.module.rules[0].use[0].options.presets.push(
11-
require.resolve('@babel/preset-react'),
12-
)
12+
babelLoader.options.presets.push(require.resolve('@babel/preset-react'))
13+
14+
// We can also push Babel istanbul plugin to instrument the code on the fly
15+
// and get code coverage reports from component tests (optional)
16+
if (!babelLoader.options.plugins) {
17+
babelLoader.options.plugins = []
18+
}
19+
babelLoader.options.plugins.push(require.resolve('babel-plugin-istanbul'))
20+
// add code coverage plugin
21+
require('@cypress/code-coverage/task')(on, config)
22+
1323
on('file:preprocessor', webpackPreprocessor(opts))
24+
25+
// if adding code coverage, important to return updated config
26+
return config
1427
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
require('cypress-react-unit-test/dist/hooks')
2+
// if we need code coverage, need to include its custom support hook
3+
require('@cypress/code-coverage/support')

examples/webpack-options/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"private": true,
55
"scripts": {
66
"test": "../../node_modules/.bin/cypress run",
7-
"cy:open": "../../node_modules/.bin/cypress open"
7+
"cy:open": "../../node_modules/.bin/cypress open",
8+
"check-coverage": "../../node_modules/.bin/check-coverage component/Test.js",
9+
"only-covered": "../../node_modules/.bin/only-covered component/Test.js"
810
},
911
"devDependencies": {
1012
"cypress-react-unit-test": "file:../.."

0 commit comments

Comments
 (0)