In this tutorial, you will learn how to set-up your project so you can run your project's 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 them against your local build, however, this does not stop your website falling over, or, your staging environment from breaking.  When writing end-to-end tests you will likely want to run them against multiple different environments, maybe triggered after a successful deployment to an environment, or nightly in the early days just to make sure your environments are still working as expected.  There are several different ways you can configure your project; in today's tutorial, I will share my preferred approach.  Sound good? 

All the Config?

As Cypress is installed via NPM, after a successful installation, if you look within the project you should see a file called Cypress.json. Cypress.json is the file to update for all global settings. What it really wants to do is create a config file for each environment.

First, in your solution create a folder called 'config' or something similar. Copy your cypress.json file and create a new file for each environment you want to configure. So, cypress.stg.json etc.... Your config file might look similar to this:

When you run Cypress you will need a way to set the environment. If you are running a normal Javascript project with web pack, etc.. then you can use an env variable and configure it in your web pack config file. If you are only running Cypress in your project, I suggest that within your plugins/index.html you update your module exports with some code similar to this:

Now you have some specific config files and a way to load the correct file before your tests run, the last part is to create some scrips 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 are you like, enjoy!