This section is about how to add test code on resolvers we’ve implemented earlier in the previous post
I’ll focus on gallery.test.ts
since it is related to the previous post.
It should CRUD
galleries with resolvers. Therefore, I’ve created four resolvers earlier.
- galleries (query)
- createGallery (mutation)
- updateGallery (mutation)
- deleteGallery (mutation)
Since above resolvers need the user to be signed in, in order to complete the request, I’ve written signUp
mutation query in beforeAll
life cycle in jest.
With the token provided in the result, we will put this into request header as shown below in beforeAll
function.
I’ll use above client
to test other resolvers. First, let’s look at how I’ve tested galleries
resolvers.
Since the user hasn’t created any gallery yet, the resolver will return an empty array which I expect as a result.
Now let’s look at the below resolvers which are very much similar.
Let’s run jest to see if the tests cover every line of code.
yarn test --coverage ./tests/gallery.test.ts
There is one uncovered line which is line 8.
To cover the above line, I should add case on when photoURL
doesn’t start with http
.
Let’s run below command again and see if I can achieve 100% coverage.
yarn test --coverage ./tests/gallery.test.ts
We’ve finally made it 🎉. Although I can improve the test code more firmly, I’ve currently finished up the minimal requirement for testing our resolvers.
Let’s keep up our work on making resolvers for HackaTalk server :)
Thank you for reading!