When Kolosek team first started using GraphQL in the NodeJS project, there was a struggle with writing tests: the team read numerous blogs, searching for the best way. Unfortunately, there didn’t seem to be one, so the team decided to make one themselves and here it is.
This article will help you test your GraphQL server using Mocha, Chai, SuperTest and GraphQL.
First, let’s set up everything needed for running tests:
npm i --save-dev mocha chai supertest graphql
Now that you have everything installed you can go ahead and import all the dependencies you will use in the User.test.js file:
const chai = require('chai');
const expect = chai.expect;const url = `http://localhost:3001/`;const request = require('supertest')(url);
describe('GraphQL', () => { // Tests});
Setup is even easier than setting up RSpec for testing!
Before running any test you will have to add a command to your package.json “scripts”:
"scripts": { "test": "mocha test/*.test.js" }
This will tell Mocha to test all the files with .test extension in your test folder. To run the test just type:
npm test
It will show 0 passing since you haven’t written any test cases yet.
Let’s define a graphql schema which you will use to write tests. For instance, here you’ll create a User type with some basic information about the user in UserType.js file:
const graphql = require('graphql');
export default new graphql.GraphQLObjectType({name : 'UserType', fields : { id : { type : graphql.GraphQLInt }, name : { type : graphql.GraphQLString }, username:{ type: graphql.GraphQLString }, email : { type : graphql.GraphQLString }});
Kolosek team organizes tests the similar way it organizes RSpec controller tests!
Let’s define the query field that will return users with the fields you defined just now:
const graphql = require('graphql');const UserType = require('./UserType').default;
export default { user: { description: 'Returns information about user/s', type: new graphql.GraphQLList(UserType), args: { id: { type: graphql.GraphQLInt }, }, resolve: async (_, { id }) => { if (id) return User.find({ id }) return User.find() } },
For you to be able to query users, you will have to define GraphQL schema:
const graphql = require('graphql');const UserQuery = require('./UserQuery');
export default new graphql.GraphQLSchema({ name: 'Query', fields: UserQuery})
The last thing to do before you start writing the tests is to create a universal GraphQL controller which will be triggered for every query, so the client app (you are using React) has an endpoint to make request too, and your tests too.
const graphql = require('graphql');const schema = require('./schema').default;
module.exports = { graphql: async (req, res) => { try { const result = await graphql(schema, req.body.query, req); if (result.errors) throw (result.errors); return res.ok(result); } catch (err) { return res.badRequest(err); } },
Now that you have defined the User type, defined the query object for it and included it in the GraphQLSchema, you’re all set to write tests.
Let’s assume that there are some users in your database, the only thing left is to send requests with supertest to your GraphQLController and to output the results of your query:
const chai = require('chai');
const expect = chai.expect;const url = `http://localhost:3001/`;const request = require('supertest')(url);
describe('GraphQL', () => { it('Returns user with id = 10', (done) => { request.post('/graphql') .send({ query: '{ user(id: 10) { id name username email } }'}) .expect(200) .end((err,res) => { // res will contain array with one user if (err) return done(err); res.body.user.should.have.property('id') res.body.user.should.have.property('name') res.body.user.should.have.property('username') res.body.user.should.have.property('email') done(); }) })
it('Returns all users', (done) => { request.post('/graphql') .send({ query: '{ user { id name username email } }' }) .expect(200) .end((err, res) => { // res will contain array of all users if (err) return done(err); // assume there are a 100 users in the database res.body.user.should.have.lengthOf(100); }) })});
Of course, Chai library provides much more options to check out.
With this, we came to an end of this article.
Hope you enjoyed it and found it informative!
Originally published at kolosek.com on April 23, 2018.