npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

jest-gql-mock-validator

v1.3.0

Published

A plugin for jest that validates mocked gql query responses against the defined gql queries

Readme

Jest GraphQL Mock Validator

A Jest utility for validating GraphQL mocks against your schema and simplifying GraphQL API testing by providing mockable query functionality.

Installation

To install the library, use npm or yarn:

npm install jest-gql-mock-validator --save-dev

or

yarn add jest-gql-mock-validator --dev

Setup

To enable the library in your Jest environment, you need to extend Jest with the custom matchers and mocks provided by jest-gql-mock-validator.

1. Configure Jest to use jest-gql-mock-validator

Update your jest.config.js to include the setup file:

module.exports = {
  setupFilesAfterEnv: ["jest-qgl-mock-validator"],
};

2. Configure schema via GraphQL-Config

This library uses graphql-config to manage and load your GraphQL schema. Ensure you have a valid graphql-config configuration file in your project root.

Supported Configuration Files

  • .graphqlrc
  • .graphqlrc.json
  • .graphqlrc.yaml or .graphqlrc.yml
  • graphql.config.js

Example .graphqlrc.yml

Create a .graphqlrc.yml file in your project root to specify the schema location:

schema: ./schema.graphql

Documentation

For more details on how to configure graphql-config, refer to the graphql-config documentation.


Usage

The library provides utilities for:

  1. Validating the JSON response against the provided GraphQL query when mocking a response.
  2. Independently validating a JSON object against a provided GraphQL query.

1. Validate mocked query or mutation responses

Use mockGqlQuery() to construct a mock in lieu of jest.fn() to allow the use of .mockResolvedGqlOnce() to validate the response against the provided GraphQL queries or mutations:

import { mockGqlQuery } from "jest-gql-mock-validator";
import { getUsers } from "./queries";

// construct a mock that enables .mockResolvedGqlOnce below
const mockSendQuery = mockGqlQuery();

// validate that the JSON response being provided to the user is valid for the provided gql query or mutation
mockSendQuery.mockResolvedGqlOnce(getUsers, {
  getUsers: [
    { id: "1", username: "Alice" },
    { id: "2", username: "Bob" },
  ],
});

2. Validate a JSON object against a gql query or mutation

Use the custom matcher expect().toMatchGqlMock() to validate a JSON object against the GraphQL query or mutation:

expect(response).toMatchGqlMock(query);

For example:

import { toMatchGqlMock } from "jest-gql-mock-validator";
import { getUsers } from "./queries";

const mockResponse = {
  getUsers: [
    { id: "1", username: "Alice" },
    { id: "2", username: "Bob" },
  ],
};

expect(mockResponse).toMatchGqlMock(getUsers);

Contributing

Contributions are welcome! To contribute:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-name).
  3. Commit your changes (git commit -m "Add feature").
  4. Push to your branch (git push origin feature-name).
  5. Open a pull request.

License

This library is licensed under the MIT License. See LICENSE for details.