Dealing with complex tests
So far, we have seen simple test cases that did not require multiple API requests. So, let’s create the test/login.test.js
file to verify the sign-up and first user login to our application. We will use what we have learned so far, keeping in mind that we don’t want to replicate code.
We need to build the Fastify instance to write new test cases, as we did in the test/basic.test.js
file. To do so, we need to do the following:
- Create a new utility file and call it
test/helper.js
. - In the
test/helper.js
file, move thebuildApp
function and its configuration variables,startArgs
andenvParam
. This action requires some copying and pasting. - Update the
test/basic.test.js
file within the new import,const { buildApp } =
require('./helper')
.
By doing so, we can reuse code to instantiate the Fastify application across all the test files we are going to create. We are now ready to write more complex tests.