Skip to content

Commit 9d0671e

Browse files
authored
feat: install Cypress binary in the preBuild step (#11)
* chore: refactor postBuild * feat: install cypress binary preBuild * chore: run semantic release from beta branch * trigger build without PR on Circle * if debug is on, inherit stdio when installing Cypress binary * update README examples
1 parent 0b8da43 commit 9d0671e

File tree

5 files changed

+326
-80
lines changed

5 files changed

+326
-80
lines changed

README.md

+19-6
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,26 @@ And then add the plugin's name to the list of build plugins in `netlify.yml` fil
2323

2424
### basic
2525

26+
Here is the most basic Netlify config file `netlify.yml` in YAML format with just Cypress plugin
27+
28+
```yaml
29+
plugins:
30+
# local Cypress plugin will test our site after it is built
31+
- package: netlify-plugin-cypress
32+
```
33+
34+
and its equivalent TOML format `netlify.toml`
35+
36+
```toml
37+
[[plugins]]
38+
package = "netlify-plugin-cypress"
39+
```
40+
41+
### recommended
42+
43+
We strongly recommend setting `CYPRESS_CACHE_FOLDER` to place the Cypress binary _inside the node_modules_ folder to [cache it between builds](https://on.cypress.io/caching)
44+
2645
```yaml
27-
# Netlify config file
2846
build:
2947
command: "npm run build"
3048
publish: "build"
@@ -132,11 +150,6 @@ Set environment variable `DEBUG=netlify-plugin-cypress` to see the debug logs. T
132150
variable <code>CYPRESS_CACHE_FOLDER = "./node_modules/CypressBinary"</code>.
133151
</details>
134152

135-
<details>
136-
<summary>Cypress binary is missing</summary>
137-
If you see error messages from `cypress` NPM module <code>Error: The cypress npm package is installed, but the Cypress binary is missing.</code> add to your repository <code>package.json</code> scripts <code>"postinstall": "cypress install"</code> command. See <a href="https://github.com/cypress-io/netlify-plugin-cypress-example/blob/master/package.json">netlify-plugin-cypress-example</a> for instance.
138-
</details>
139-
140153
<details>
141154
<summary>Several versions of Cypress are installed according to the build logs</summary>
142155
From the Netlify UI under Deploys, pick "Trigger Deploy" and select "Clear cache and deploy site". This should cleanly install new "node_modules" and remove old Cypress versions.

circle.yml

+1
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ workflows:
3535
branches:
3636
only:
3737
- master
38+
- beta

index.js

+62-40
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-check
22
const ecstatic = require('ecstatic')
33
const http = require('http')
4+
const execa = require('execa')
45
const debug = require('debug')('netlify-plugin-cypress')
56
const debugVerbose = require('debug')('netlify-plugin-cypress:verbose')
67

@@ -27,71 +28,92 @@ async function runCypressTests (baseUrl, record, spec) {
2728
})
2829
}
2930

31+
async function preBuild() {
32+
debug('installing Cypress binary just in case')
33+
if (debug.enabled) {
34+
await execa('npx', ['cypress', 'install'], {stdio: 'inherit'})
35+
} else {
36+
await execa('npx', ['cypress', 'install'])
37+
}
38+
}
39+
40+
async function postBuild({ fullPublishFolder, record, spec, failBuild }) {
41+
const port = 8080
42+
const server = serveFolder(fullPublishFolder, port)
43+
debug('local server listening on port %d', port)
44+
45+
const baseUrl = `http://localhost:${port}`
46+
47+
const results = await runCypressTests(baseUrl, record, spec)
48+
49+
await new Promise((resolve, reject) => {
50+
server.close(err => {
51+
if (err) {
52+
return reject(err)
53+
}
54+
debug('closed local server on port %d', port)
55+
resolve()
56+
})
57+
})
58+
59+
// seems Cypress TS definition does not have "failures" and "message" properties
60+
if (results.failures) {
61+
// Cypress failed without even running the tests
62+
console.error('Problem running Cypress')
63+
console.error(results.message)
64+
65+
return failBuild('Problem running Cypress', {
66+
error: new Error(results.message)
67+
})
68+
}
69+
70+
debug('Cypress run results')
71+
Object.keys(results).forEach(key => {
72+
if (key.startsWith('total')) {
73+
debug('%s:', key, results[key])
74+
}
75+
})
76+
77+
// results.totalFailed gives total number of failed tests
78+
if (results.totalFailed) {
79+
return failBuild('Failed Cypress tests', {
80+
error: new Error(`${results.totalFailed} test(s) failed`)
81+
})
82+
}
83+
}
84+
3085
module.exports = function cypressPlugin (pluginConfig) {
3186
debugVerbose('cypressPlugin config %o', pluginConfig)
3287

3388
return {
3489
name: 'cypress netlify plugin',
90+
preBuild,
3591
postBuild: async (arg) => {
3692
debugVerbose('postBuild arg %o', arg)
3793

3894
const fullPublishFolder = arg.netlifyConfig.build.publish
3995
debug('folder to publish is "%s"', fullPublishFolder)
4096

41-
const port = 8080
42-
const server = serveFolder(fullPublishFolder, port)
43-
debug('local server listening on port %d', port)
44-
4597
// only if the user wants to record the tests and has set the record key
4698
// then we should attempt recording
4799
const record =
48100
typeof process.env.CYPRESS_RECORD_KEY === 'string' &&
49101
Boolean(pluginConfig.record)
50-
const baseUrl = `http://localhost:${port}`
51-
const spec = pluginConfig.spec
52-
53-
const results = await runCypressTests(baseUrl, record, spec)
54102

55-
await new Promise((resolve, reject) => {
56-
server.close(err => {
57-
if (err) {
58-
return reject(err)
59-
}
60-
debug('closed local server on port %d', port)
61-
resolve()
62-
})
63-
})
103+
const spec = pluginConfig.spec
64104

65105
const exitWithError = (message, info) => {
66106
console.error('Exit with error: %s', message)
67107
throw info.error
68108
}
69109
const failBuild = arg.utils && arg.utils.build && arg.utils.build.failBuild || exitWithError
70110

71-
// seems Cypress TS definition does not have "failures" and "message" properties
72-
if (results.failures) {
73-
// Cypress failed without even running the tests
74-
console.error('Problem running Cypress')
75-
console.error(results.message)
76-
77-
return failBuild('Problem running Cypress', {
78-
error: new Error(results.message)
79-
})
80-
}
81-
82-
debug('Cypress run results')
83-
Object.keys(results).forEach(key => {
84-
if (key.startsWith('total')) {
85-
debug('%s:', key, results[key])
86-
}
111+
return postBuild({
112+
fullPublishFolder,
113+
record,
114+
spec,
115+
failBuild
87116
})
88-
89-
// results.totalFailed gives total number of failed tests
90-
if (results.totalFailed) {
91-
return failBuild('Failed Cypress tests', {
92-
error: new Error(`${results.totalFailed} test(s) failed`)
93-
})
94-
}
95117
}
96118
}
97119
}

0 commit comments

Comments
 (0)