Skip to content

Commit f2d8f76

Browse files
authored
feat: rework test phases to make onSuccess default (#133)
BREAKING CHANGE: now the onSuccess step is the default testing step, running the Cypress tests against the preview or production deploy. Another change is the default browser is Chromium instead of Electron. ```toml [[plugins]] # runs Cypress tests against the deployed URL package = "netlify-plugin-cypress" ```
1 parent 0084fa1 commit f2d8f76

27 files changed

+354
-122
lines changed

README.md

Lines changed: 70 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# netlify-plugin-cypress
22
[![CircleCI](https://circleci.com/gh/cypress-io/netlify-plugin-cypress/tree/master.svg?style=svg&circle-token=9cbb587a5a0ae4ce28b011dd03d10d66de906708)](https://circleci.com/gh/cypress-io/netlify-plugin-cypress/tree/master) [![renovate-app badge][renovate-badge]][renovate-app] [![netlify-plugin-cypress](https://img.shields.io/endpoint?url=https://dashboard.cypress.io/badge/simple/ixroqc/master&style=flat&logo=cypress)](https://dashboard.cypress.io/projects/ixroqc/runs) [![Netlify Status](https://api.netlify.com/api/v1/badges/76892baf-2ad8-4642-b283-f2135963ff51/deploy-status)](https://app.netlify.com/sites/sad-lumiere-6a00a5/deploys)
3-
> Runs Cypress end-to-end tests after Netlify builds the site but before it is deployed
4-
5-
**Note:** currently the built site is served statically and tested _without proxying redirects_.
3+
> Runs Cypress end-to-end tests on Netlify Build
64
75
## Install and use
86

@@ -26,7 +24,7 @@ This plugin installs [via Puppeteer](https://github.com/puppeteer/puppeteer) Chr
2624

2725
## How does it work
2826

29-
When Netlify Build runs, it "knows" the output folder name and calls the `netlify-plugin-cypress` after the build has finished with that folder. Then the plugin runs Cypress tests using its [NPM module API](https://on.cypress.io/module-api). If the tests pass, the plugin finishes and the Netlify deploy starts.
27+
When Netlify Build runs, it calls the plugin `netlify-plugin-cypress` before and after the build, and after the deployment. The plugin runs the Cypress tests using its [NPM module API](https://on.cypress.io/module-api) against the local folder or against the deployed URL.
3028

3129
## Examples
3230

@@ -36,7 +34,7 @@ Here is the most basic [Netlify config file](https://docs.netlify.com/configure-
3634

3735
```toml
3836
[[plugins]]
39-
# local Cypress plugin will test our site after it is built
37+
# runs Cypress tests against the deployed URL
4038
package = "netlify-plugin-cypress"
4139
```
4240

@@ -61,7 +59,7 @@ CYPRESS_CACHE_FOLDER = "./node_modules/CypressBinary"
6159
TERM = "xterm"
6260

6361
[[plugins]]
64-
# local Cypress plugin will test our site after it is built
62+
# runs Cypress tests against the deployed URL
6563
package = "netlify-plugin-cypress"
6664
```
6765

@@ -77,22 +75,15 @@ publish = "build"
7775
# ...remaining configuration...
7876
```
7977

80-
### testing deployed url
78+
### tutorial
8179

82-
After successful deployment you can run tests against the `DEPLOY_PRIME_URL` provided by the Netlify system.
80+
Read the full tutorial at [Test Sites Deployed To Netlify Using netlify-plugin-cypress](https://glebbahmutov.com/blog/test-netlify/).
8381

84-
```toml
85-
[[plugins]]
86-
package = "netlify-plugin-cypress"
87-
[plugins.inputs.onSuccess]
88-
enable = true
89-
```
90-
91-
The following parameters can be used with "onSuccess" tests: `record`, `group`, `tag`, `spec`.
82+
**Note:** if any tests against the deployed URL fail, the Netlify build still considers it a success. Thus if you want to have a test check against the deploy, install [Cypress GitHub App](https://on.cypress.io/github-integration). The app will provide its own failing status check in this case.
9283

93-
Read the full tutorial [Test Sites Deployed To Netlify Using netlify-plugin-cypress](https://glebbahmutov.com/blog/test-netlify/).
84+
### options
9485

95-
**Note:** if any tests against the deployed URL fail, the Netlify build still considers it a success. Thus if you want to have a test check against the deploy, install [Cypress GitHub App](https://on.cypress.io/github-integration). The app will provide its own failing status check in this case.
86+
You can control the browser, the specs to run, record tests on Cypress Dashboard, etc, see [manifest.yml](./manifest.yml) file.
9687

9788
### recording
9889

@@ -110,7 +101,7 @@ publish = "build"
110101
TERM = "xterm"
111102

112103
[[plugins]]
113-
# local Cypress plugin will test our site after it is built
104+
# runs Cypress tests against the deployed URL
114105
package = "netlify-plugin-cypress"
115106
[plugins.inputs]
116107
record = true
@@ -132,7 +123,7 @@ You can change the group name for the recorded run using `group` parameter
132123

133124
```toml
134125
[[plugins]]
135-
# local Cypress plugin will test our site after it is built
126+
# runs Cypress tests against the deployed URL
136127
package = "netlify-plugin-cypress"
137128
[plugins.inputs]
138129
record = true
@@ -145,7 +136,7 @@ You can give recorded run [tags](https://on.cypress.io/module-api#cypress-run) u
145136

146137
```toml
147138
[[plugins]]
148-
# local Cypress plugin will test our site after it is built
139+
# runs Cypress tests against the deployed URL
149140
package = "netlify-plugin-cypress"
150141
[plugins.inputs]
151142
record = true
@@ -169,17 +160,17 @@ publish = "build"
169160
TERM = "xterm"
170161

171162
[[plugins]]
172-
# local Cypress plugin will test our site after it is built
163+
# runs Cypress tests against the deployed URL
173164
package = "netlify-plugin-cypress"
174165
[plugins.inputs]
175166
spec = "cypress/integration/smoke*.js"
176167
```
177168

178169
See [cypress-example-kitchensink](https://github.com/cypress-io/cypress-example-kitchensink) for instance.
179170

180-
### Chromium
171+
### browser
181172

182-
By default all tests run using built-in Electron browser. If you want to use Chromium:
173+
By default all tests run using the Chromium browser. If you want to use Electron:
183174

184175
```toml
185176
[build]
@@ -195,12 +186,13 @@ publish = "build"
195186
[[plugins]]
196187
package = "netlify-plugin-cypress"
197188
[plugins.inputs]
198-
browser = "chromium"
189+
# allowed values: electron, chromium
190+
browser = "electron"
199191
```
200192

201193
### testing SPA routes
202194

203-
SPAs need catch-all redirect setup to make non-root paths accesssible by tests. You can enable this with `spa` parameter.
195+
SPAs need catch-all redirect setup to make non-root paths accessible by tests. You can enable this with `spa` parameter.
204196

205197
```
206198
[[plugins]]
@@ -215,7 +207,7 @@ See [lws-spa](https://github.com/lwsjs/spa) for more options and [tests/routing]
215207

216208
### testing the site before build
217209

218-
By default this plugin tests static site _after build_. But maybe you want to run end-to-end tests against the _local development server_. You can start local server, wait for it to respond and then run Cypress tests by passing parameters to this plugin. Here is a sample config file
210+
By default this plugin tests static site _after deploy_. But maybe you want to run end-to-end tests against the _local development server_. You can start the local server, wait for it to respond and then run Cypress tests by passing parameters to this plugin. Here is a sample config file
219211

220212
```toml
221213
[[plugins]]
@@ -228,10 +220,42 @@ By default this plugin tests static site _after build_. But maybe you want to ru
228220
wait-on-timeout = '30' # seconds
229221
```
230222

231-
Parameters you can place into `preBuild` inputs: `start`, `wait-on`, `wait-on-timeout`, `spec`, `record`, `group`, and `tag`. If there is `preBuild` and `postBuild` testing with different tags, the first one wins :)
223+
Parameters you can place into `preBuild` inputs: `start`, `wait-on`, `wait-on-timeout`, `spec`, `record`, `group`, and `tag`.
232224

233225
See [netlify-plugin-prebuild-example](https://github.com/cypress-io/netlify-plugin-prebuild-example) repo
234226

227+
### testing the site after build
228+
229+
By default this plugin tests static site _after deploy_. But maybe you want to run end-to-end tests locally after building the static site. Cypress includes a local static server for this case. Here is a sample config file
230+
231+
```toml
232+
[[plugins]]
233+
package = "netlify-plugin-cypress"
234+
# let's run tests against the built site
235+
[plugins.inputs.postBuild]
236+
enable = true
237+
```
238+
239+
Parameters you can place into `postBuild` inputs: `spec`, `record`, `group`, `tag`, and `spa`.
240+
241+
#### The SPA parameter
242+
243+
If your site requires all unknown URLs to redirect back to the index page, use the `spa` parameter
244+
245+
```toml
246+
[[plugins]]
247+
package = "netlify-plugin-cypress"
248+
# let's run tests against the built site
249+
[plugins.inputs.postBuild]
250+
enable = true
251+
# must allow our test server to redirect unknown routes to "/"
252+
# so that client-side routing can correctly route them
253+
# can be set to true or "index.html" (or similar fallback filename in the built folder)
254+
spa = true
255+
```
256+
257+
See [the routing example](./tests/routing/netlify.toml).
258+
235259
### using Netlify CLI
236260

237261
Even better when testing the prebuilt site is to run the [Netlify CLI](https://cli.netlify.com/) to make sure the local API redirects and Netlify functions work in addition to the web site. Add `netlify-cli` as a dev dependency and start it during testing.
@@ -253,13 +277,17 @@ For more, see [tests/test-netlify-dev](./tests/test-netlify-dev) example and rea
253277

254278
### skipping tests
255279

256-
If you are testing the site before building it, you probably want to skip testing it after the build. See [tests/test-prebuild-only](./tests/test-prebuild-only/netlify.toml):
280+
If you are testing the site before building it and want to skip testing the deployed URL
257281

258282
```toml
259283
[[plugins]]
260284
package = "netlify-plugin-cypress"
285+
# do not test the deployed URL
261286
[plugins.inputs]
262-
skip = true
287+
enable = false
288+
# test the local site
289+
[plugins.inputs.preBuild]
290+
enable = true
263291
```
264292

265293
### parallelization
@@ -294,6 +322,13 @@ Name | Description
294322
[bahmutov/eleventyone](https://github.com/bahmutov/eleventyone) | Example used in [Test Sites Deployed To Netlify Using netlify-plugin-cypress](https://glebbahmutov.com/blog/test-netlify/) tutorial
295323
[gatsby-starter-portfolio-cara](https://github.com/bahmutov/gatsby-starter-portfolio-cara) | A Gatsby site example
296324

325+
## Major upgrades
326+
327+
### v1 to v2
328+
329+
- The default browser has been switched to Chromium. If you want to use the built-in Electron use an explicit option [browser](#browser)
330+
- We have changed the default testing phase. In v1 the tests executed after building the site by default. In v2 the tests run against the deployed URL by default, and you need to enable the testing during `preBuild` or `postBuild` steps.
331+
297332
## Debugging
298333

299334
Set environment variable `DEBUG=netlify-plugin-cypress` to see the debug logs. To see even more information, set `DEBUG=netlify-plugin-cypress,netlify-plugin-cypress:verbose`
@@ -332,6 +367,11 @@ Set environment variable `DEBUG=netlify-plugin-cypress` to see the debug logs. T
332367
Switch to using Chromium browser that seems to be a bit more reliable. Use <code>browser = "chromium"</code> setting.
333368
</details>
334369

370+
<details>
371+
<summary>You want to skip Puppeteer download</summary>
372+
If you do not plan on using Chromium to run the tests, if you want to use the built-in Electron browser, you can save time by skipping the Puppeteer download. Set the environment variable <code>PUPPETEER_SKIP_DOWNLOAD = 1</code> on your CI.
373+
</details>
374+
335375
## License
336376

337377
This project is licensed under the terms of the [MIT license](LICENSE.md).

circle.yml

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,25 @@ jobs:
1717
executor:
1818
name: node/default
1919
tag: '12'
20+
environment:
21+
# since we do not need Cypress to publish the NPM package
22+
# we can skip the binary download
23+
CYPRESS_INSTALL_BINARY: 0
2024
steps:
2125
- checkout
2226
- node/with-cache:
2327
steps:
2428
- run: npm ci
25-
- run: npx semantic-release
29+
# allow CircleCI to release beta versions
30+
# from pull request build jobs
31+
- run:
32+
name: Semantic release 🚀
33+
command: npx semantic-release
34+
# by tricking Circle and removing the environment variables
35+
environment:
36+
CIRCLE_PR_NUMBER:
37+
CIRCLE_PULL_REQUEST:
38+
CI_PULL_REQUEST:
2639

2740
'basic test':
2841
executor: cypress/base-12-14-0
@@ -84,7 +97,7 @@ jobs:
8497
at: ~/
8598
- run:
8699
name: Netlify Build 🏗
87-
command: npx netlify build
100+
command: npm run netlify:build
88101
working_directory: tests/test-prebuild-only
89102
environment:
90103
DEBUG: netlify-plugin-cypress
@@ -110,7 +123,7 @@ jobs:
110123
at: ~/
111124
- run:
112125
name: Netlify Build 🏗
113-
command: npx netlify build
126+
command: npm run netlify:build
114127
working_directory: tests/test-netlify-dev
115128
environment:
116129
DEBUG: netlify-plugin-cypress
@@ -175,19 +188,17 @@ workflows:
175188
requires:
176189
- cypress/install
177190
- release:
191+
# run the release job on all branches
192+
# since we might want to release a beta version
178193
requires:
179194
- build
180-
- 'basic test'
181-
- 'html-pages'
182-
- 'recommended test'
183-
- 'recording test'
184-
- 'test-twice'
185-
- 'test-prebuild-only'
186-
- test-using-chromium
187-
- test-netlify-dev
188-
- 'routing'
189-
filters:
190-
branches:
191-
only:
192-
- master
193-
- beta
195+
# temporary while publishing pre-release
196+
# - 'basic test'
197+
# - 'html-pages'
198+
# - 'recommended test'
199+
# - 'recording test'
200+
# - 'test-twice'
201+
# - 'test-prebuild-only'
202+
# - test-using-chromium
203+
# - test-netlify-dev
204+
# - 'routing'

manifest.yml

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,45 @@
11
name: netlify-plugin-cypress
22
inputs:
3-
# these settings apply during postBuild step
4-
# when we are testing the site served from the distribution folder
3+
# by default the Cypress tests run against the deployed URL
4+
# and these settings apply during the "onSuccess" step
5+
- name: enable
6+
description: Run tests against the preview or production deploy
7+
default: true
8+
9+
# Cypress comes with built-in Electron browser
10+
# and this NPM package installs Chromium browser
11+
- name: browser
12+
description: Allowed values are chromium, electron
13+
default: chromium
14+
515
- name: record
16+
description: Record test results to Cypress Dashboard
617
default: false
18+
719
- name: spec
20+
description: |
21+
Run just the given spec or spec pattern,
22+
equivalent to "cypress run --spec ..."
23+
824
- name: group
25+
description: |
26+
If recording to Cypress Dashboard,
27+
pass the group name with "cypress run --record --group ..."
28+
929
- name: tag
10-
- name: spa
11-
# by default run the tests
12-
- name: skip
13-
default: false
14-
# by default the tests run in Electron
15-
# but because of the dependency we download Chromium
16-
# so you can set "browser = electron"
17-
- name: browser
18-
default: electron
30+
description: |
31+
If recording to Cypress Dashboard,
32+
pass the tag with "cypress run --record --tag ..."
1933
2034
# tells the plugin how to start the server using custom command
2135
# and waiting for an url, record to the dashboard, tag, etc
2236
# see README "testing the site before build"
2337
- name: preBuild
38+
description: Run tests before building the site
2439

25-
# you can control how the plugin runs the tests after deploy
26-
- name: onSuccess
40+
# tells the plugin to start a static server during postBuild
41+
# and test just the built static site.
42+
# see README "testing the site after build"
43+
# NOTE: does not execute Netlify API redirects or functions
44+
- name: postBuild
45+
description: Run tests against the built static site

netlify.toml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ publish = "public"
1010
[[plugins]]
1111
# local Cypress plugin will test our site after it is built
1212
package = "."
13+
14+
# run tests after deploying to Netlify
1315
[plugins.inputs]
14-
# browser = "electron"
15-
# spec = 'cypress/integration/spec.js'
16-
# [plugins.inputs.preBuild]
17-
# start = 'npm start'
18-
# wait-on = 'http://localhost:5000'
19-
# wait-on-timeout = '3' # seconds
20-
[plugins.inputs.onSuccess]
21-
enable = true
2216
record = true
2317
group = 'deployed'
18+
19+
# run tests after building the site
20+
[plugins.inputs.postBuild]
21+
enable = true
22+
record = true
23+
group = 'postBuild'
24+

package-lock.json

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)