Skip to content

Commit

Permalink
feat: add configFile parameter (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Mar 5, 2021
1 parent 9456bf1 commit e1d95ed
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 11 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,27 @@ package = "netlify-plugin-cypress"
browser = "electron"
```

### configFile

If you would like to use a different Cypress config file instead of `cypress.json`, specify it using the `configFile` option

```toml
[build]
command = "npm run build"
publish = "build"
[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"

[[plugins]]
package = "netlify-plugin-cypress"
[plugins.inputs]
configFile = "cypress.netlify.json"
```

### testing SPA routes

SPAs need catch-all redirect setup to make non-root paths accessible by tests. You can enable this with `spa` parameter.
Expand Down
38 changes: 28 additions & 10 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
# since we do not need Cypress to publish the NPM package
# we can skip the binary download
CYPRESS_INSTALL_BINARY: 0
# we also skip Puppeteer Chromium download
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true

steps:
- checkout
- node/with-cache:
Expand Down Expand Up @@ -154,6 +157,19 @@ jobs:
environment:
DEBUG: netlify-plugin-cypress

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

workflows:
version: 2
test_and_release:
Expand Down Expand Up @@ -187,18 +203,20 @@ workflows:
- routing:
requires:
- cypress/install
- config-file:
requires:
- cypress/install
- release:
# run the release job on all branches
# since we might want to release a beta version
requires:
- build
# temporary while publishing pre-release
# - 'basic test'
# - 'html-pages'
# - 'recommended test'
# - 'recording test'
# - 'test-twice'
# - 'test-prebuild-only'
# - test-using-chromium
# - test-netlify-dev
# - 'routing'
- 'basic test'
- 'html-pages'
- 'recommended test'
- 'recording test'
- 'test-twice'
- 'test-prebuild-only'
- test-using-chromium
- test-netlify-dev
- 'routing'
3 changes: 3 additions & 0 deletions manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ inputs:
description: Allowed values are chromium, electron
default: chromium

- name: configFile
description: Path to the Cypress config file to use

- name: record
description: Record test results to Cypress Dashboard
default: false
Expand Down
21 changes: 20 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ async function waitOnMaybe(buildUtils, options = {}) {

const isValidBrowser = (name) => name === 'electron' || name === 'chromium'

async function runCypressTests(baseUrl, record, spec, group, tag, browser) {
async function runCypressTests(
baseUrl,
record,
spec,
group,
tag,
browser,
configFile,
) {
if (!isValidBrowser(browser)) {
throw new Error(`Invalid browser name "${browser}"`)
}
Expand Down Expand Up @@ -99,6 +107,7 @@ async function runCypressTests(baseUrl, record, spec, group, tag, browser) {
ciBuildId,
browser: browserPath,
headless: true,
configFile,
})
}

Expand Down Expand Up @@ -236,6 +245,7 @@ async function postBuild({
tag,
spa,
browser,
configFile,
errorCallback,
summaryCallback,
}) {
Expand All @@ -260,6 +270,7 @@ async function postBuild({
group,
tag,
browser,
configFile,
)

await new Promise((resolve, reject) => {
Expand Down Expand Up @@ -315,13 +326,16 @@ module.exports = {
}
}

const configFile = preBuildInputs.configFile

const results = await runCypressTests(
baseUrl,
record,
spec,
group,
tag,
browser,
configFile,
)

if (closeServer) {
Expand Down Expand Up @@ -369,6 +383,7 @@ module.exports = {
}
}
const spa = postBuildInputs.spa
const configFile = postBuildInputs.configFile

const errorCallback = utils.build.failBuild.bind(utils.build)
const summaryCallback = utils.status.show.bind(utils.status)
Expand All @@ -381,6 +396,7 @@ module.exports = {
tag,
spa,
browser,
configFile,
errorCallback,
summaryCallback,
})
Expand Down Expand Up @@ -448,6 +464,8 @@ module.exports = {
tag,
})

const configFile = onSuccessInputs.configFile

console.log('testing deployed url %s', deployPrimeUrl)
const results = await runCypressTests(
deployPrimeUrl,
Expand All @@ -456,6 +474,7 @@ module.exports = {
group,
tag,
browser,
configFile,
)
processCypressResults(results, errorCallback, summaryCallback)
},
Expand Down
14 changes: 14 additions & 0 deletions tests/config-file/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# using specific config file

These tests specify non-default Cypress configuration file to use

These tests run:
- [ ] before the build
- [x] after the build
- [ ] on deploy success

## Local testing

```
DEBUG=netlify-plugin-cypress npm run netlify:build
```
5 changes: 5 additions & 0 deletions tests/config-file/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"pluginsFile": false,
"supportFile": false,
"fixturesFolder": false
}
8 changes: 8 additions & 0 deletions tests/config-file/cypress.netlify.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"pluginsFile": false,
"supportFile": false,
"fixturesFolder": false,
"env": {
"CUSTOM_FILE": 42
}
}
7 changes: 7 additions & 0 deletions tests/config-file/cypress/integration/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// <reference types="cypress" />
describe('custom config file', () => {
it('uses cypress.netlify.json', () => {
// this property is set in the cypress.netlify.json file
expect(Cypress.env()).to.have.property('CUSTOM_FILE', 42)
})
})
18 changes: 18 additions & 0 deletions tests/config-file/netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[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 = "../../"

# do not run tests after deploy (testing)
[plugins.inputs]
enable = false
configFile = "cypress.netlify.json"

# run tests postBuild
[plugins.inputs.postBuild]
enable = true
configFile = "cypress.netlify.json"
12 changes: 12 additions & 0 deletions tests/config-file/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "config-file",
"version": "0.1.0",
"private": true,
"scripts": {
"start": "../../node_modules/.bin/react-scripts start",
"build": "../../node_modules/.bin/react-scripts build",
"cypress:open": "../../node_modules/.bin/cypress open",
"cypress:run": "../../node_modules/.bin/cypress run",
"netlify:build": "../../node_modules/.bin/netlify build"
}
}
1 change: 1 addition & 0 deletions tests/config-file/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Basic</h1>

0 comments on commit e1d95ed

Please sign in to comment.