In this tutorial, you will learn how to set up your project so you can run your Cypress tests against different environments. Cypress is a JavaScript end-to-end testing framework that I highly recommend you consider adopting for your JS projects (read this for some more information).
When writing tests, it's all very good running your integration tests against your local build. Running tests locally is not the same thing as running them against production. When writing end-to-end tests you will likely want to run your test suite against multiple different environments. For example, when code is being deployed by your CI/CD pipeline onto an environment. There are several different ways you can configure your project to accomplish this. In today's tutorial, I will share my preferred approach 😊
All the Config?
After installing Cypress within your project you will see a number of default install files. One of these files should be called cypress.json
. cypress.json
can be used to add any global settings your cypress project might need. cypress.json
is an ideal place to add all of our different environment information. Like a normal Javascript enviroment/.env
files, we could create a cypress.json
per environment. To do this create a folder in your project called config
or something similar. Copy your cypress.json
file and create a new file for each environment you want to configure, e.g. cypress.prod.json
, cypress.stg.json
etc... The content of each config file could look similar to this:
You should now have a config file per environment. Next, you will need a way of configuring Cypress to use the correct file in the correct circumstance. If you are running a normal JavaScript project with a web pack, etc.. then you can use an env
variable and configure it in your web pack config file. If you only have Cypress installed in your project, I suggest that within plugins/index.js
you update your module exports with some code similar to this:
You now have a way to load the correct files before your tests run, the last step is to create some scripts within your package.json
file for each environment you want to test, like so:
With these three bits of the puzzle solved, you can now run your integration tests against as many environments. In your build pipeline simply call the correct script and when your code is deploying to each environment and your integration tests will run against the correct environment. Happy Coding 🤘