Testing GraphQL Server in Node.js
When we started using GraphQL in our NodeJS project, we struggled with writing tests. So, we made one ourselves. Here, we share our way of testing GraphQL queries.
Join the DZone community and get the full member experience.
Join For FreeWhen we started using GraphQL in our Node.js project, 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. Here, we'll share our way of testing GraphQL queries.
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 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 the .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 the SailsJS + GraphQL app!
Let's define a GraphQL schema that 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!
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 that will be triggered for every query, so our client app (we're using React) has an endpoint to make requests to, 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, the chai library provides much more options to check out.
With this, we come to the end of this article.
Hope you enjoyed it and found it informative!
Published at DZone with permission of Nesha Zoric, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments