One Way to Run Post Deployment Cypress Tests for an Amplify App
I recently had to figure out a way to have Cypress tests run against an Amplify app immediately after the app has been deployed. Since Amplify doesn’t have a post deployment step as part of its build and deployment process (defined in amplify.yml), I had to come up with a solution using other AWS services.
I settled on using a combination of Event Bridge and CodeBuild.
EventBridge
EventBridge is an AWS service that connects various AWS components together using events.
How it works
- Matches events emitted from AWS services based on a json pattern.
- A rule is where you define the event pattern to match
- The rule configuration page has presets to chose from, in this case I chose
Amplifyas thesourceandAmplify Deployment Status Changeas thedetail-type. This results in the following auto generated pattern:
{
"source": ["aws.amplify"],
"detail-type": ["Amplify Deployment Status Change"]
}
This would match many events during a build, so the pattern needs to be refined to be more specific to match a deployment success event. To see all the events for a source and detail-type you can set the rule target to cloudwatch and then view the logged json for all the events the source and detail-type send.
A rule has a target which is the AWS service to trigger, in this case the target will be a CodeBuild project.
This is the event pattern that I determined I will need to use by logging the events to cloudwatch as described above:
{
"source": ["aws.amplify"],
"detail-type": ["Amplify Deployment Status Change"],
"detail": {
"appId": ["your_app_id"],
"branchName": ["main"],
"jobStatus": ["SUCCEED"]
}
}
CodeBuild
CodeBuild is an AWS service that enables running various commands against a code repository to generate the final artifacts for storage in S3 and later deployment.
Commands are defined in various steps within a buildspec.yml file that is stored in the root of the code project repo. In my case, I can use CodeBuild simply to run my Cypress tests after deployment and to store the Cypress artifacts in S3 for viewing.
How it works
- A CodeBuild project is linked to a specific github repository.
- When a CodeBuild project is run, it clones the github repository and runs user defined commands within the build environment. In this case, it will run commands to install Cypress and run Cypress end to end tests against the deployed site.
- A command that returns a non zero exit code (such as
npx cypress run) will cause the build to be flagged as failed. - The CodeBuild Project can be configured to send a notification to an SNS topic when the build fails.
- CodeBuild will upload the Cypress artifacts (screen shots of failed tests) to an S3 bucket.
- Here is a simplified version of the buildspec.yml file that I am using:
version: 0.2
env:
variables: # Set any environment variables that Cypress needs here. Prefix them with CYPRESS_
phases:
install:
runtime-versions:
nodejs: latest
commands:
- npm ci
build:
commands:
- npx cypress run
artifacts:
files:
- "**/*.png"
- "**/*.mp4"
base-directory: "cypress"
discard-paths: yes # Uploads just the files, not their full directory paths
name: builds/$CODEBUILD_BUILD_NUMBER/cypress-artifacts
# make sure to set "enable semantic versioning" in the Codebuild project configuration section for artifacts