Set up GraphQL with Fastify, Mercurius, TypeScript, and Automated Testing

Set up GraphQL with Fastify, Mercurius, TypeScript, and Automated Testing

In this short tutorial, I wanna show you how to set up a Fastify web server implementing GraphQL via the mercurius module, and adding the first automated test using supertest while using TypeScript.

In this short tutorial, I wanna show you how to set up a Fastify web server implementing GraphQL via the mercurius module, and adding the first automated test using supertest while using TypeScript.

To provide you some boilerplate for your future projects and as a reference for this article I created a small GitHub repository that includes all the code mentioned in this article, so make sure to clone it and have it open as a reference while reading this article.


First of all, we have a very simple file structure. On the root level, we have our config files like tsconfig.json.eslintrc.jsonjest.config.jsand our package.json. Right beneath we only have our src , dist and node_modules folders, so nothing really fancy here.

The whole source (in our case only one index.ts ) is located under the src folder and right beneath it we have a test folder containing our tests. Of course, you are absolutely free to change the structure to your likes. I for myself like it when I have one test file right beneath the actual implementation file it should test, even though I did a bit otherwise here.

javascript graphql programming testing nodejs

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Where To Learn Test Programming — July 2020 Edition

What do you do when you have lots of free time on your hands? Why not learn test programming strategies and approaches?

Rethinking Testing Through Declarative Programming

Write readable and maintainable tests .Rethinking Testing Through Declarative Programming

Nodejs Graphql testing Query using Graphiql #11

Full Playlist Graphql Github Link

How to Do API Testing?

API endpoint when you send the get request to that URL it returns the JSON response. In this article, I am going to use postman assertions for all the examples since it is the most popular tool. But this article is not intended only for the postman tool.


Here I show how to test your Javascript code. We will test pure functions (they are the easiest to test, because they don't have side effects). Then we test a function with side effects and we mock a global function using Jest. And finally we test an object method. We also talk about AAA (Arrange Act Assert) pattern here.