Start testing your GraphQL Schema, Queries and, Mutations!

Written by estrada9166 | Published 2018/08/15
Tech Story Tags: javascript | graphql | graphql-schema | graphql-queries | graphql-mutations

TLDRvia the TL;DR App

Making some test of your code is always a good practice that you should implement. The tests that you do will help you prevent some bugs and also it will ensure that your app work as you think it should work.

Sometimes making tests can be difficult and will require a lot of code, but most of the times it depends on the implementation that you’re using to test your code; there are packages that can help you make the tests with a few lines of code.

Today I’m going to introduce [easygraphql-tester](https://github.com/EasyGraphQL/easygraphql-tester) which is a npm package that will help you test your Schema, Queries, and Mutations in the server side and also in the client side.

How to use it?

Using [easygraphql-tester](https://github.com/EasyGraphQL/easygraphql-tester) doesn’t need a lot of extra code to test your Schema, Queries, and Mutations.

First steps:

  • Import the package [easygraphql-tester](https://github.com/EasyGraphQL/easygraphql-tester)
  • Read the GraphQL schema
  • Initialize the tester and pass the schemaCode to it

const EasyGraphQLTester = require('easygraphql-tester')const fs = require('fs')const path = require('path')

const schemaCode = fs.readFileSync(path.join(__dirname, 'schema.gql'), 'utf8')

const tester = new EasyGraphQLTester(schemaCode)

Test a Query:

  • Set the query as a const
  • Call test from tester pass as first argument if the test should pass, as second argument the mutation and as a third one the variables that the input are expecting

const EasyGraphQLTester = require('easygraphql-tester')const fs = require('fs')const path = require('path')

const schemaCode = fs.readFileSync(path.join(__dirname, 'schema', 'schema.gql'), 'utf8')const tester = new EasyGraphQLTester(schemaCode)

const query = `{getMe {idemailfamilyInfo {father {email}mother {username}}}}`

tester.test(true, query)

If the query is success, it will return a mock of the fields that you requested. The tester also is going to validate the arguments in case the query is expecting some and the type of the argument.

Test a Mutation:

  • Set the mutation as a const
  • Call test from tester pass as first argument if the test should pass, as second argument the mutation and as a third one the variables that the input are expecting

const EasyGraphQLTester = require('easygraphql-tester')const fs = require('fs')const path = require('path')

const schemaCode = fs.readFileSync(path.join(__dirname, 'schema', 'schema.gql'), 'utf8')const tester = new EasyGraphQLTester(schemaCode)

const mutation = `mutation CreateUser{createUser {email}}`

tester.test(true, mutation, {email: 'test@test.com',username: 'test',fullName: 'test',password: 'test'})

If the query is success, it will return a mock of the fields that you requested on the mutation.

Mocking Queries and Mutations:

[easygraphql-tester](https://github.com/EasyGraphQL/easygraphql-tester) can work as a mocker of your query or mutation, using it is simple.

Call the method .mock() and pass an object with this options:

  • query: It’ll be the query/mutation to test.
  • variables: This is required if it is a mutation, it must be an object with the fields of the input.
  • fixture: This is optional and it is if you want to pass your custom fixtures.
  • saveFixture: By default is false, if you pass fixtures, and set it to true when you make the same query again, it will return the fixture value.

The result will have top-level fields, it means that the result will be an object with a property that is going to be the name (top level field) of the query or alias with the mocked result.

Mock example

'use strict'

const EasyGraphQLTester = require('easygraphql-tester')const fs = require('fs')const path = require('path')

const userSchema = fs.readFileSync(path.join(__dirname, 'schema', 'user.gql'), 'utf8')

const tester = new EasyGraphQLTester(userSchema)

const query = `{getUser(id: "1") {idnamefamilyInfo {lastNameemail}}}`

const fixture = {id: '1',name: 'EasyGraphQL'}

const { getUser } = tester.mock({ query, fixture })

// getUser{id: '1',name: 'EasyGraphQL',familyInfo: [{lastName: 'Bartoletti',email: 'YSjsYuV@wtnK.com'},{lastName: 'Bartoletti',email: 'YSjsYuV@wtnK.com'},{lastName: 'Bartoletti',email: 'YSjsYuV@wtnK.com'}]}

Errors:

If there is an error on the query or mutation [easygraphql-tester](https://github.com/EasyGraphQL/easygraphql-tester) will let you know what is happening.

Trying to access an invalid field id on getMe -> father

const EasyGraphQLTester = require('easygraphql-tester')const fs = require('fs')const path = require('path')

const schemaCode = fs.readFileSync(path.join(__dirname, 'schema', 'schema.gql'), 'utf8')const tester = new EasyGraphQLTester(schemaCode)

const query = `{getMe {idemailfamilyInfo {father {idemail}}}}`tester.test(true, query) // Error: Invalid field id on getMe

Invalid arguments on query

const EasyGraphQLTester = require('easygraphql-tester')const fs = require('fs')const path = require('path')

const schemaCode = fs.readFileSync(path.join(__dirname, 'schema', 'schema.gql'), 'utf8')const tester = new EasyGraphQLTester(schemaCode)

const getUserByUsername = `{getUserByUsername(username: 1, name: test) {email}}`

tester.test(true, getUserByUsername) // Error: username argument is not type String

Missing field on input

const EasyGraphQLTester = require('easygraphql-tester')const fs = require('fs')const path = require('path')

const schemaCode = fs.readFileSync(path.join(__dirname, 'schema', 'schema.gql'), 'utf8')const tester = new EasyGraphQLTester(schemaCode)

const mutation = `mutation CreateUser{createUser {email}}`const test = tester.test(true, mutation, {email: 'test@test.com',fullName: 'test',password: 'test'})// Error: username argument is missing on createUser

Using with Mocha & Chai:

To get better results on your test you can use it with Mocha & Chai (also you can use it with your favorite ones) to test the results and validate the fields that are returning.

'use strict'

const fs = require('fs')const path = require('path')const { expect } = require('chai')const EasyGraphQLTester = require('../lib')

const schemaCode = fs.readFileSync(path.join(__dirname, 'schema', 'schema.gql'), 'utf8')

describe('Mutation', () => {let tester

before(() => {tester = new EasyGraphQLTester(schemaCode)})

describe('Should throw an error if variables are missing', () => {it('Should throw an error if the variables are missing', () => {let errortry {const mutation = `mutation CreateUser{createUser {email}}`tester.mock(mutation)} catch (err) {error = err}

  expect(error).to.be.an.instanceOf(Error)  
  expect(error.message).to.be.eq('Variables are missing')  
})  

})

describe('Should return selected fields', () => {it('Should return selected fields', () => {const mutation = `mutation CreateUser{createUser {email}}`const test = tester.mock(mutation, {email: 'test@test.com',username: 'test',fullName: 'test',password: 'test'})

  expect(test).to.exist  
  expect(test.email).to.be.a('string')  
})  

})})

If you like this package don’t forget to give a ⭐️ on GitHub

Demo with mocks: https://repl.it/@alejandroestrada/easygraphql-tester

Demo with Jest: https://codesandbox.io/s/42m2rx71j4

Repo: https://github.com/EasyGraphQL/easygraphql-tester

npm: https://www.npmjs.com/package/easygraphql-tester

Website: https://easygraphql.com/


Published by HackerNoon on 2018/08/15