E2E with Cypress
Cypress is an end-to-end web app testing framework with automatic waiting, live reloads, and multiple browser support.
Cypress in @iqmetrix/cra-template
Note that Cypress is already configured if you created your app with @iqmetrix/cra-template
.
#
Global Cypress ConfigurationInstall Cypress, Mocha Reporter and utility packages
- Cypress: main package with the UI (
cypress open
) and CLI (cypress run
) - mocha-junit-reporter: better JUnit reports (Azure DevOps supports only JUnit, NUint, VSTest, XUnit & CTest test result formats)
- mocha: framework used by the reporter
- start-server-and-test: utility based on wait-on library to run the server and tests together.
- @iqmetrix/cypress: utilities created by iqmetrix to make your life easier. Check the api doc page
- Cypress: main package with the UI (
Bootstrap your Cypress config. This command will create samples for E2E and unit tests, along with examples of mocking APIs (fixtures).
Add the generated Cypress files to
.gitignore
file.gitignoreImport
@iqmetrix/cypress
.cypress/support/index.jsConfigure Cypress to find your running application and report in JUnit format.
Refer to the full Cypress configuration documentation for more details.
cypress.jsonConfigure your auth and client application credentials
Cypress needs to log in in order to interact with your application. You'll need to provide it the following settings in order for it to authenticate. For the
clientId
, you should use the one associated to your backend service. If you are unsure of what all this means, please refer to the Auth service documentation.cypress.json
Secure credential storage
We recommend storing secrets and credentials in a more access controlled area rather than storing them in your cypress.json
and checking them into source control. If you'd like to go down this route, see here;
#
Additional configuration for CRA/typescript projectsFor more information, refer to the official Cypress CRA guide
Add the following dependencies
Update the types for your
cypress/plugins/index.js
cypress/plugins/index.jsFinally, add a new
tsconfig.json
under the cypress foldercypress/tsconfig.jsonIf you are using
eslint
, you will need to remove theparserOptions.project
configuration if set.
#
Configure ScriptsIf you wish to have your Cypress tests run during the build step in your CI/CD, you can modify your test
task to run them like below. Otherwise, adding the test:e2e
alone for local usage is sufficient!
For React apps
Remember to update the
TODO_APP_NAME
with your app path url.package.jsonFor Hub v1 apps
package.json
Most of Hub v1 apps have no unit tests set-up. If you have it on your app, follow the same scripts of React apps configuration, changing only the
test:unit
andtest:cypress
scripts.
#
Write TestsRefer to the official Cypress documentation for full usage examples.
Writing test for Typescript projects
When writing tests for Typescript projects, you can use the extension .ts
for your test files. This allows you to test the typing of your components and functions.
#
Storing Credentials SecurelyKeeping secrets out of source control is a good practice (even if they are just for the test environment!). In the example above, we're pulling credentials from environment variables via cy.login(Cypress.env("auth"))
. There are two methods of keeping your secrets in a safe place while allowing your Cypress tests to use them: Pipeline environment variables or Azure Key Vault.
#
Pipeline Environment VariablesYou can set environment variables on the Pipeline with the ability to keep them secret in Azure DevOps.
- Set these environment variables in your Pipeline and local environment. Environment variables can be set directly on the Pipeline in the Azure DevOps Pipeline or you can create a Variable Group and have the Pipeline use that Variable Group. You may also make them 'secret' as well! Cypress will strip the
CYPRESS_
and inject everything else into theCypress.env()
. See here for more details;
- Update your
cy.login
call in your tests
#
Azure Key VaultPrerequisites: You must have an Azure subscription and a Key Vault resource to use this plugin.
Alternatively, you can store your secrets/credentials in Azure Key Vault and use the cy.loginWithKeyVaultCredentials
method. In order for Cypress to access Key Vault, you'll need to create a Service Principal and authorize the Service Principal to get secrets from your Key Vault.
- Configuring Key Vault
Use the Azure Cloud Shell snippet below to create/get client secret credentials.
- Set correct account subscription:
- Create a service principal and configure its access to Azure resources:
- Grant the above mentioned application authorization to perform secret operations on the keyvault:
Set environment variables to authenticate with your Azure Key Vault. These can all be found through the Azure Portal under Azure Active Directory
> App registrations
. The Client ID
and Tenant ID
can be found in Summary
and Client Secret
is under Certificates & secrets
. For example, you can access hubtests
using information from its applications registration.
Enviroment Variables on Azure
Don't forget to set these environment variables on your Pipeline too! Environment variables can be set directly on the Pipeline in the Azure DevOps Pipeline or you can create a Variable Group and have the Pipeline use that Variable Group.
- Register the Cypress plugin
Create a test user or use an existing one
Use the commands in your tests to authenticate test users
#
Mocking APIsIf you want to test only the flow and mock APIs or if your API is not ready and you want to write tests and not wait for it, you can create a custom command to mock app APIs.
Create the custom command for mocking APIs
cypress/support/mockApi.jsImport it in your cypress suite
cypress/support/index.jsAdd the mocked response for your API into the fixtures folder
cypress/fixtures/me.jsonUse the created
mockApi
command into your testscypress/integration/example2.test.js
#
Notes#
Browser Support in Cypress 4Cypress 4 marks a significant milestone in development that will continue to elevate the testing experience for everyone. Release notes contains a broader list of breaking changes, features and bug fixes, but the following highlights we consider being important:
Requirements: upgrade Cypress to 4.0.0 and Node.js 8 and above
Firefox and Edge support is in beta and produces CORS issues in Hub. No workaround currently exists, and we suggest to wait for full support. Similar issues can be found on the Cypress Gitter.
Tests in Edge could fail to run on macOS if another instance of Edge is open. This is similar to a resolved FireFox bug #6380
https://docs.cypress.io/guides/references/trade-offs.html#Multiple-browsers-open-at-the-same-time)
#
Persist authentication tokens between testsBy default Cypress does not preserve localStorage
between tests. This requires you to explicitly restore the token between the tests when using older versions of @iqmetrix/get-authentication-token