From 871cf5a42bd1e5483aa89213e5da173e9d9e1e7c Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Tue, 9 Feb 2021 11:31:57 -0500 Subject: [PATCH 1/5] feat: serve HTML static pages by default --- circle.yml | 17 +++++++++++++++++ tests/html-pages/README.md | 5 +++++ tests/html-pages/cypress.json | 5 +++++ tests/html-pages/cypress/integration/spec.js | 10 ++++++++++ tests/html-pages/netlify.toml | 8 ++++++++ tests/html-pages/public/commands/about.html | 1 + tests/html-pages/public/index.html | 1 + 7 files changed, 47 insertions(+) create mode 100644 tests/html-pages/README.md create mode 100644 tests/html-pages/cypress.json create mode 100644 tests/html-pages/cypress/integration/spec.js create mode 100644 tests/html-pages/netlify.toml create mode 100644 tests/html-pages/public/commands/about.html create mode 100644 tests/html-pages/public/index.html diff --git a/circle.yml b/circle.yml index f4d3ad1a..f5b18e5a 100644 --- a/circle.yml +++ b/circle.yml @@ -115,6 +115,19 @@ jobs: environment: DEBUG: netlify-plugin-cypress + 'html-pages': + 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/html-pages + environment: + DEBUG: netlify-plugin-cypress + routing: executor: cypress/base-12-14-0 steps: @@ -137,6 +150,9 @@ workflows: - 'basic test': requires: - cypress/install + - 'html-pages': + requires: + - cypress/install - 'recommended test': requires: - cypress/install @@ -162,6 +178,7 @@ workflows: requires: - build - 'basic test' + - 'html-pages' - 'recommended test' - 'recording test' - 'test-twice' diff --git a/tests/html-pages/README.md b/tests/html-pages/README.md new file mode 100644 index 00000000..f27fd33a --- /dev/null +++ b/tests/html-pages/README.md @@ -0,0 +1,5 @@ +# HTML pages + +See [#116](https://github.com/cypress-io/netlify-plugin-cypress/issues/116) + +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 diff --git a/tests/html-pages/cypress.json b/tests/html-pages/cypress.json new file mode 100644 index 00000000..e36f9847 --- /dev/null +++ b/tests/html-pages/cypress.json @@ -0,0 +1,5 @@ +{ + "pluginsFile": false, + "supportFile": false, + "fixturesFolder": false +} diff --git a/tests/html-pages/cypress/integration/spec.js b/tests/html-pages/cypress/integration/spec.js new file mode 100644 index 00000000..5d994089 --- /dev/null +++ b/tests/html-pages/cypress/integration/spec.js @@ -0,0 +1,10 @@ +/// +describe('html-pages', () => { + it('loads the index page', () => { + cy.visit('/') + }) + + it('loads the about page', () => { + cy.visit('/commands/about') + }) +}) diff --git a/tests/html-pages/netlify.toml b/tests/html-pages/netlify.toml new file mode 100644 index 00000000..13b880f2 --- /dev/null +++ b/tests/html-pages/netlify.toml @@ -0,0 +1,8 @@ +[build] +command = "echo 'Netlify build command ...'" +publish = "public" + +[[plugins]] + # local Cypress plugin will test our site after it is built + # in production, please use: package = "netlify-plugin-cypress" + package = "../../" diff --git a/tests/html-pages/public/commands/about.html b/tests/html-pages/public/commands/about.html new file mode 100644 index 00000000..91462584 --- /dev/null +++ b/tests/html-pages/public/commands/about.html @@ -0,0 +1 @@ +

Basic

diff --git a/tests/html-pages/public/index.html b/tests/html-pages/public/index.html new file mode 100644 index 00000000..91462584 --- /dev/null +++ b/tests/html-pages/public/index.html @@ -0,0 +1 @@ +

Basic

From 20217651f3ab6a7b90e57035e3b5350886097811 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Tue, 9 Feb 2021 11:34:53 -0500 Subject: [PATCH 2/5] update tests a little --- tests/html-pages/cypress/integration/spec.js | 2 ++ tests/html-pages/public/commands/about.html | 2 +- tests/html-pages/public/index.html | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/html-pages/cypress/integration/spec.js b/tests/html-pages/cypress/integration/spec.js index 5d994089..3be4268f 100644 --- a/tests/html-pages/cypress/integration/spec.js +++ b/tests/html-pages/cypress/integration/spec.js @@ -2,9 +2,11 @@ describe('html-pages', () => { it('loads the index page', () => { cy.visit('/') + cy.contains('Index page') }) it('loads the about page', () => { cy.visit('/commands/about') + cy.contains('About') }) }) diff --git a/tests/html-pages/public/commands/about.html b/tests/html-pages/public/commands/about.html index 91462584..ae068f61 100644 --- a/tests/html-pages/public/commands/about.html +++ b/tests/html-pages/public/commands/about.html @@ -1 +1 @@ -

Basic

+

About

diff --git a/tests/html-pages/public/index.html b/tests/html-pages/public/index.html index 91462584..04a30d1e 100644 --- a/tests/html-pages/public/index.html +++ b/tests/html-pages/public/index.html @@ -1 +1 @@ -

Basic

+

Index page

