Adding test code to graphql resolver in HackaTalk server

Hyo
3 min readFeb 16, 2020

--

This section is about how to add test code on resolvers we’ve implemented earlier in the previous post

Figure 1: Current status for testing result

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.

Figure 2: Writing mutation query for signUp

With the token provided in the result, we will put this into request header as shown below in beforeAll function.

Figure 3: Adding user token to authorization header

I’ll use above client to test other resolvers. First, let’s look at how I’ve tested galleries resolvers.

Figure 4: Testing galleries query resolver

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.

Figure 5: Testing createGallery mutation resolver
Figure 6: Testing updateGallery mutation resolver
Figure 7: Testing deleteGallery mutation resolver

Let’s run jest to see if the tests cover every line of code.

yarn test --coverage ./tests/gallery.test.ts
Figure 8: Coverage for current status

There is one uncovered line which is line 8.

Figure 9: Uncovered line 8

To cover the above line, I should add case on when photoURL doesn’t start with http.

Figure 10: Test case to cover the uncovered line

Let’s run below command again and see if I can achieve 100% coverage.

yarn test --coverage ./tests/gallery.test.ts
Figure 11: Final testing coverage

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!

--

--

No responses yet