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

mercurius-integration-testing

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mercurius-integration-testing

[![npm version](https://badge.fury.io/js/mercurius-integration-testing.svg)](https://badge.fury.io/js/mercurius-integration-testing) [![codecov](https://codecov.io/gh/PabloSzx/mercurius-integration-testing/branch/master/graph/badge.svg)](https://codecov.i

  • 2.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.2K
decreased by-40.99%
Maintainers
1
Weekly downloads
 
Created
Source

mercurius-integration-testing

npm version codecov

yarn add mercurius-integration-testing
# or
npm install mercurius-integration-testing

Features

  • DocumentNode and string support
  • TypeScript support
  • query | mutation support.
  • batchQueries support.
  • headers management.
  • cookies management.

Table of Contents

Usage

// app.ts | app.js
import Fastify from "fastify";
import Mercurius from "mercurius";
import schema from "./schema";
import { buildContext } from "./buildContext";

export const app = Fastify();

app.register(Mercurius, {
  schema,
  resolvers: {},
  context: buildContext,
  allowBatchedQueries: true,
});
// integration.test.js | integration.test.ts

import { createMercuriusTestClient } from "mercurius-integration-testing";
import { app } from "../app";

// ...

const testClient = createMercuriusTestClient(app);

expect(testClient.query("query { helloWorld }")).resolves.toEqual({
  data: {
    helloWorld: "helloWorld",
  },
});

API

createMercuriusTestClient

Create a testing client instance, you should give it the fastify instance in which Mercurius was already registered, and optionally, some options

const client = createMercuriusTestClient(app, {
  /**
   * Optional, specify headers to be added to every request in the client
   */
  headers: {
    authorization: "hello-world",
  },
  /**
   * Optional, by default it points to /graphql
   */
  url: "/graphql",
  /**
   * Optional, specify cookies to be added to every request in the client
   */
  cookies: {
    authorization: "hello-world",
  },
});
query, mutate

.query and .mutate are basically the same function, but for readability, both exists

// You can give it a simple string
const queryResponse = await client.query(`
query {
  helloWorld
}
`);

// Data returned from the API
queryResponse.data;

// Possible array of errors from the API
queryResponse.errors;

// You can also call `mutate`
// to improve readability for mutations
const mutationResponse = await client.mutate(`
mutation {
  helloWorld
}
`);
DocumentNode support
// You can also give them `DocumentNode`s
// from `graphql-tag` or equivalents
await client.query(gql`
  query {
    helloWorld
  }
`);
Variables
// You can give variables in the second parameter options
await client.query(
  `
  query($foo: String!) {
    hello(foo: $foo)
  }
`,
  {
    variables: {
      foo: "bar",
    },
  }
);
TypeScript
const dataResponse = await client.query<{
  helloWorld: string;
}>(`
query {
  helloWorld
}
`);

// string
dataResponse.data.helloWorld;

const variablesResponse = await client.query<
  {
    user: {
      email: string;
    };
  },
  {
    name: string;
  }
>(
  `
  query($name: String!) {
    user(name: $name) {
      email
    }
  }
`,
  {
    variables: {
      name: "bob",
    },
  }
);

// string
variablesResponse.data.user.email;
Other options
await client.query(
  `
  query example {
    helloExample
  }
`,
  {
    // You can specify operation name if the queries
    // are named
    operationName: "helloExample",
    // Query specific headers
    // These are going to be "merged" with the client set headers
    headers: {
      hello: "world",
    },

    // Query specific cookies
    // These are going to be "merged" with the client set headers
    cookies: {
      foo: "bar",
    },
  }
);
setHeaders

You can change the default client headers whenever

client.setHeaders({
  authorization: "other-header",
});
setCookies

You can change the default client cookies whenever

client.setCookies({
  authorization: "other-cookie",
});
batchQueries

If allowBatchedQueries is set in the Mercurius registration, you can call some queries together

const batchedResponse = await client.batchQueries(
  [
    {
      query: `
  query {
    helloWorld
  }
  `,
    },
    {
      query: `
  query($name: String!) {
    user(name: $name) {
      email
    }
  }
  `,
      variables: {
        name: "bob",
      },
      // operationName: "you-can-specify-it-here-if-needed"
    },
  ],
  // Optional
  {
    // Optional request specific cookies
    cookies: {
      foo: "bar",
    },
    // Optional request specific headers
    headers: {
      foo: "bar",
    },
  }
);

batchedResponse ===
  [{ data: { helloWorld: "foo" } }, { data: { user: { email: "hello@world.com" } } }];

License

MIT

Keywords

FAQs

Package last updated on 11 Oct 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