Cypress
Overview
This document guides you through the steps required to run Cypress tests using Signadot's Continuous Testing (CT) feature.
Running Tests as a Signadot Job
Running a test as a Signadot Job provides context awareness, meaning that if the sandbox referenced in the test is terminated, closed, or in any other failed state, the test will automatically fail. This makes the tests more reliable.
Create Job Runner Group
To start with Signadot CT, we need to create a Job Runner Group
name: cypress
spec:
cluster: "@{cluster}"
labels:
env: "@{env}"
namespace: <namespace where to run the tests>
image: cypress/included:latest
jobTimeout: 30m
scaling:
manual:
desiredPods: 1
Create Job Specification
spec:
namePrefix: cypress-e2e
runnerGroup: cypress
script: |
#!/bin/bash
set -e
# Clone the git repo
echo "Cloning repo"
git clone --single-branch -b "@{branch}" \
https://github.com/<org>/<repo>.git
# Run the cypress test
cd <repo>
npx cypress run --spec cypress/e2e/my-test.cy.js
routingContext:
sandbox: "@{sandbox}"
uploadArtifact:
# By default, the artifacts are located in that path.
# However, check the settings to make sure is the right location.
- path: <repo>/cypress/videos/my-test.cy.js.mp4
Set Up the Routing Context
There are some use cases where we need to run tests against specific Sandboxes, so we need to rely on some routing context. We could use the preview URL, but there are some situations where it would be better to simply use the routing key. For the cypress integration, we have to intercept the requests.
Cypress.Commands.add('config', (from, to) => {
var frontendURL = 'https://yourwebsite.com';
// inject routing key
cy.intercept(frontendURL + '/*', (req) => {
req.headers['baggage'] += ',sd-routing-key=' + Cypress.env('SIGNADOT_ROUTING_KEY');
})
cy.visit(frontendURL);
// ... do more things
})
Also update the job to map the SIGNADOT_ROUTING_KEY
environment variable for use by Cypress. Note that Cypress requires
the use of prefix CYPRESS_
as mentioned on their official doc.
spec:
namePrefix: cypress-e2e
runnerGroup: cypress
script: |
#!/bin/bash
set -e
# Clone the git repo
echo "Cloning signadot repo"
git clone --single-branch -b "@{branch}" \
https://github.com/<org>/<repo>.git
# Run the cypress test
cd <repo>
export CYPRESS_SIGNADOT_ROUTING_KEY=$SIGNADOT_ROUTING_KEY
npx cypress run --spec cypress/e2e/my-test.cy.js
routingContext:
sandbox: "@{sandbox}"
uploadArtifact:
- path: your_project/cypress/videos/my-test.cy.js.mp4