Skip to content
This repository was archived by the owner on Oct 1, 2020. It is now read-only.

Commit 2d29c87

Browse files
authored
Test react scripts (#74)
* add example React app * delete webpack optimization * use module to clean options * fix lint * disable video in e2e test, update snapshot * split workflow into 3 jobs * add react app test job * add no-op install * add public folder to Git * add README to react-app example * link example from root readme * add timeout, fix warning * remove wait-on * do not use Node 12 executor for the app * try to use default executor for install * hmm back to Node 12 * try using react-scripts 3.2 * ignore build folder * add example of working dynamic import
1 parent a6baf1f commit 2d29c87

File tree

25 files changed

+7108
-245
lines changed

25 files changed

+7108
-245
lines changed

.eslintignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
test/_test-output
22
cypress/tests/e2e/compile-error.js
33
test/fixtures
4+
examples/react-app/src
5+
examples/react-app/node_modules
6+
examples/react-app/build
7+
examples/react-app/package.json
8+
examples/react-app/cypress/integration/dynamic-import-spec.js
49
*.ts

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_modules/
44
_test-output
55
cypress/videos
66
cypress/screenshots
7+
examples/react-app/build/

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@ This plugin (and all Cypress plugins) run in Cypress's own version of Node. If y
3535
In your project's [plugins file](https://on.cypress.io/guides/tooling/plugins-guide.html):
3636

3737
```javascript
38-
const webpack = require('@cypress/webpack-preprocessor')
38+
const webpackPreprocessor = require('@cypress/webpack-preprocessor')
3939

4040
module.exports = (on) => {
41-
on('file:preprocessor', webpack())
41+
on('file:preprocessor', webpackPreprocessor())
4242
}
4343
```
4444

45+
## Examples
46+
47+
- [React app](examples/react-app)
48+
4549
## Options
4650

4751
Pass in options as the second argument to `webpack`:

__snapshots__/e2e.spec.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@ SyntaxError: /[cwd]/cypress/tests/e2e/compile-error.js: Unexpected token, expect
3333
| ^
3434
15 | })
3535
16 |
36-
3736
@ multi ./cypress/tests/e2e/compile-error.js main[0]
3837
39-
4038
This occurred while Cypress was compiling and bundling your test code. This is usually caused by:
4139
4240
- A missing file or dependency
@@ -53,18 +51,12 @@ Fix the error in your code and re-run your tests.
5351
│ Pending: 0 │
5452
│ Skipped: 0 │
5553
│ Screenshots: 0 │
56-
│ Video: true
54+
│ Video: false
5755
│ Duration: X seconds │
5856
│ Spec Ran: e2e/compile-error.js │
5957
└────────────────────────────────────────────────────────────────────────────────────────────────┘
6058
6159
62-
(Video)
63-
64-
- Started processing: Compressing to 32 CRF
65-
- Finished processing: /[cwd]/cypress/videos/e2e/compile-error.js.mp4 (X second)
66-
67-
6860
====================================================================================================
6961
7062
(Run Finished)

circle.yml

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
1+
# based on https://github.com/cypress-io/circleci-orb/blob/master/docs/recipes.md
12
version: 2.1
23
orbs:
34
cypress: cypress-io/cypress@1
45
workflows:
56
build:
67
jobs:
7-
- cypress/run:
8+
# first, run a single job to install dependencies and Cypress
9+
# and perform a few more small steps like linting
10+
- cypress/install:
11+
name: Install
812
executor: cypress/base-12-14-0
913
yarn: true
14+
post-steps:
15+
- run:
16+
name: Show info 📺
17+
command: npx cypress info
18+
19+
- cypress/run:
20+
name: Tests
21+
requires:
22+
- Install
23+
executor: cypress/base-12-14-0
24+
install-command: echo 'Nothing else to install in this job'
1025
command: npm test
26+
no-workspace: true
1127
post-steps:
1228
- run:
1329
name: Lint code 🧹
@@ -18,6 +34,29 @@ workflows:
1834
- run:
1935
name: Run tests w/ webpack@latest 🧪
2036
command: npm test
21-
- run:
22-
name: Semantic release 🚀
23-
command: npm run semantic-release
37+
38+
- cypress/run:
39+
name: Test React App
40+
requires:
41+
- Install
42+
executor: cypress/base-12-14-0
43+
install-command: echo 'Nothing else to install in this job'
44+
timeout: '1m'
45+
start: npm start
46+
no-workspace: true
47+
working_directory: examples/react-app
48+
49+
# wait for all jobs to finish and possible run NPM release
50+
- cypress/run:
51+
name: NPM release
52+
requires:
53+
- Install
54+
- Tests
55+
- Test React App
56+
executor: cypress/base-12-14-0
57+
# nothing to install - cypress/install job does it
58+
# and nothing to pass to the next job
59+
install-command: echo 'Nothing else to install in this job'
60+
no-workspace: true
61+
# instead of "cypress run" do NPM release 😁
62+
command: npm run semantic-release

examples/react-app/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SKIP_PREFLIGHT_CHECK=true
2+
BROWSER=none

examples/react-app/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
This is an example React application clone from [codesandbox.io](https://codesandbox.io/s/oj3qm2zq06) and blog post [How To Build a React To-Do App with React Hooks](https://www.digitalocean.com/community/tutorials/how-to-build-a-react-to-do-app-with-react-hooks).
2+
3+
Because the application is using `react-scripts`, we can point our Cypress Webpack preprocessor at the Webpack config included with `react-scripts` module. See [cypress/plugins/index.js](cypress/plugins/index.js) file.
4+
5+
**Tip:** you can use [find-webpack](https://github.com/bahmutov/find-webpack) utility to find the Webpack config options provided by common tools: `react-scripts`, Vue CLI.
6+
7+
## Chunks and dynamic imports
8+
9+
Normally, a code that uses [dynamic imports](https://github.com/tc39/proposal-dynamic-import) is bundled by Webpack into a separate JavaScript bundle (chunk) to be loaded on demand. Cypress needs all chunks to be in the same bundled file, since it cannot be serving additional files, besides the spec itself. Thus we need to force all chunks into the single bundle by changing the Webpack plugins' options.
10+
11+
See [cypress/integration/dynamic-import-spec.js](cypress/integration/dynamic-import-spec.js) and the [cypress/plugins/index.js](cypress/plugins/index.js) file - it uses [find-webpack cleanForCypress](https://github.com/bahmutov/find-webpack) utility to force dynamic imports into the same spec bundle.

examples/react-app/cypress.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"baseUrl": "http://localhost:3000",
3+
"fixturesFolder": false,
4+
"supportFile": false,
5+
"viewportWidth": 600
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"env": {
3+
"cypress/globals": true
4+
},
5+
"extends": [
6+
"eslint:recommended",
7+
"plugin:cypress/recommended"
8+
],
9+
"plugins": ["cypress"]
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference types="cypress" />
2+
describe('Dynamic import', () => {
3+
it('works', () => {
4+
// dynamically import module
5+
// and then invoke an exported method "reverse"
6+
cy.wrap(import('../utils'))
7+
.invoke('reverse', 'Hello').should('equal', 'olleH')
8+
})
9+
})

0 commit comments

Comments
 (0)