(Really) Testing your GraphQL application

Yannick Visbeek
MisterGreen Engineering
3 min readMar 18, 2019

As there are many ways of testing any application, there are many ways of testing a GraphQL server. If you want to add unit tests for example, it’s possible to mock a schema, resolvers and functions using Apollo’s graphql-tools. If you want to run integration tests on your server, it’s possible to mock an actual GraphQL server by using Apollo’s createTestClient function, which uses an actual server instance to test against, without actually starting an http-server.

At MisterGreen we make use of a microservice architecture, which in our case means that we have several GraphQL services — each with their own database where necessary — which are all tied together in our API gateway. In end-to-end testing our GraphQL applications we try to include every part of this system in order to cover any side-effects and errors that might arise throughout out the application.

How do we do this?

We created a test environment, which should be identical to our production environment except that it might include newly developed features. This test environment thus holds an actual schema, actual resolvers and an actual database (nothing is mocked!). When we run our tests (using Jest), these tests get run against an actual fully operating application (or not) that includes all other services we have in our landscape. So, a GraphQL application is not tested in isolation, but all effects throughout the landscape can be identified.

Some code

To start this project we need apollo-server-express, express and nodemon, so let’s add these dependencies to a new project.

yarn add apollo-server-express express & yarn add --dev nodemon

All other dependencies can be found in the package.json file in the github repository.

Let’s then set up a basic GraphQL server.

If we add the following scripts to our package.json we can easily run this application by executing “yarn serve”.

"scripts": {
"start": "ts-node ./src/index",
"serve": "nodemon yarn start"
},

When opening up the playground we can see that the allCars query is available, which will retrieve the list of cars.

Now that we have the server in place, let’s add some tests. For this, first we create a GraphQL client that will query the server.

For the tests we add a test folder in the project folder. In this folder we’ll create a cars.spec.ts file.

This will only test if the allCars GraphQL query we created returns an array and if the array includes the Tesla Model Y, but it’s easy to add more tests. Then, in order to run this test, the server we created earlier must be running as the client used by the test queries this server. The tests can be run by adding the following line to the scripts.

"test": "jest --runInBand --config jest.config.json"

Conclusions

In this blog we set up a really basic GraphQL server with a simple test that uses a client to test the server while it’s running. This way of testing then can easily become more powerful by including a test database in the service and connecting other (deployed?) GraphQL services. By doing this, you can spot errors throughout the system that you’re preparing to bring to production as close to its production state as possible!

The repository with the code used in this blog post can be found here:

If you have any questions or would like to share another method of end-to-end testing your GraphQL applications, let me know!

--

--