From d0dc036f83a51e256c3e2e19afee7fb51f0af370 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Tue, 9 Feb 2021 11:49:09 -0500 Subject: [PATCH 3/5] refactor serve --- .gitignore | 4 ++-- src/index.js | 38 +------------------------------- src/utils.js | 45 ++++++++++++++++++++++++++++++++++++++ tests/html-pages/README.md | 6 +++++ 4 files changed, 54 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index c231968a..fb883bdb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ node_modules/ -cypress/screenshots +**/cypress/screenshots **/cypress/videos build # Local Netlify folder -.netlify \ No newline at end of file +.netlify diff --git a/src/index.js b/src/index.js index 5f6a36f2..e16ef131 100644 --- a/src/index.js +++ b/src/index.js @@ -1,46 +1,10 @@ // @ts-check -const LocalWebServer = require('local-web-server') const debug = require('debug')('netlify-plugin-cypress') const debugVerbose = require('debug')('netlify-plugin-cypress:verbose') -const { ping, getBrowserPath } = require('./utils') -const fs = require('fs') +const { ping, getBrowserPath, serveFolder } = require('./utils') const DEFAULT_BROWSER = 'electron' -function serveFolder(directory, port, spa) { - if (typeof spa === 'boolean') { - if (spa) { - // spa parameter should be the name of the - // fallback file in the directory to serve - // typically it is "index.html" - spa = 'index.html' - } else { - // do not use fallback mechanism for routing - spa = undefined - } - } - debug( - 'serving local folder %o from working directory %s', - { - directory, - port, - spa, - }, - process.cwd(), - ) - - if (!fs.existsSync(directory)) { - throw new Error(`Cannot find folder "${directory}" to serve`) - } - - return LocalWebServer.create({ - // @ts-ignore - directory, - port, - spa, - }).server -} - function startServerMaybe(run, options = {}) { const startCommand = options.start if (!startCommand) { diff --git a/src/utils.js b/src/utils.js index 4b3dbaf5..3d4749e2 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,6 +1,8 @@ // @ts-check const puppeteer = require('puppeteer') const debug = require('debug')('netlify-plugin-cypress') +const LocalWebServer = require('local-web-server') +const fs = require('fs') const { ping } = require('./ping') const getBrowserPath = async () => { @@ -15,7 +17,50 @@ const getBrowserPath = async () => { return info.executablePath } +/** + * Servers local folder + * @see https://github.com/lwsjs/local-web-server + * @param {string} directory + * @param {number} port + * @param {boolean|'index.html'} spa + */ +function serveFolder(directory, port, spa) { + if (typeof spa === 'boolean') { + if (spa) { + // spa parameter should be the name of the + // fallback file in the directory to serve + // typically it is "index.html" + spa = 'index.html' + } else { + // do not use fallback mechanism for routing + spa = undefined + } + } + debug( + 'serving local folder %o from working directory %s', + { + directory, + port, + spa, + }, + process.cwd(), + ) + + if (!fs.existsSync(directory)) { + throw new Error(`Cannot find folder "${directory}" to serve`) + } + + return LocalWebServer.create({ + // @ts-ignore + directory, + port, + spa, + staticExtensions: 'html', + }).server +} + module.exports = { ping, getBrowserPath, + serveFolder, } diff --git a/tests/html-pages/README.md b/tests/html-pages/README.md index f27fd33a..d21a97b7 100644 --- a/tests/html-pages/README.md +++ b/tests/html-pages/README.md @@ -3,3 +3,9 @@ See [#116](https://github.com/cypress-io/netlify-plugin-cypress/issues/116) 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 + +Test locally with + +``` +$ DEBUG=netlify-plugin-cypress ../../node_modules/.bin/netlify build +``` From c7cfb047436ce7a3b965b7bb6a62cecbd5fdec37 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Tue, 9 Feb 2021 11:54:28 -0500 Subject: [PATCH 4/5] fix the static extensions list --- src/serve-cli.js | 4 ++++ src/utils.js | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 src/serve-cli.js diff --git a/src/serve-cli.js b/src/serve-cli.js new file mode 100644 index 00000000..6eb82531 --- /dev/null +++ b/src/serve-cli.js @@ -0,0 +1,4 @@ +// a little utility to debug our static file server +const { serveFolder } = require('./utils') + +serveFolder('./public', '8080', false) diff --git a/src/utils.js b/src/utils.js index 3d4749e2..c40b55ab 100644 --- a/src/utils.js +++ b/src/utils.js @@ -55,7 +55,9 @@ function serveFolder(directory, port, spa) { directory, port, spa, - staticExtensions: 'html', + // to debug use + // DEBUG=koa-send + staticExtensions: ['html'], }).server } From 097cd4b9542ad520246cbbe79c49721414130268 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Tue, 9 Feb 2021 12:01:50 -0500 Subject: [PATCH 5/5] add note about html extension --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index ab92cf62..ebb5dbe0 100644 --- a/README.md +++ b/README.md @@ -266,6 +266,24 @@ If you are testing the site before building it, you probably want to skip testin 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. +### HTML files + +When serving the built folder, we automatically serve `.html` files. For example, if your folder has the following structure: + +``` +public/ + index.html + pages/ + about.html +``` + +The `public` folder is served automatically and the following test successfully visits both the root and the `about.html` pages: + +```js +cy.visit('/') +cy.visit('/pages/about') // visits the about.html +``` + ## Example repos Name | Description @@ -307,6 +325,11 @@ Set environment variable `DEBUG=netlify-plugin-cypress` to see the debug logs. T Cypress run, add an environment variable TERM = xterm. +
+ Electron browser crashes while running tests + Switch to using Chromium browser that seems to be a bit more reliable. Use browser = "chromium" setting. +
+ ## License This project is licensed under the terms of the [MIT license](LICENSE.md).