Skip to content

Commit 3b1fd82

Browse files
committed
update code to make the routing test pass
1 parent 2afe844 commit 3b1fd82

File tree

7 files changed

+83
-54
lines changed

7 files changed

+83
-54
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,12 @@ Set environment variable `DEBUG=netlify-plugin-cypress` to see the debug logs. T
339339
Switch to using Chromium browser that seems to be a bit more reliable. Use <code>browser = "chromium"</code> setting.
340340
</details>
341341

342+
## Changelog
343+
344+
### v1 to v2
345+
346+
- 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.
347+
342348
## License
343349

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

manifest.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
name: netlify-plugin-cypress
22
inputs:
3-
# these settings apply during onSuccess step
3+
# by default the Cypress tests run against the deployed URL
4+
# and these settings apply during the "onSuccess" step
45
- name: enable
56
description: Run tests against the preview or production deploy
67
default: true
78

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+
815
- name: record
916
description: Record test results to Cypress Dashboard
1017
default: false
@@ -24,12 +31,6 @@ inputs:
2431
If recording to Cypress Dashboard,
2532
pass the tag with "cypress run --record --tag ..."
2633
27-
# Cypress comes with built-in Electron browser
28-
# and this NPM package installs Chromium browser
29-
- name: browser
30-
description: Allowed values are chromium, electron
31-
default: chromium
32-
3334
# tells the plugin how to start the server using custom command
3435
# and waiting for an url, record to the dashboard, tag, etc
3536
# see README "testing the site before build"

package-lock.json

