easygraphql-tester is node library created to make GraphQL tests based on the schema; it's used
to test the queries, mutations and schema on the easiest way possible.
It will check:
If the query/mutation/subscription is defined on the schema.
If the requested fields are defined.
If the arguments are valid.
If the input on a mutation is valid.
If the union is valid.
And much more....
Installation
To install the package on your project just run on the root of your project
$ npm install easygraphql-tester --save-dev
easygraphql-tester can be used in two ways; the first one is using .tester as an assertion of the query/mutation, and the second one is using .mock to return the mocked query/mutation.
After you initializate the class, you can use the method graphql and it'll receive
4 arguments, the only one that is required is the first argument, those arguments are:
query: The query/mutation you want to test.
rootValue: It's going to be the rootValue to pass to the resolver.
contextValue: It's going to be the context to pass to the resolver.
variableValues: It's going to be the variables that the query/mutation are going to use.
easygraphql-tester works as an assertion library used to make tests with your favorite test runner.
To use it as an assertion library, you must follow the next steps:
Define a Query or Mutation to test.
Pass as first argument, a boolean to .test(true) or .test(false).
true: if it is fine, everything should work fine.
false: if it should fail, there is an error or invalid field on the query/mutation or arguments/input.
Pass as second argument, the query/mutation to test.
The third argument is required if it is a mutation, it must be an object with the fields of the input
The next example is going to be made with mocha, but it can be done with your favorite test runner.
Mocha example
'use strict'constfs=require('fs')constpath=require('path')constEasyGraphQLTester=require('../lib')constuserSchema=fs.readFileSync(path.join(__dirname,'schema','user.gql'),'utf8')constfamilySchema=fs.readFileSync(path.join(__dirname,'schema','family.gql'),'utf8')describe('Test my queries, mutations and subscriptions',()=>{lettesterbefore(()=>{tester=newEasyGraphQLTester([userSchema,familySchema])})describe('Should pass if the query is invalid',()=>{it('Invalid query getUser',()=>{constinvalidQuery=` { getUser { id invalidField familyInfo { father { email username } } } } `// First arg: false, there is no invalidField on the schema.tester.test(false,invalidQuery)})it('Should pass if the query is valid',()=>{constvalidQuery=` { getMeByTestResult(result: 4.9) { email } } `tester.test(true,validQuery)})it('Should pass if the mutation is valid',()=>{constmutation=` mutation UpdateUserScores($scores: ScoresInput!) { updateUserScores(scores: $scores) { email scores } } `tester.test(true,mutation,{scores: {scores: [1,2,3]}})})it('Should not pass if one value on the mutation input is invalid',()=>{constmutation=` mutation UpdateUserScores($scores: ScoresInput!) { updateUserScores(scores: $scores) { email scores } } `// First arg: false, there is no invalidField on the schema.tester.test(false,mutation,{scores: {scores: [1],invalidField: true}})})it('Should search',()=>{constquery=` { search(id: "1") { ... on User { id } ... on FamilyInfo { id father { username } brothers { username } } } } `tester.test(true,query)})it('Should test a subscription',()=>{constsubscription=` subscription { newUsers(limit: 1) { id username email } } `tester.test(true,subscription)})})})
Mocking Queries and Mutations
easygraphql-tester can works 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, it'll be an object with the key data and inside it
the name of the query/mutation/subscription and the fields to set.
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.
validateDeprecated: If you want to validate if the query is requesting a deprecated field, set this option to true
and it'll return an error if a field is deprecated.
mockErrors: If you want to mock the errors instead of throwing it, set this option to true and now, the responsw will have
{ data: ..., errors: [...] }
The result will have top level fields, it means that the result will be an object
with a property that is going to be data and inside it the name (top level field)
of the query or alias with the mocked result.
In case you have a custom scalar, set the value on the fixture, if it's not set it will be {}
Fixtures:
There are two ways to set the fixture on a operation:
Operation options:
Set the fixture as an option when testing a query/mutation/subscription
Also, the fixture can be set before the test using .setFixture() method from the constructor,
it'll receive two arguments; the first one is going to be the fixture, and the second one will be
an object of options to set if it should auto mock the extra fields that are on the query but are not
on the fixture, by default it's true.
Run tester.clearFixture() to return the fixture to null and autoMock = true in case you
set it to false