Skip to content

Instantly share code, notes, and snippets.

@seandearnaley
Last active February 1, 2020 22:59
Show Gist options
  • Save seandearnaley/8aa52cea51aeeaa26f91379b654df8b1 to your computer and use it in GitHub Desktop.
Save seandearnaley/8aa52cea51aeeaa26f91379b654df8b1 to your computer and use it in GitHub Desktop.
BrainStrike Server Integration Test
import { createTestClient } from "apollo-server-testing";
import gql from "graphql-tag";
import {
constructTestServer,
createTestingConnection,
Connection
} from "./__utils";
import {
mockFirstCardResponse,
mockFirstCardResponseId
} from "./__testData";
const GET_CARDS = gql`
query getCards {
cards {
id
number
label
description
}
}
`;
const GET_CARD = gql`
query card($id: ID!) {
card(id: $id) {
id
number
label
description
}
}
`;
describe("Queries", () => {
let connection: Connection;
beforeAll(async () => {
console.log("creating test connection");
connection = await createTestingConnection();
});
afterAll(() => connection.close());
it("fetches list of cards", async () => {
// create an instance of ApolloServer that mocks out context, while reusing
// existing dataSources, resolvers, and typeDefs.
const { apolloServer, cardAPI } = await constructTestServer(connection, {
context: () => ({})
});
// mock the datasources' underlying fetch methods
cardAPI.getCards = jest.fn(async () =>
Promise.resolve([mockFirstCardResponse])
);
// use our test server as input to the createTestClient fn
// This will give us an interface, similar to apolloClient.query
// to run queries against our instance of ApolloServer
const { query } = createTestClient(apolloServer);
const res = await query({
query: GET_CARDS
});
expect(res).toMatchSnapshot();
});
it("fetches single card", async () => {
const { apolloServer, cardAPI } = await constructTestServer(connection, {
context: () => ({})
});
cardAPI.getCard = jest.fn(async () =>
Promise.resolve(mockFirstCardResponse)
);
const { query } = createTestClient(apolloServer);
const res = await query({
query: GET_CARD,
variables: { id: mockFirstCardResponseId }
});
expect(res).toMatchSnapshot();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment