Skip to content

Commit 7e81c6d

Browse files
committed
initial
0 parents  commit 7e81c6d

File tree

7 files changed

+302
-0
lines changed

7 files changed

+302
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

LICENSE.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## MIT License
2+
3+
Copyright (c) 2018 Cypress.io https://cypress.io
4+
5+
Permission is hereby granted, free of charge, to any person
6+
obtaining a copy of this software and associated documentation
7+
files (the "Software"), to deal in the Software without
8+
restriction, including without limitation the rights to use,
9+
copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the
11+
Software is furnished to do so, subject to the following
12+
conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24+
OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# netlify-plugin-cypress
2+
> Runs Cypress end-to-end tests after Netlify builds the site
3+
4+
**Note:** currently the built site is served statically and tested _without proxying redirects_.
5+
6+
## Examples
7+
8+
### basic
9+
10+
```yaml
11+
# Netlify config file
12+
build:
13+
command: "npm run build"
14+
publish: "build"
15+
environment:
16+
# do not show Cypress installation progress messages
17+
CI: 1
18+
# cache Cypress binary in local "node_modules" folder
19+
# so Netlify caches it
20+
CYPRESS_CACHE_FOLDER: "./node_modules/CypressBinary"
21+
# set TERM variable for terminal output
22+
TERM: "xterm"
23+
24+
plugins:
25+
# local Cypress plugin will test our site after it is built
26+
- package: netlify-plugin-cypress
27+
```
28+
29+
### recording
30+
31+
To record test results and artifacts on Cypress Dashboard, set `record: true` plugin input and set `CYPRESS_RECORD_KEY` as an environment variable via Netlify Deploy settings.
32+
33+
```yaml
34+
# Netlify config file
35+
build:
36+
command: "npm run build"
37+
publish: "build"
38+
environment:
39+
# do not show Cypress installation progress messages
40+
CI: 1
41+
# cache Cypress binary in local "node_modules" folder
42+
# so Netlify caches it
43+
CYPRESS_CACHE_FOLDER: "./node_modules/CypressBinary"
44+
# set TERM variable for terminal output
45+
TERM: "xterm"
46+
47+
plugins:
48+
# local Cypress plugin will test our site after it is built
49+
- package: netlify-plugin-cypress
50+
config:
51+
record: true
52+
```
53+
54+
## Debugging
55+
56+
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`
57+
58+
## Common problems
59+
60+
<details>
61+
<summary>Too many progress messages while installing Cypress</summary>
62+
If you see A LOT of progress messages during "npm install" step, set an environment
63+
variable during build <code>CI = 1</code> to remove them.
64+
</details>
65+
66+
<details>
67+
<summary>Cypress binary is installed on every build</summary>
68+
By default Cypress binary is installed in the home folder, see <a href="">caching</a>.
69+
Netlify build does NOT cache this folder, but it DOES cache the local "node_modules" folder.
70+
Tell Cypress to install its binary in the "node_modules" folder by setting build environment
71+
variable <code>CYPRESS_CACHE_FOLDER = "./node_modules/CypressBinary"</code>.
72+
</details>
73+
74+
<details>
75+
<summary>Cypress binary is missing</summary>
76+
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.
77+
</details>
78+
79+
<details>
80+
<summary>Several versions of Cypress are installed according to the build logs</summary>
81+
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.
82+
</details>
83+
84+
<details>
85+
<summary>Term message warnings in the Cypress output</summary>
86+
If you see messages like <code>tput: No value for $TERM and no -T specified</code> during
87+
Cypress run, add an environment variable <code>TERM = xterm</code>.
88+
</details>
89+
90+
## License
91+
92+
This project is licensed under the terms of the [MIT license](/LICENSE.md).

index.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// @ts-check
2+
const ecstatic = require('ecstatic')
3+
const http = require('http')
4+
const debug = require('debug')('netlify-plugin-cypress')
5+
const debugVerbose = require('debug')('netlify-plugin-cypress:verbose')
6+
7+
function serveFolder (folder, port) {
8+
const server = ecstatic({
9+
root: folder
10+
})
11+
return http.createServer(server).listen(port)
12+
}
13+
14+
async function runCypressTests (baseUrl, record) {
15+
// we will use Cypress via its NPM module API
16+
// https://on.cypress.io/module-api
17+
const cypress = require('cypress')
18+
19+
console.log('running Cypress against url %s recording?', baseUrl, record)
20+
return await cypress.run({
21+
config: {
22+
baseUrl
23+
},
24+
record
25+
})
26+
}
27+
28+
module.exports = function cypressPlugin (pluginConfig) {
29+
debugVerbose('cypressPlugin config %o', pluginConfig)
30+
31+
return {
32+
name: 'cypress netlify plugin',
33+
postBuild: async (arg) => {
34+
debugVerbose('postBuild arg %o', arg)
35+
36+
const fullPublishFolder = arg.netlifyConfig.build.publish
37+
debug('folder to publish is "%s"', fullPublishFolder)
38+
39+
const port = 8080
40+
const server = serveFolder(fullPublishFolder, port)
41+
debug('local server listening on port %d', port)
42+
43+
// only if the user wants to record the tests and has set the record key
44+
// then we should attempt recording
45+
const record =
46+
typeof process.env.CYPRESS_RECORD_KEY === 'string' &&
47+
Boolean(pluginConfig.record)
48+
const baseUrl = `http://localhost:${port}`
49+
const results = await runCypressTests(baseUrl, record)
50+
51+
await new Promise((resolve, reject) => {
52+
server.close(err => {
53+
if (err) {
54+
return reject(err)
55+
}
56+
debug('closed local server on port %d', port)
57+
resolve()
58+
})
59+
})
60+
61+
const exitWithError = (message, info) => {
62+
console.error('Exit with error: %s', message)
63+
throw info.error
64+
}
65+
const failBuild = arg.utils && arg.utils.build && arg.utils.build.failBuild || exitWithError
66+
67+
// seems Cypress TS definition does not have "failures" and "message" properties
68+
if (results.failures) {
69+
// Cypress failed without even running the tests
70+
console.error('Problem running Cypress')
71+
console.error(results.message)
72+
73+
return failBuild('Problem running Cypress', {
74+
error: new Error(results.message)
75+
})
76+
}
77+
78+
debug('Cypress run results')
79+
Object.keys(results).forEach(key => {
80+
if (key.startsWith('total')) {
81+
debug('%s:', key, results[key])
82+
}
83+
})
84+
85+
// results.totalFailed gives total number of failed tests
86+
if (results.totalFailed) {
87+
return failBuild('Failed Cypress tests', {
88+
error: new Error(`${results.totalFailed} test(s) failed`)
89+
})
90+
}
91+
}
92+
}
93+
}

manifest.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name: netlify-plugin-cypress
2+
inputs:
3+
- name: record
4+
default: false

package-lock.json

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

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "netlify-plugin-cypress",
3+
"private": true,
4+
"version": "0.0.1",
5+
"description": "Cypress Netlify build plugin",
6+
"main": "index.js",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"debug": "4.1.1",
14+
"ecstatic": "4.1.2"
15+
}
16+
}

0 commit comments

Comments
 (0)