easygraphql-tester is a Node.js library designed to facilitate testing of GraphQL schemas, queries, and mutations. Currently at stable version 6.0.1, its release cadence is active but irregular, typically aligning with updates to the GraphQL specification or bug fixes. It distinguishes itself by offering both assertion-based testing (validating schema adherence, field existence, argument types, and input validity) and mocking capabilities, allowing developers to generate fake data for operations without a live GraphQL server. This enables comprehensive unit testing of GraphQL logic and schema definitions. It supports single or multiple schema files, direct `graphql-js` schema objects, and integrates resolvers for more complete backend testing. The library is part of the broader EasyGraphQL suite, which includes tools for load testing, schema mocking, and server creation.
npm install easygraphql-testerVerified import paths — ran on the pinned version, not inferred.
This example demonstrates initializing `easygraphql-tester` with a GraphQL schema and resolvers, then executing a valid query and a mutation using the `graphql` method to test resolver logic and schema adherence.
Review the `graphql` peer dependency specified in `package.json` for `easygraphql-tester` and update your `graphql` package accordingly. You may also need to adjust error handling logic.
Upgrade your Node.js environment to version 8 or higher. Ensure all resolver functions are either `async` or return Promises if they perform asynchronous operations.
Initialize the tester with your schema and resolvers: `new EasyGraphQLTester(schema, resolvers);`.
Consult the official documentation for the precise usage of `.tester(isValid: boolean, query: string, variables?: object)` for assertions and `.mock({ query: string, variables?: object, fixture?: object, saveFixture?: boolean })` for mocking.Ensure that your union types are correctly defined in the schema and that any mock data or fixtures provided for union fields specify the `__typename` and conform to one of the union's member types.
For CommonJS, use `const EasyGraphQLTester = require('easygraphql-tester');`. For TypeScript/ESM, ensure you're importing the default export: `import EasyGraphQLTester from 'easygraphql-tester';` or `import * as EasyGraphQLTester from 'easygraphql-tester';` (though the former is usually sufficient).Double-check the GraphQL schema definition for the existence and correct spelling of the queried operation. Verify the query string for typos.
Review your `.gql` or `.graphql` files for type definitions. Ensure all custom types, inputs, enums, and scalars are defined. If using multiple schema files, confirm they are all passed to the `EasyGraphQLTester` constructor in an array and correctly extend types if necessary.
Carefully inspect the GraphQL string for syntax issues such as missing commas, incorrect field names, invalid directives, or malformed arguments. A GraphQL linter can help identify these issues.