Setting Up the Test Environment
:exclamation: For this project we have swapped out usual test-runner Jest for another common module, Mocha. This is because Jest was designed to try and run all the tests at the same time, which makes things difficult when your tests depend on whether or not certain data is in your database. This sort of interference is often referred to as a race condition.
As you will see, Mocha is very similar to Jest, and is a great addition to your CV.
- 
Install mocha,chai, andsupertestasdev dependenciesusingnpm i -D mocha chai supertest.
- 
In a previous step you have setup the .env.testwith the same environment variables as your.env. :exclamation: Make sure to give your test database a different name. Something likemusic_library_api_testwill do. Having this file will allow us to run our tests in a different database than our development one. This is handy, as our test database will be destroyed after our tests run.
- 
Make a folder called testsin the root of your project. Add a file calledtest-setup.jsIn this file we will require thedotenvmodule, and load the variables stored in our.env.testfile.
- 
Make sure .env.testis in your.gitignore.
- 
Add a testscript to yourpackage.jsonfile:mocha tests/**/*.js --exit --recursive --timeout 60000 --file ./tests/test-setup.js.
- 
Add a pretestscript to yourpackage.json. Set the command to:node scripts/create-database.js test. Note that this time we pass thetestoption at the end of the command. This tells the script to load the variables from.env.testinstead of.env.
- 
Add a posttestscript, set the command to:node scripts/drop-database.js. This will delete your test database after your tests have finished running.
- 
Run npm test. If all goes well, then mocha will just let you know that there aren't any tests yet.