Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mock-apollo-client

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mock-apollo-client

Library to help unit testing when using apollo-client

  • 0.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
150K
decreased by-2.96%
Maintainers
1
Weekly downloads
 
Created

What is mock-apollo-client?

The mock-apollo-client npm package is designed to help developers test their Apollo Client GraphQL queries and mutations by providing a way to mock the responses. This is particularly useful for unit testing and integration testing in a controlled environment without needing to rely on a live GraphQL server.

What are mock-apollo-client's main functionalities?

Mocking Queries

This feature allows you to mock GraphQL queries. The code sample demonstrates how to mock a query using the MockedProvider from @apollo/client/testing. The GET_DOGS query is mocked to return a predefined response.

const { MockedProvider } = require('@apollo/client/testing');
const { ApolloClient, InMemoryCache, gql } = require('@apollo/client');

const GET_DOGS = gql`
  query GetDogs {
    dogs {
      id
      breed
    }
  }
`;

const mocks = [
  {
    request: {
      query: GET_DOGS,
    },
    result: {
      data: {
        dogs: [{ id: '1', breed: 'Bulldog' }],
      },
    },
  },
];

const client = new ApolloClient({
  cache: new InMemoryCache(),
});

<MockedProvider mocks={mocks} addTypename={false}>
  <MyComponent />
</MockedProvider>;

Mocking Mutations

This feature allows you to mock GraphQL mutations. The code sample demonstrates how to mock a mutation using the MockedProvider from @apollo/client/testing. The ADD_DOG mutation is mocked to return a predefined response when called with specific variables.

const { MockedProvider } = require('@apollo/client/testing');
const { ApolloClient, InMemoryCache, gql } = require('@apollo/client');

const ADD_DOG = gql`
  mutation AddDog($breed: String!) {
    addDog(breed: $breed) {
      id
      breed
    }
  }
`;

const mocks = [
  {
    request: {
      query: ADD_DOG,
      variables: { breed: 'Bulldog' },
    },
    result: {
      data: {
        addDog: { id: '1', breed: 'Bulldog' },
      },
    },
  },
];

const client = new ApolloClient({
  cache: new InMemoryCache(),
});

<MockedProvider mocks={mocks} addTypename={false}>
  <MyComponent />
</MockedProvider>;

Testing Error States

This feature allows you to test how your components handle error states. The code sample demonstrates how to mock an error response for a query using the MockedProvider from @apollo/client/testing. The GET_DOGS query is mocked to return an error.

const { MockedProvider } = require('@apollo/client/testing');
const { ApolloClient, InMemoryCache, gql } = require('@apollo/client');

const GET_DOGS = gql`
  query GetDogs {
    dogs {
      id
      breed
    }
  }
`;

const mocks = [
  {
    request: {
      query: GET_DOGS,
    },
    error: new Error('An error occurred'),
  },
];

const client = new ApolloClient({
  cache: new InMemoryCache(),
});

<MockedProvider mocks={mocks} addTypename={false}>
  <MyComponent />
</MockedProvider>;

Other packages similar to mock-apollo-client

Keywords

FAQs

Package last updated on 12 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc