Testing GraphQL Server in NodeJS

When [Kolosek team started using GraphQL in our NodeJS project](https://kolosek.com/sails-graphql-guide/), we had a struggle with writing tests: we read numerous blogs, searching for the best way. Unfortunately, there didn't seem to be one, so we made one ourselves and here we'll share our way of testing GraphQL queries.

This article will help you test your GraphQL server using **[Mocha](https://mochajs.org/#getting-started)**, **[Chai](http://chaijs.com/api/)**, **[supertest](https://github.com/visionmedia/supertest)** and **[graphql](http://graphql.org/graphql-js/)**.

First, we will set up everything needed for running tests:

    npm i --save-dev mocha chai supertest graphql

Now that we have everything installed we can go ahead and **import all the dependencies** we will use in our *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](https://kolosek.com/rails-rspec-setup/) for testing!*

Before running any test we will have to add a command to our package.json "scripts":

      "scripts": {
          "test": "mocha test/*.test.js"
       }
       
This will tell **Mocha** to test all the files with *.test* extension in our test folder. To run the test just type: 

    npm test

It will say **0 passing** since we haven't written any test cases yet.

*Follow our [organizational guidelines for SailsJS+GraphQL](https://kolosek.com/sails-graphql-guide/) app!*

Let's define a graphql schema which we will use to write tests. For instance, here we'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
         }
    });
*We're organizing our tests the similar way we organize [RSpec controller tests](https://kolosek.com/rspec-controller-test/)!*

Let's define the query field that will return users with the fields we 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 us to be able to query users, we 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 we start writing the tests is to create a universal  GraphQL controller which will be triggered for every query, so our client app ([we're using React](https://kolosek.com/react-first-steps/)) has an endpoint to make request too, and our 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 we have defined our User type, defined the query object for it and included it in our GraphQLSchema, we're all set to **write tests**.

Let's assume that there are some users in our database, the only thing left is to send requests with *supertest* to our *GraphQLController* and to output the results of our 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](http://chaijs.com/api/) to check out.
 
With this, we came to an end of this article.

Hope you enjoyed it and found it informative!

This article is originally published on [Kolosek Blog](https://kolosek.com/testing-graphql-server/).