Skip to content

Katarzyna Kmiotek

Testing GraphQL (Playground, Postman, Supertest/Chai)

GraphQL, Postman, Supertest, API testing2 min read

Recently I started my personal project to learn how to build and test GraphQL server. The backend is ready and deployed. Now can focus on testing it.

The endpoint that I am using is the collection of teas has few basic methods available ( teas, teaByid, producers, addTea. deleteTea etc) that are ready to be tested.

Testing with Playground

The easiest and provided by default from GraphQL endpoint (using Apollo Server) testing tool is GraphQL IDE - Playground . It is interface allowing user to test queries, mutations and subscriptions. Intelligent features of Playground allow quickly identify issue with query (syntax, real-time error highlighting), accessing endpoint documentation and schema. Allows work on multiple tabs so working on different test cases. Playground allows also testing with variables (convenient way for developers to build query this way and then just copy it to code base), HTTP headers etc.
Playground IDE: Playground IDE Playground errors Playground real-time error One of my favourite Playground features is keyboard shortcut CTRL + Space allowing access all available commands based on your current context.

Playground IDE is available on local development on localhost:4000/graphql or can be also enabled in production by passing “playground: true “ to server instance as on a GraphQl server I was working on.
playground set to true

Testing with Postman

Of course that Postman has a feature allowing testing GraphQL!
If you are familiar with Postman and testing REST endpoints there will be just few adjustments to write test cases for GraphQL endpoint. All requests (query, mutation) use POST method, all to the same endpoint ; the body of message uses GraphQL schema .
Postman same as Playground allows using variables and the same as in testing REST endpoint authorisation, HTTP headers can be added. Same as in Playground CTRL + Space shortcut is available.
Postman view

If not custom error codes are configured in GraphQL server the unsuccessful query will return 200 status code anyway so it’s advisable to avoid adding this standard test case and focus on response body.
Postman test

And the best GraphQL Postman collection runner can be run in seconds same as REST endpoint collection, works with Newman so also easy executed in Docker.

Testing with Supertest, Chai-Graphql, Jest

It was my first experience with using Supertest - what a great tool it is! I was using Chai (Chai-graphql) assertion library and Jest as a test runner.
The easiest way to start project is to run

1npm init -y
2npm i -D jest supertest chai chai-graphql

add script in package.json

1"scripts": {
2 "test": "jest"
3 },

In a file (teas.test.js) add set up:
setup I am using Supertest POST request and send GraphQL query (copied from Playground) query Can use variables and detailed check response body response body Can also post mutation (add and delete request) add delete And because the endpoint doesn't have custom error codes configured when send request that is not resolved and returns empty data array still get 200 status code. 200 status code

There was lots of console.log on a way!


Testing GraphQL with Rest Assured

© 2024 by Katarzyna Kmiotek. All rights reserved.
Theme by LekoArts