Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,25 @@ Parameters you can place into `preBuild` inputs: `start`, `wait-on`, `wait-on-ti

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

### using Netlify CLI

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.

```shell
$ npm i -D netlify-cli
```

```toml
[[plugins]]
package = "netlify-plugin-cypress"
# start Netlify server
[plugins.inputs.preBuild]
start = 'npx netlify dev'
wait-on = 'http://localhost:8888'
```

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.

### skipping tests

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):
Expand Down
17 changes: 17 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ jobs:
environment:
DEBUG: netlify-plugin-cypress

'test-netlify-dev':
executor: cypress/base-12-14-0
steps:
# all dependencies were installed in previous job
- attach_workspace:
at: ~/
- run:
name: Netlify Build 🏗
command: npx netlify build
working_directory: tests/test-netlify-dev
environment:
DEBUG: netlify-plugin-cypress

routing:
executor: cypress/base-12-14-0
steps:
Expand Down Expand Up @@ -139,6 +152,9 @@ workflows:
- test-using-chromium:
requires:
- cypress/install
- test-netlify-dev:
requires:
- cypress/install
- routing:
requires:
- cypress/install
Expand All @@ -151,6 +167,7 @@ workflows:
- 'test-twice'
- 'test-prebuild-only'
- test-using-chromium
- test-netlify-dev
- 'routing'
filters:
branches:
Expand Down
6 changes: 6 additions & 0 deletions tests/test-netlify-dev/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pluginsFile": false,
"supportFile": false,
"fixturesFolder": false,
"baseUrl": "http://localhost:8888"
}
12 changes: 12 additions & 0 deletions tests/test-netlify-dev/cypress/integration/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference types="cypress" />
it('loads page', () => {
cy.visit('/')
cy.contains('Placeholder').should('be.visible')
})

it('greets me', () => {
cy.visit('/')
cy.request('/api/hello')
.its('body')
.should('equal', 'Hello from a serverless function!')
})
40 changes: 40 additions & 0 deletions tests/test-netlify-dev/netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# this Netlify project uses both functions
# and API redirects

[build]
command = "echo 'Netlify build command ...'"
publish = "public"
functions = "src/functions"

[build.environment]
# cache Cypress binary in local "node_modules" folder
# so Netlify caches it
CYPRESS_CACHE_FOLDER = "./node_modules/CypressBinary"
# set TERM variable for terminal output
TERM = "xterm"

[dev]
command = "npx serve public"
targetPort = 5000
framework = "#custom"

[[redirects]]
from = "/api/*"
to = "/.netlify/functions/:splat"
status = 200

[[plugins]]
# local Cypress plugin will test our site after it is built
# in production, please use: package = "netlify-plugin-cypress"
package = "../../"

# the local site uses Netlify API redirects
# and Netlify functions
# let's run tests against Netlify Dev server
[plugins.inputs.preBuild]
start = 'npx netlify dev'
wait-on = 'http://localhost:8888'

# and skip tests after building it
[plugins.inputs]
skip = true
1 change: 1 addition & 0 deletions tests/test-netlify-dev/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<body>Placeholder</body>
8 changes: 8 additions & 0 deletions tests/test-netlify-dev/src/functions/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Netlify function
exports.handler = async (event, context) => {
try {
return { statusCode: 200, body: `Hello from a serverless function!` }
} catch (err) {
return { statusCode: 500, body: err.toString() }
}
}