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

@os-team/graphql-test-client

v1.0.31

Published

The wrapper of the superagent to make GraphQL queries in tests.

Downloads

65

Readme

@os-team/graphql-test-client NPM version

The wrapper of the superagent to make GraphQL queries in tests.

Unlike apollo-boost and apollo-client, superagent saves cookies in the Node.js environment. This is useful, for example, for testing the authentication flow.

Usage

Install the package in devDependencies (-D) using the following command:

yarn add -D @os-team/graphql-test-client

Example 1. Base request

For example, let's request the sign-in method.

import GraphQLTestClient from '@os-team/graphql-test-client';

const signInMutation = `
  mutation SignInMutation($input: SignInInput!) {
    signIn(input: $input) {
      ok
    }
  }
`;

it('Should make a request successfully', async () => {
  const input = {
    email: '[email protected]',
    password: 'password',
  };

  // Create an instance of GraphQLTestClient
  const client = new GraphQLTestClient({
    url: 'http://localhost:4000/graphql',
  });

  // Make a GraphQL request and pass the variables
  const res = await client.query({
    query: signInMutation,
    variables: { input },
  });

  // Test the response
  expect(res.body.errors).toBeUndefined();
  expect(res.body.data).toBeDefined();
});

Cool, but if you want to request the profile method after that, you must save the cookies. See the next example how you can do it.

Example 2. Request with cookies

Pass saveCookies: true when creating an instance of GraphQLTestClient. Now the GraphQL test client will save cookies after each request.

import GraphQLTestClient from '@os-team/graphql-test-client';

const signInMutation = `
  mutation SignInMutation($input: SignInInput!) {
    signIn(input: $input) {
      ok
    }
  }
`;

const profileQuery = `
  query profileQuery {
    profile {
      email
      firstName
      lastName
    }
  }
`;

it('Should sign in successfully', async () => {
  const input = {
    email: '[email protected]',
    password: 'password',
  };

  // Create an instance of GraphQLTestClient
  const client = new GraphQLTestClient({
    url: 'http://localhost:4000/graphql',
    saveCookies: true,
  });

  // Sign in
  await client.query({
    query: signInMutation,
    variables: { input },
  });

  // Get profile
  const res = await client.query({
    query: profileQuery,
  });

  // Test the response
  expect(res.body.errors).toBeUndefined();
  expect(res.body.data).toBeDefined();
});

Example 3. Request with file uploads

Sometimes you want to upload files. Pass the files in the uploadables parameter.

import GraphQLTestClient from '@os-team/graphql-test-client';

const createPostMutation = `
  mutation CreatePostMutation($input: CreatePostInput!) {
    createPost(input: $input) {
      id
      title
      content
      cover
    }
  }
`;

it('Should create a new post', async () => {
  const input = {
    title: 'title',
    content: {},
  };

  // Create an instance of GraphQLTestClient
  const client = new GraphQLTestClient({
    url: 'http://localhost:4000/graphql',
  });

  // Make the request
  const res = await client.query({
    query: createPostMutation,
    variables: { input },
    uploadables: {
      cover: path.resolve(__dirname, './image.jpg'),
    },
  });

  // Test the response
  expect(res.body.errors).toBeUndefined();
  expect(res.body.data).toBeDefined();
});

GraphQLTestClient uses the specification for GraphQL multipart form requests to upload files. Make sure you implement this spec.

Example 4. Request with headers

There are 2 ways how you can specify headers:

  • Specify unique headers for each request.
  • Specify shared headers for all requests.
import GraphQLTestClient from '@os-team/graphql-test-client';

const createPostMutation = `
  mutation CreatePostMutation($input: CreatePostInput!) {
    createPost(input: $input) {
      id
      title
      content
      cover
    }
  }
`;

it('Should create a new post', async () => {
  const input = {
    title: 'title',
    content: {},
  };

  // Create an instance of GraphQLTestClient
  const client = new GraphQLTestClient({
    url: 'http://localhost:4000/graphql',
    sharedHeaders: {
      Authorization: 'Bearer AUTH_KEY',
    },
  });

  // Make the request
  const res = await client.query({
    query: createPostMutation,
    variables: { input },
    headers: {
      'X-Header': 'Some header',
    },
  });

  // Test the response
  expect(res.body.errors).toBeUndefined();
  expect(res.body.data).toBeDefined();
});

If the same header is used in sharedHeaders and headers, the latter will rewrite the shared header.


Most likely you will use multiple test files (e.g. one test file for testing one API method). In this case:

  1. Create an instance of GraphQLTestClient in a separate file and export this variable to use it in any test file.
  2. Add the path to this file to setupFilesAfterEnv. Jest will run this code before each test file in the suite is executed.