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 can be used to add any global settings your cypress project might need.
.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.stg.json etc... The content of each config file could look similar to this:
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 🤘