Setting up the test environment
We have installed the required libraries to write and run the tests, so the next thing on our list is to prepare the testing environment itself. There are two main things we need to do here—the first is to prepare the database configurations for the test database, and the second thing is to prepare the Strapi instance that is required for the tests.
Preparing the database configuration
When we are running our tests, we will often want to interact with the database. We will not want to use the real database for sure to run our tests, and the development database might contain old or dirty data that could potentially break the tests. Instead, we will set up a temporary database for the tests that we can use while running the tests so that they are run on a clean database, then remove it later on when it isn't needed.
When we use jest
to run the tests, it will set the NODE_ENV
variable value to test
. This means we can easily create...