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

Commit 283f8c4

Browse files
authored
Add example using react-app-rewired (#512)
1 parent d65d2fb commit 283f8c4

32 files changed

+644
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ Folder Name | Description
240240
[visual-testing-with-percy](examples/visual-testing-with-percy) | [Visual testing](#visual-testing) for components using 3rd party service [Percy.io](https://percy.io/)
241241
[webpack-file](examples/webpack-file) | Load existing `webpack.config.js` file
242242
[webpack-options](examples/webpack-options) | Using the default Webpack options from `@cypress/webpack-preprocessor` to transpile JSX specs
243+
[rewired](examples/rewired) | Component tests for apps that use [react-app-rewired](https://github.com/timarney/react-app-rewired)
243244
<!-- prettier-ignore-end -->
244245

245246
### External examples

circle.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,27 @@ workflows:
140140
npm run only-covered
141141
working_directory: examples/react-scripts
142142

143+
- cypress/run:
144+
name: Example React App Rewired
145+
requires:
146+
- Component Tests
147+
executor: cypress/base-12
148+
install-command: |
149+
ls -la ../..
150+
echo ***Installing cypress-react-unit-test from root TGZ archive***
151+
npm install -D ../../cypress-react-unit-test-0.0.0-development.tgz
152+
echo ***Installing other dependencies***
153+
npm install
154+
echo ***rename root node_modules to avoid accidental dependencies***
155+
mv ../../node_modules ../../no_modules
156+
echo ***React version***
157+
npm ls react react-dom
158+
verify-command: echo 'Already verified'
159+
no-workspace: true
160+
working_directory: examples/rewired
161+
command: npm test
162+
store_artifacts: true
163+
143164
- cypress/run:
144165
name: Example Next.js
145166
requires:
@@ -506,6 +527,7 @@ workflows:
506527
- Example Component Folder
507528
- Example Cucumber
508529
- Example React Scripts
530+
- Example React App Rewired
509531
- Example Sass
510532
- Example Snapshots
511533
- Example Tailwind

examples/rewired/.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# do not complain about babel-loader in this folder
2+
# and in the root folder
3+
SKIP_PREFLIGHT_CHECK=true

examples/rewired/.npmrc

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

examples/rewired/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# example: react-app-rewired
2+
3+
Component tests for apps that use [react-app-rewired](https://github.com/timarney/react-app-rewired)
4+
5+
Main idea: find and load Webpack options using [find-webpack](https://github.com/bahmutov/find-webpack) module, then pass it through the [config-overrides.js](config-overrides.js) file. Then give it to the Cypress Webpack preprocessor. See [cypress/plugins/index.js](cypress/plugins/index.js) for details.
6+
7+
TODO: go through the rest of the spec files in [src](src) folder to make sure they work.
8+
TODO: validate the code coverage
9+
10+
## Usage
11+
12+
1. Make sure the root project has been built .
13+
14+
```bash
15+
# in the root of the project
16+
npm install
17+
npm run build
18+
```
19+
20+
2. Run `npm install` in this folder to symlink the `cypress-react-unit-test` dependency.
21+
22+
```bash
23+
# in this folder
24+
npm install
25+
```
26+
27+
3. Start Cypress
28+
29+
```bash
30+
npm run cy:open
31+
# or just run headless tests
32+
npm test
33+
```
34+
35+
## Debugging
36+
37+
You can see the Webpack config by running Cypress with `DEBUG=overrides` environment variable.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// to see these debug messages, run the program with
2+
// DEBUG=overrides
3+
const debug = require('debug')('overrides')
4+
5+
module.exports = function override(config, env) {
6+
debug('in config overrides, env "%s"', env)
7+
debug('config %o', config)
8+
9+
// throw an error to stop the app
10+
// useful for debugging to avoid the dev server
11+
// from hiding the console output
12+
// throw new Error('stop here')
13+
14+
//do stuff with the webpack config...
15+
16+
return config
17+
}

examples/rewired/cypress.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"testFiles": "*.spec.js",
3+
"viewportWidth": 500,
4+
"viewportHeight": 800,
5+
"experimentalComponentTesting": true,
6+
"componentFolder": "src"
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "Using fixtures to represent data",
3+
"email": "hello@cypress.io",
4+
"body": "Fixtures are a great way to mock data for responses to routes"
5+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// load file preprocessor that comes with this plugin
2+
// https://github.com/bahmutov/cypress-react-unit-test#install
3+
const debug = require('debug')('overrides')
4+
const fw = require('find-webpack')
5+
6+
const env = (process.env.NODE_ENV = process.env.NODE_ENV || 'development')
7+
8+
const webpackPreprocessor = require('@cypress/webpack-preprocessor')
9+
10+
module.exports = (on, config) => {
11+
// require('@cypress/code-coverage/task')(on, config)
12+
13+
const foundWebpackOptions = fw.getWebpackOptions()
14+
const configOverrides = require('react-app-rewired/config-overrides')
15+
const webpackOptions = configOverrides.webpack(foundWebpackOptions, env)
16+
17+
debug('webpackOptions %o', webpackOptions)
18+
fw.cleanForCypress(
19+
{
20+
reactScripts: true,
21+
looseModules: true,
22+
},
23+
webpackOptions,
24+
)
25+
26+
on('file:preprocessor', webpackPreprocessor({ webpackOptions }))
27+
28+
// IMPORTANT to return the config object
29+
// with the any changed environment variables
30+
return config
31+
}
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')

0 commit comments

Comments
 (0)