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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@shopify/graphql-testing

v8.2.1

Published

Utilities to create mock GraphQL factories

Downloads

152,264

Readme

@shopify/graphql-testing

Build Status Build Status License: MIT npm version

This package provides utilities to help in the following testing scenarios:

  1. Testing a GraphQL operation with mock data
  2. Testing the state of your application before/after all the GraphQL operations resolve

Installation

yarn add @shopify/graphql-testing

Usage

The default utility exported by this library is createGraphQLFactory. This factory accepts an optional options argument that allows you to pass a cacheOptions that will be used to construct an Apollo in-memory cache and/ or links which can contain ApolloLinks that will be passed to the apollo client links.

const createGraphQL = createGraphQLFactory({
  cacheOptions: {
    possibleTypes: {},
  },
});

The resulting function can be used to create a GraphQL controller that tracks and resolves GraphQL operations according to the mocks you supply. The mock you provide should be an object where the keys are operation names, and the values are either an object to return as data for that operation, or a function that takes a GraphQLRequest and returns suitable data. Alternatively, the mock can be a function that accepts a GraphQLRequest and returns suitable mock data.

const graphQL = createGraphQL({
  Pet: ({variables: {id}}) => ({pet: {id, name: 'Garfield'}}),
  Pets: () => ({pets: []}),
});

The call to the function returned by createGraphQLFactory (createGraphQL in the example above) creates a GraphQL instance, which is described in detail below.

GraphQL

The following method and properties are available on the GraphQL object:

resolveNext()

Note: Prefer graphQL.resolveAll() over this for almost all use cases.

By default, the mock client will hold all the graphQL operations triggered by your application in a pending state. To resolve all pending graphQL operations, call graphQL.resolveAll(), which returns a promise that resolves once all the operations have completed.

await graphQL.resolveNext();

You can also pass a query, mutation or filter option to resolveNext. query and mutation will filter the pending operations and only resolve the ones with a matching operation. filter allows you to write your own custom filter function based upon the operation, for instace this will allow you to filter by the variables passed to the query. If filter is passed in addition to query or mutation then both filters shall be applied.

await graphQL.resolveNext({query: petQuery});
await graphQL.resolveNext({
  query: petQuery,
  filter: (operation) => operation.variables.id === '1',
});

Note that, until a GraphQL operation has been resolved, it does not appear in the operations list described below.

resolveAll()

Similar to resolveNext(), but this will wait for any newly created GraphQL operations to finish. This is useful in the cases that one graphql call is loaded, which kicks off another GraphQL query/mutation.

The signature for resolveAll is identical to the signature for resolveNext.

await graphQL.resolveAll();

waitForQueryUpdates()

Apollo 3.6.0+ batches updates to the cache asynchronously. This means that in cases where you trigger fetching more data using fetchMore often the cache is persisted in the next tick and thus the component making the query does not update in the expected act block. This can result in flaky tests where you request data, and use resolveAll to fetch it, but the React component under test does not update.

The waitForQueryUpdates() method shall ensure all cache updates are resolved.

wrap()

The wrap() method allows you to wrap all calls to resolveAll() and waitForQueryUpdates() in a function call. This can be useful when working with React components, which require that all operations that lead to state changes be wrapped in an act() call. The following example demonstrates using this with @shopify/react-testing:

const myComponent = mount(<MyComponent />);
const graphQL = createGraphQL(mocks);

graphQL.wrap((resolve) => myComponent.act(resolve));

// Before, calling this could cause warnings about state updates happening outside
// of act(). Now, all GraphQL resolutions are safely wrapped in myComponent.act().
await graphQL.resolveAll();

update()

The update() method updates mocks after they have been initialized:

const myComponent = mount(<MyComponent />);
const newName = 'Garfield2';
const graphQL = createGraphQL({
  Pet: {
    pet: {
      __typename: 'Cat',
      name: 'Garfield',
    },
  },
});

graphQL.wrap((resolve) => myComponent.act(resolve));
await graphQL.resolveAll();

graphQL.update({
  Pet: {
    pet: {
      __typename: 'Cat',
      name: newName,
    },
  },
});

const click = myComponent.find('button').trigger('onClick');
await graphQL.resolveAll();
await click;

expect(myComponent).toContainReactText(newName);

#operations

graphQL.operations is a custom data structure that tracks all resolved GraphQL operations that the GraphQL controller has performed. This object has first(), last(), all(), and nth() methods, which allow you to inspect individual operations. All of these methods also accept an optional options argument, which allows you to narrow down the operations to specific queries or mutations:

const graphQL = createGraphQL(mocks);

// the very first operation, or undefined if no operations have been performed
graphQL.operations.first();

// the second last operation run with petQuery
graphQL.operations.nth(-2, {query: petQuery});

// the last operation of any kind
graphQL.operations.last();

// all mutations with this mutation
graphQL.operations.all({mutation: addPetMutation});

The query and mutation options both accept either a regular DocumentNode, or an async GraphQL component created with @shopify/react-graphql’s createAsyncQueryComponent function.

Errors

You can use createGraphQL to test error states.

Network Errors

You can test network errors by throwing an Error.

const graphQL = createGraphQL({
  Pet: () => {
    throw new Error('Network Error');
  },
});

GraphQL Errors

You can test the graphql api returning an error state by returning an Error or GraphQLError.

const graphQL = createGraphQL({
  Pet: new Error('Error message'),
});
import {GraphQLError} from 'graphql';
const graphQL = createGraphQL({
  Pet: new GraphQLError('Error message'),
});

Matchers

This library provides a Jest matcher. To use this matcher, you’ll need to include @shopify/graphql-testing/matchers in your Jest setup file. The following matcher will then be available:

toHavePerformedGraphQLOperation(operation: GraphQLOperation, variables?: object)

This assertion should be run on a GraphQL object. It verifies that at least one operation matching the one you passed (either a DocumentNode or an async query component from @shopify/react-graphql) was completed. If you pass variables, this assertion will also ensure that at least one operation had matching variables. You only need to provide a subset of all variables, and the assertion argument can use any of Jest’s asymmetric matchers.

const graphQL = createGraphQL(mocks);

// perform something...

expect(graphQL).toHavePerformedGraphQLOperation(myQuery);
expect(graphQL).toHavePerformedGraphQLOperation(MyQueryComponent, {
  id: '123',
  first: expect.any(Number),
});