Skip to content

Commit

Permalink
[chore] Add end-to-end testing with Cypress (#2261)
Browse files Browse the repository at this point in the history
Can be used to write automated tests which actually utilize a browser.

Will produce videos as artifacts.

Can also be run locally.

Uses an environment variable for logging in.
  • Loading branch information
Benedicte Emilie Brækken committed Jan 28, 2021
1 parent 686fb62 commit b3db232
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 1 deletion.
53 changes: 53 additions & 0 deletions .github/workflows/test_e2e.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: End-to-end tests

on: [push]

jobs:
e2e:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x]

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Cache node modules
uses: actions/cache@v2
env:
# added -vX just to reset the cache, feel free to up it if you need
# to reset the cache again
cache-name: cache-node-modules-v4
with:
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
- name: Install npm dependencies
run: npm install

- name: Run cypress
run: npm run test-e2e
env:
CYPRESS_SANITY_SESSION_TOKEN: ${{ secrets.CYPRESS_SANITY_SESSION_TOKEN }}

- name: Archive videos
uses: actions/upload-artifact@v2
with:
name: videos
path: cypress/videos
# Make sure this step runs even though the test steps fails.
# Otherwise we won't get videos if tests fail.
if: ${{ always() }}

- name: Archive screenshots
uses: actions/upload-artifact@v2
with:
name: screenshots
path: cypress/screenshots
# Make sure this step runs even though the test steps fails.
# Otherwise we won't get videos if tests fail.
if: ${{ always() }}
3 changes: 3 additions & 0 deletions cypress/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['plugin:cypress/recommended'],
}
2 changes: 2 additions & 0 deletions cypress/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/videos/
/screenshots/
3 changes: 3 additions & 0 deletions cypress/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"baseUrl": "http://localhost:3333"
}
5 changes: 5 additions & 0 deletions cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Using fixtures to represent data",
"email": "hello@cypress.io",
"body": "Fixtures are a great way to mock data for responses to routes"
}
7 changes: 7 additions & 0 deletions cypress/integration/booleanInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe('BooleanInput', () => {
it('should have a height of 35px if no description', () => {
cy.visit('/test/desk/booleansTest;bd99e58a-845f-4d52-b54a-56a9b7af3be1')

cy.getField('switchIndeterminate2').invoke('outerHeight').should('be.eq', 35)
})
})
21 changes: 21 additions & 0 deletions cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference types="cypress" />
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

/**
* @type {Cypress.PluginConfig}
*/
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
}
29 changes: 29 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Cypress.Commands.add('login', (sanitySessionToken) => {
const token = sanitySessionToken ? sanitySessionToken : Cypress.env('SANITY_SESSION_TOKEN')

cy.intercept({url: 'v1/users/me', method: 'GET'}).as('getUser')

return cy.visit('/').then(() => {
cy.wait('@getUser').then((interception) => {
const domain = new URL(interception.response.url).hostname

cy.setCookie('sanitySession', token, {
secure: true,
httpOnly: true,
sameSite: 'None',
domain: `.${domain}`,
})
})
})
})

Cypress.Commands.add('getField', (fieldName) => {
// TODO(@benedicteb, 2021-01-26) Add <data-qa=..." /> or something to html to make select super robust
return cy.get(`[data-focus-path=${fieldName}]`)
})

Cypress.Commands.add('getFieldInput', (fieldName) => {
return cy.getField(fieldName).within(($field) => {
return cy.get('input')
})
})
23 changes: 23 additions & 0 deletions cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// ***********************************************************
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// noinspection ES6UnusedImports
import commands from './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')
before(() => {
cy.login()
})
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"stylelint:fix": "stylelint \"**/*.css\" --fix",
"test": "echo 'Run `npm run test-all` to run `npm test` for every package'",
"test-all": "npm run type-check && lerna run test --concurrency=2 --stream",
"test-e2e": "start-server-and-test start http://localhost:3333 'npm run cypress -- run --config-file ./cypress/cypress.json'",
"updated": "lerna updated",
"watch": "gulp watch",
"release-notes": "node scripts/printReleaseNotesTemplate.js",
Expand All @@ -46,7 +47,8 @@
"verify-format": "lerna run clean && npm run prettier -- -c",
"postinstall": "npm run bootstrap",
"vercel-build": "npm run build && cd examples/test-studio && npm run build -- -y",
"type-check": "tsc -b ./packages --force"
"type-check": "tsc -b ./packages --force",
"cypress": "cypress"
},
"devDependencies": {
"@babel/cli": "^7.11.6",
Expand All @@ -66,11 +68,13 @@
"babel-plugin-lodash": "^3.3.4",
"boxen": "^4.1.0",
"chalk": "^2.3.0",
"cypress": "^6.3.0",
"deepmerge": "^3.3.0",
"del": "^5.1.0",
"eslint": "^7.8.1",
"eslint-config-prettier": "^6.7.0",
"eslint-import-resolver-webpack": "^0.11.1",
"eslint-plugin-cypress": "^2.11.2",
"eslint-plugin-es5": "^1.4.1",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-prettier": "^3.1.1",
Expand All @@ -97,6 +101,7 @@
"prettier": "2.1.2",
"rimraf": "^2.7.1",
"semver": "^6.1.2",
"start-server-and-test": "^1.11.7",
"stylelint": "^13.4.1",
"stylelint-config-prettier": "^8.0.1",
"stylelint-prettier": "^1.1.2",
Expand Down

1 comment on commit b3db232

@vercel
Copy link

@vercel vercel bot commented on b3db232 Jan 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.