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.
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:
[easygraphql-tester](https://github.com/EasyGraphQL/easygraphql-tester)
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:
const
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:
const
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: '[email protected]',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.
[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:
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.
'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: '[email protected]'},{lastName: 'Bartoletti',email: '[email protected]'},{lastName: 'Bartoletti',email: '[email protected]'}]}
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: '[email protected]',fullName: 'test',password: 'test'})// Error: username argument is missing on createUser
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: '[email protected]',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:
Repo: https://github.com/EasyGraphQL/easygraphql-tester
npm: https://www.npmjs.com/package/easygraphql-tester
Website: https://easygraphql.com/