Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
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 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 doesn’t need a lot of extra code to test your Schema, Queries, and Mutations.
First steps:
- Import the package 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 the 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 query = ` { getMe { id email familyInfo { father { email } mother { username } } } }`
tester.test(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 the mutation and as a second 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(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.
Errors:
If there is an error on the query or mutation 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 { id email familyInfo { father { id email } } } }`tester.test(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(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(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 error try { const mutation = ` mutation CreateUser{ createUser { email } } ` tester.test(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.test(mutation, { email: 'test@test.com', username: 'test', fullName: 'test', password: 'test' }) expect(test).to.exist expect(test.email).to.be.a('string') }) })})
Here are some links that can be useful:
Demo: https://repl.it/@alejandroestrada/easygraphql-tester
Repo: https://github.com/EasyGraphQL/easygraphql-tester
npm: https://www.npmjs.com/package/easygraphql-tester
Start testing your GraphQL Schema, Queries and, Mutations! was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.