Skip to content

Commit ca29752

Browse files
authored
feat: serve HTML static pages by default (#117)
1 parent 4498c8a commit ca29752

File tree

12 files changed

+132
-39
lines changed

12 files changed

+132
-39
lines changed

Diff for: .gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
node_modules/
2-
cypress/screenshots
2+
**/cypress/screenshots
33
**/cypress/videos
44
build
55

66
# Local Netlify folder
7-
.netlify
7+
.netlify

Diff for: README.md

+23
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,24 @@ If you are testing the site before building it, you probably want to skip testin
266266

267267
Running tests in parallel **is not supported** because Netlify plugin system runs on a single machine. Thus you can record the tests on Cypress Dashboard, but not run tests in parallel. If Netlify expands its build offering by allowing multiple build machines, we could take advantage of it and run tests in parallel.
268268

269+
### HTML files
270+
271+
When serving the built folder, we automatically serve `.html` files. For example, if your folder has the following structure:
272+
273+
```
274+
public/
275+
index.html
276+
pages/
277+
about.html
278+
```
279+
280+
The `public` folder is served automatically and the following test successfully visits both the root and the `about.html` pages:
281+
282+
```js
283+
cy.visit('/')
284+
cy.visit('/pages/about') // visits the about.html
285+
```
286+
269287
## Example repos
270288

271289
Name | Description
@@ -307,6 +325,11 @@ Set environment variable `DEBUG=netlify-plugin-cypress` to see the debug logs. T
307325
Cypress run, add an environment variable <code>TERM = xterm</code>.
308326
</details>
309327

328+
<details>
329+
<summary>Electron browser crashes while running tests</summary>
330+
Switch to using Chromium browser that seems to be a bit more reliable. Use <code>browser = "chromium"</code> setting.
331+
</details>
332+
310333
## License
311334

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

Diff for: circle.yml

+17
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ jobs:
115115
environment:
116116
DEBUG: netlify-plugin-cypress
117117

118+
'html-pages':
119+
executor: cypress/base-12-14-0
120+
steps:
121+
# all dependencies were installed in previous job
122+
- attach_workspace:
123+
at: ~/
124+
- run:
125+
name: Netlify Build 🏗
126+
command: npx netlify build
127+
working_directory: tests/html-pages
128+
environment:
129+
DEBUG: netlify-plugin-cypress
130+
118131
routing:
119132
executor: cypress/base-12-14-0
120133
steps:
@@ -137,6 +150,9 @@ workflows:
137150
- 'basic test':
138151
requires:
139152
- cypress/install
153+
- 'html-pages':
154+
requires:
155+
- cypress/install
140156
- 'recommended test':
141157
requires:
142158
- cypress/install
@@ -162,6 +178,7 @@ workflows:
162178
requires:
163179
- build
164180
- 'basic test'
181+
- 'html-pages'
165182
- 'recommended test'
166183
- 'recording test'
167184
- 'test-twice'

Diff for: src/index.js

+1-37
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,10 @@
11
// @ts-check
2-
const LocalWebServer = require('local-web-server')
32
const debug = require('debug')('netlify-plugin-cypress')
43
const debugVerbose = require('debug')('netlify-plugin-cypress:verbose')
5-
const { ping, getBrowserPath } = require('./utils')
6-
const fs = require('fs')
4+
const { ping, getBrowserPath, serveFolder } = require('./utils')
75

86
const DEFAULT_BROWSER = 'electron'
97

10-
function serveFolder(directory, port, spa) {
11-
if (typeof spa === 'boolean') {
12-
if (spa) {
13-
// spa parameter should be the name of the
14-
// fallback file in the directory to serve
15-
// typically it is "index.html"
16-
spa = 'index.html'
17-
} else {
18-
// do not use fallback mechanism for routing
19-
spa = undefined
20-
}
21-
}
22-
debug(
23-
'serving local folder %o from working directory %s',
24-
{
25-
directory,
26-
port,
27-
spa,
28-
},
29-
process.cwd(),
30-
)
31-
32-
if (!fs.existsSync(directory)) {
33-
throw new Error(`Cannot find folder "${directory}" to serve`)
34-
}
35-
36-
return LocalWebServer.create({
37-
// @ts-ignore
38-
directory,
39-
port,
40-
spa,
41-
}).server
42-
}
43-
448
function startServerMaybe(run, options = {}) {
459
const startCommand = options.start
4610
if (!startCommand) {

Diff for: src/serve-cli.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// a little utility to debug our static file server
2+
const { serveFolder } = require('./utils')
3+
4+
serveFolder('./public', '8080', false)

Diff for: src/utils.js

+47
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// @ts-check
22
const puppeteer = require('puppeteer')
33
const debug = require('debug')('netlify-plugin-cypress')
4+
const LocalWebServer = require('local-web-server')
5+
const fs = require('fs')
46
const { ping } = require('./ping')
57

68
const getBrowserPath = async () => {
@@ -15,7 +17,52 @@ const getBrowserPath = async () => {
1517
return info.executablePath
1618
}
1719

20+
/**
21+
* Servers local folder
22+
* @see https://github.com/lwsjs/local-web-server
23+
* @param {string} directory
24+
* @param {number} port
25+
* @param {boolean|'index.html'} spa
26+
*/
27+
function serveFolder(directory, port, spa) {
28+
if (typeof spa === 'boolean') {
29+
if (spa) {
30+
// spa parameter should be the name of the
31+
// fallback file in the directory to serve
32+
// typically it is "index.html"
33+
spa = 'index.html'
34+
} else {
35+
// do not use fallback mechanism for routing
36+
spa = undefined
37+
}
38+
}
39+
debug(
40+
'serving local folder %o from working directory %s',
41+
{
42+
directory,
43+
port,
44+
spa,
45+
},
46+
process.cwd(),
47+
)
48+
49+
if (!fs.existsSync(directory)) {
50+
throw new Error(`Cannot find folder "${directory}" to serve`)
51+
}
52+
53+
return LocalWebServer.create({
54+
// @ts-ignore
55+
directory,
56+
port,
57+
spa,
58+
// to debug use
59+
// DEBUG=koa-send
60+
staticExtensions: ['html'],
61+
}).server
62+
}
63+
1864
module.exports = {
1965
ping,
2066
getBrowserPath,
67+
serveFolder,
2168
}

Diff for: tests/html-pages/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# HTML pages
2+
3+
See [#116](https://github.com/cypress-io/netlify-plugin-cypress/issues/116)
4+
5+
In this example the public folder has both `index.html` and pages like `public/commands/about.html` which we want to visit using `cy.visit('/commands/about')` without using `.html` extension
6+
7+
Test locally with
8+
9+
```
10+
$ DEBUG=netlify-plugin-cypress ../../node_modules/.bin/netlify build
11+
```

Diff for: tests/html-pages/cypress.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"pluginsFile": false,
3+
"supportFile": false,
4+
"fixturesFolder": false
5+
}

Diff for: tests/html-pages/cypress/integration/spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference types="cypress" />
2+
describe('html-pages', () => {
3+
it('loads the index page', () => {
4+
cy.visit('/')
5+
cy.contains('Index page')
6+
})
7+
8+
it('loads the about page', () => {
9+
cy.visit('/commands/about')
10+
cy.contains('About')
11+
})
12+
})

Diff for: tests/html-pages/netlify.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[build]
2+
command = "echo 'Netlify build command ...'"
3+
publish = "public"
4+
5+
[[plugins]]
6+
# local Cypress plugin will test our site after it is built
7+
# in production, please use: package = "netlify-plugin-cypress"
8+
package = "../../"

Diff for: tests/html-pages/public/commands/about.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>About</h1>

Diff for: tests/html-pages/public/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Index page</h1>

0 commit comments

Comments
 (0)