Lines changed: 9 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"debug": "4.1.1",
2525
"got": "10.7.0",
2626
"local-web-server": "^4.2.1",
27-
"puppeteer": "^7.0.1"
27+
"puppeteer": "^7.0.1",
28+
"ramda": "0.27.1"
2829
},
2930
"repository": {
3031
"type": "git",

src/index.js

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @ts-check
22
const { stripIndent } = require('common-tags')
3+
const R = require('ramda')
34
const debug = require('debug')('netlify-plugin-cypress')
45
const debugVerbose = require('debug')('netlify-plugin-cypress:verbose')
56
const { ping, getBrowserPath, serveFolder } = require('./utils')
@@ -278,21 +279,26 @@ const hasRecordKey = () => typeof process.env.CYPRESS_RECORD_KEY === 'string'
278279

279280
module.exports = {
280281
onPreBuild: async (arg) => {
282+
// we need to install everything to be ready
281283
await install(arg)
282284
await cypressVerify(arg)
283285
await cypressInfo(arg)
284286

285-
debug('cypress plugin preBuild inputs %o', arg.inputs)
286-
const preBuildInputs = arg.inputs && arg.inputs.preBuild
287-
if (!preBuildInputs) {
288-
debug('there are no preBuild inputs')
287+
const { inputs, utils } = arg
288+
289+
const preBuildInputs = inputs.preBuild || {}
290+
debug('preBuild inputs %o', preBuildInputs)
291+
292+
const enablePreBuildTests = Boolean(preBuildInputs.enable)
293+
if (!enablePreBuildTests) {
294+
debug('Skipping preBuild tests')
289295
return
290296
}
291297

292-
const browser = arg.inputs.browser || DEFAULT_BROWSER
298+
const browser = preBuildInputs.browser || DEFAULT_BROWSER
293299

294-
const closeServer = startServerMaybe(arg.utils.run, preBuildInputs)
295-
await waitOnMaybe(arg.utils.build, preBuildInputs)
300+
const closeServer = startServerMaybe(utils.run, preBuildInputs)
301+
await waitOnMaybe(utils.build, preBuildInputs)
296302

297303
const baseUrl = preBuildInputs['wait-on']
298304
const record = hasRecordKey() && Boolean(preBuildInputs.record)
@@ -323,47 +329,49 @@ module.exports = {
323329
closeServer()
324330
}
325331

326-
const errorCallback = arg.utils.build.failBuild.bind(arg.utils.build)
327-
const summaryCallback = arg.utils.status.show.bind(arg.utils.status)
332+
const errorCallback = utils.build.failBuild.bind(utils.build)
333+
const summaryCallback = utils.status.show.bind(utils.status)
328334

329335
processCypressResults(results, errorCallback, summaryCallback)
330336
},
331337

332-
onPostBuild: async (arg) => {
333-
debugVerbose('postBuild arg %o', arg)
334-
debug('cypress plugin postBuild inputs %o', arg.inputs)
338+
onPostBuild: async ({ inputs, constants, utils }) => {
339+
debugVerbose('===postBuild===')
335340

336-
const skipTests = Boolean(arg.inputs.skip)
337-
if (skipTests) {
338-
console.log('Skipping tests because skip=true')
341+
const postBuildInputs = inputs.postBuild || {}
342+
debug('cypress plugin postBuild inputs %o', postBuildInputs)
343+
344+
const enablePostBuildTests = Boolean(postBuildInputs.enable)
345+
if (!enablePostBuildTests) {
346+
debug('Skipping postBuild tests')
339347
return
340348
}
341349

342-
const fullPublishFolder = arg.constants.PUBLISH_DIR
350+
const fullPublishFolder = constants.PUBLISH_DIR
343351
debug('folder to publish is "%s"', fullPublishFolder)
344352

345-
const browser = arg.inputs.browser || DEFAULT_BROWSER
353+
const browser = postBuildInputs.browser || DEFAULT_BROWSER
346354

347355
// only if the user wants to record the tests and has set the record key
348356
// then we should attempt recording
349-
const record = hasRecordKey() && Boolean(arg.inputs.record)
357+
const record = hasRecordKey() && Boolean(postBuildInputs.record)
350358

351-
const spec = arg.inputs.spec
359+
const spec = postBuildInputs.spec
352360
let group
353361
let tag
354362
if (record) {
355-
group = arg.inputs.group || 'postBuild'
363+
group = postBuildInputs.group || 'postBuild'
356364

357-
if (arg.inputs.tag) {
358-
tag = arg.inputs.tag
365+
if (postBuildInputs.tag) {
366+
tag = postBuildInputs.tag
359367
} else {
360368
tag = process.env.CONTEXT
361369
}
362370
}
363-
const spa = arg.inputs.spa
371+
const spa = postBuildInputs.spa
364372

365-
const errorCallback = arg.utils.build.failBuild.bind(arg.utils.build)
366-
const summaryCallback = arg.utils.status.show.bind(arg.utils.status)
373+
const errorCallback = utils.build.failBuild.bind(utils.build)
374+
const summaryCallback = utils.status.show.bind(utils.status)
367375

368376
await postBuild({
369377
fullPublishFolder,
@@ -382,11 +390,12 @@ module.exports = {
382390
* Executes after successful Netlify deployment.
383391
* @param {any} arg
384392
*/
385-
onSuccess: async (arg) => {
386-
debugVerbose('onSuccess arg %o', arg)
393+
onSuccess: async ({ utils, inputs, constants }) => {
394+
debugVerbose('onSuccess arg %o', { utils, inputs, constants })
387395

388-
const { utils, inputs, constants } = arg
389-
debug('onSuccess inputs %o', inputs)
396+
// extract test run parameters
397+
const onSuccessInputs = R.omit(['preBuild', 'postBuild'], inputs || {})
398+
debug('onSuccess inputs %o', onSuccessInputs)
390399

391400
const isLocal = constants.IS_LOCAL
392401
const siteName = process.env.SITE_NAME
@@ -397,16 +406,9 @@ module.exports = {
397406
isLocal,
398407
})
399408

400-
// extract test run parameters
401-
const onSuccessInputs = inputs.onSuccess
402-
if (!onSuccessInputs) {
403-
debug('no onSuccess inputs, skipping testing the deployed url')
404-
return
405-
}
406-
407-
const enableTests = Boolean(onSuccessInputs.enable)
408-
if (!enableTests) {
409-
console.log('Skipping tests because enable=false')
409+
const enableOnSuccessTests = Boolean(onSuccessInputs.enable)
410+
if (!enableOnSuccessTests) {
411+
debug('Skipping onSuccess tests')
410412
return
411413
}
412414

@@ -419,7 +421,7 @@ module.exports = {
419421
return errorCallback('Missing DEPLOY_PRIME_URL')
420422
}
421423

422-
const browser = arg.inputs.browser || DEFAULT_BROWSER
424+
const browser = onSuccessInputs.browser || DEFAULT_BROWSER
423425

424426
// only if the user wants to record the tests and has set the record key
425427
// then we should attempt recording

tests/routing/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ This example tests the redirect fallback that makes client-side routing defined
55

66
![Test screenshot](images/routing.png)
77

8+
These tests run:
9+
- [x] before the build
10+
- [x] after the build
11+
- [ ] on deploy success
12+
813
## Local build
914

10-
Use command `npm run netlify:build` to try the plugin locally
15+
Use command `DEBUG=netlify-plugin-cypress npm run netlify:build` to try the plugin locally

tests/routing/netlify.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ publish = "build"
88
CYPRESS_CACHE_FOLDER = "./node_modules/CypressBinary"
99
# set TERM variable for terminal output
1010
TERM = "xterm"
11+
# do not print too many progress messages
12+
CI = "1"
1113

1214
[[plugins]]
1315
# local Cypress plugin will test our site after it is built
@@ -17,15 +19,22 @@ TERM = "xterm"
1719
# run Cypress tests once on the site before it is built
1820
# and then after building the static folder
1921

22+
[plugins.inputs]
23+
# these settings apply to the default step onSuccess
24+
# in this case, we do not want to run any tests after deploy
25+
enable = false
26+
2027
# let's run tests against development server
2128
# before building it (and testing the built site)
2229
[plugins.inputs.preBuild]
30+
enable = true
2331
start = 'npm start'
2432
wait-on = 'http://localhost:3000'
2533
wait-on-timeout = '45' # seconds
2634

2735
# and test the built site after
28-
[plugins.inputs]
36+
[plugins.inputs.postBuild]
37+
enable = true
2938
# must allow our test server to redirect unknown routes to "/"
3039
# so that client-side routing can correctly route them
3140
# can be set to true or "index.html" (or similar fallback filename in the built folder)

0 commit comments

Comments
 (0)