How I started testing my Queries and Mutations on GraphQL š
Some months ago I was creating a personal project using GraphQL but when I was going to test my actual Queries and Mutations, I found that it will be my first time doing GraphQL testsā¦ So, was there when I started doing some research about doing tests on GraphQL and the result was not the expected, there where few packages that can help me with that, but those packages where not what I was expecting.
Then, I decided to create easygraphql which is an open source organization of free GraphQL tools that can help the community solve the same issues that I was having.
Testing
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.
Thatās why, I created easygraphql-tester
which is a package that will help you create UT of your Schema, Queries, and Mutations in an easy way!
How to use it?
First steps:
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 (Also, you can read multiple GraphQL files and initialize the tester with an array of schemas).
- Initialize the tester and pass the schema/schemas to it.
const fs = require("fs");
const path = require("path");
const EasyGraphQLTester = require("easygraphql-tester");
const schemaCode = fs.readFileSync(path.join(__dirname, "schema.gql"), "utf8");
const tester = new EasyGraphQLTester(schemaCode);
Assertion:
easygraphql-tester
works as an assertion library used to make tests with your favorite test runner (The tests used in this case are using Jest as test runner).
To use it as an assertion library, you must follow the next steps:
- Define a Query or Mutation.
- Pass as first argument a boolean to
tester.test(true, query/mutation)
ortester.test(false, query/mutation)
if the query/mutation to test is valid or not. - 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 input fields.
"use strict";
const fs = require("fs");
const path = require("path");const EasyGraphQLTester = require("easygraphql-tester");const schemaCode = fs.readFileSync(path.join(__dirname, "..", "schema.gql"), "utf8");describe("Test my schema", () => {
let tester; beforeAll(() => {
tester = new EasyGraphQLTester(schemaCode);
});describe("Queries & Mutations", () => {
test("notValidField is invalid on query", () => {
const query = `
{
getUsers {
notValidField
}
}
`; // First arg: false, there is no field notValidField
// Second arg: query to test
tester.test(false, query);
});
test("Should get all the fields on getUsers", () => {
const query = `
{
getUsers {
username
fullName
phone
}
}
`; // First arg: true, the query is valid
// Second arg: query to test
tester.test(true, query);
}); test("Should be a valid mutation", () => {
const mutation = `
mutation CreateUsers {
createUsers {
}
}
`; // First arg: true, the mutation is valid
// Second arg: mutation to test
// Third argument: Input value tester.test(true, mutation, [{
email: "demo@demo.com",
username: "demo",
fullName: "demo name"
}]);
});
});
});
If the first argument on tester.test(true/false)
is the correct value (boolean) the test will pass or not.
Mock:
Also, you can use easygraphql-tester
to mock your queries and mutations; letting you test the result with any assertion package.
tester.mock(query/mutation)
will return a mock of the query/mutation; if a field is invalid it will return an error.
"use strict";const fs = require("fs");
const path = require("path");
const EasyGraphQLTester = require("easygraphql-tester");const schemaCode = fs.readFileSync(path.join(__dirname, "..", "schema.gql"), "utf8");describe("Test my schema, Mock", () => {
let tester;
beforeAll(() => {
tester = new EasyGraphQLTester(schemaCode);
}); describe("Mock", () => {
test("Should return mocked fields of schema", () => {
const query = `
{
getUsers {
username
fullName
phone
}
}
`; const mockedResult = tester.mock(query); // You can use something like chai and validate that the
// mockedResult returns the type of data you expected console.log("Mocked result: ", mockedResult);
});
});
});
Errors:
If there is an error on the query or mutation easygraphql-tester
will let you know what is happening, here are some few cases.
Trying to access an invalid field id on getUsers:
const fs = require("fs");
const path = require("path");
const EasyGraphQLTester = require("easygraphql-tester");tester.test(true, query) // Error: Invalid field id on getMe"use strict";
const fs = require("fs");
const path = require("path");const EasyGraphQLTester = require("easygraphql-tester");const schemaCode = fs.readFileSync(path.join(__dirname, "..", "schema.gql"), "utf8");describe("Test my schema", () => {
let tester; beforeAll(() => {
tester = new EasyGraphQLTester(schemaCode);
}); describe("Queries & Mutations", () => {
test("First arg is false, and the query is invalid", () => {
const query = `
{
getUsers {
notValidField
}
}
`; //Error: Invalid field notValidField on getUsers
tester.test(false, query);
});
});
});
Missing field on input
"use strict";
const fs = require("fs");
const path = require("path");const EasyGraphQLTester = require("easygraphql-tester");const schemaCode = fs.readFileSync(path.join(__dirname, "..", "schema.gql"), "utf8");describe("Test my schema, Query", () => {
let tester; beforeAll(() => {
tester = new EasyGraphQLTester(schemaCode);
}); describe("Queries & Mutations", () => {
test("Should be a valid mutation", () => {
const mutation = `
mutation CreateUsers {
createUsers {
}
}
`; //Error: fullName argument is missing on createUsers
tester.test(true, mutation, [{
email: "demo@demo.com",
username: "demo"
}]);
});
});
});
If you like this post and easygraphql-tester
donāt forget to give an āļø on GitHub repo.
Repo: https://github.com/EasyGraphQL/easygraphql-tester
Demo with Jest: https://codesandbox.io/s/42m2rx71j4