Skip to content

Commit de49353

Browse files
authored
add note about Netlify CLI and an example (#112)
1 parent 79c8b5d commit de49353

File tree

7 files changed

+103
-0
lines changed

7 files changed

+103
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,25 @@ Parameters you can place into `preBuild` inputs: `start`, `wait-on`, `wait-on-ti
232232

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

235+
### using Netlify CLI
236+
237+
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.
238+
239+
```shell
240+
$ npm i -D netlify-cli
241+
```
242+
243+
```toml
244+
[[plugins]]
245+
package = "netlify-plugin-cypress"
246+
# start Netlify server
247+
[plugins.inputs.preBuild]
248+
start = 'npx netlify dev'
249+
wait-on = 'http://localhost:8888'
250+
```
251+
252+
For more, see [tests/test-netlify-dev](./tests/test-netlify-dev) example and read [Testing Netlify Function](https://glebbahmutov.com/blog/test-netlify/#testing-netlify-functions) section.
253+
235254
### skipping tests
236255

237256
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):

circle.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ jobs:
102102
environment:
103103
DEBUG: netlify-plugin-cypress
104104

105+
'test-netlify-dev':
106+
executor: cypress/base-12-14-0
107+
steps:
108+
# all dependencies were installed in previous job
109+
- attach_workspace:
110+
at: ~/
111+
- run:
112+
name: Netlify Build 🏗
113+
command: npx netlify build
114+
working_directory: tests/test-netlify-dev
115+
environment:
116+
DEBUG: netlify-plugin-cypress
117+
105118
routing:
106119
executor: cypress/base-12-14-0
107120
steps:
@@ -139,6 +152,9 @@ workflows:
139152
- test-using-chromium:
140153
requires:
141154
- cypress/install
155+
- test-netlify-dev:
156+
requires:
157+
- cypress/install
142158
- routing:
143159
requires:
144160
- cypress/install
@@ -151,6 +167,7 @@ workflows:
151167
- 'test-twice'
152168
- 'test-prebuild-only'
153169
- test-using-chromium
170+
- test-netlify-dev
154171
- 'routing'
155172
filters:
156173
branches:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"pluginsFile": false,
3+
"supportFile": false,
4+
"fixturesFolder": false,
5+
"baseUrl": "http://localhost:8888"
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference types="cypress" />
2+
it('loads page', () => {
3+
cy.visit('/')
4+
cy.contains('Placeholder').should('be.visible')
5+
})
6+
7+
it('greets me', () => {
8+
cy.visit('/')
9+
cy.request('/api/hello')
10+
.its('body')
11+
.should('equal', 'Hello from a serverless function!')
12+
})
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# this Netlify project uses both functions
2+
# and API redirects
3+
4+
[build]
5+
command = "echo 'Netlify build command ...'"
6+
publish = "public"
7+
functions = "src/functions"
8+
9+
[build.environment]
10+
# cache Cypress binary in local "node_modules" folder
11+
# so Netlify caches it
12+
CYPRESS_CACHE_FOLDER = "./node_modules/CypressBinary"
13+
# set TERM variable for terminal output
14+
TERM = "xterm"
15+
16+
[dev]
17+
command = "npx serve public"
18+
targetPort = 5000
19+
framework = "#custom"
20+
21+
[[redirects]]
22+
from = "/api/*"
23+
to = "/.netlify/functions/:splat"
24+
status = 200
25+
26+
[[plugins]]
27+
# local Cypress plugin will test our site after it is built
28+
# in production, please use: package = "netlify-plugin-cypress"
29+
package = "../../"
30+
31+
# the local site uses Netlify API redirects
32+
# and Netlify functions
33+
# let's run tests against Netlify Dev server
34+
[plugins.inputs.preBuild]
35+
start = 'npx netlify dev'
36+
wait-on = 'http://localhost:8888'
37+
38+
# and skip tests after building it
39+
[plugins.inputs]
40+
skip = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<body>Placeholder</body>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Netlify function
2+
exports.handler = async (event, context) => {
3+
try {
4+
return { statusCode: 200, body: `Hello from a serverless function!` }
5+
} catch (err) {
6+
return { statusCode: 500, body: err.toString() }
7+
}
8+
}

0 commit comments

Comments
 (0)