Skip to content

Instantly share code, notes, and snippets.

@coleturner
Last active June 7, 2018 02:19
Show Gist options
  • Save coleturner/8a2c7486009fddbf0cbd30ff0f0ceec3 to your computer and use it in GitHub Desktop.
Save coleturner/8a2c7486009fddbf0cbd30ff0f0ceec3 to your computer and use it in GitHub Desktop.
Quick Bootstrapping of Testing for Apollo Server
import {
makeExecutableSchema,
addMockFunctionsToSchema,
mockServer
} from 'graphql-tools';
const testCaseA = {
id: 'Test case A',
query: `
query {
animals {
origin
}
}
`,
variables: { },
context: { },
expected: { data: { animals: [{ kind: 'Dog' }] } }
};
describe('Schema', () => {
// Array of case types
const cases = [testCaseA];
const mockSchema = makeExecutableSchema({ typeDefs });
// Here we specify the return payloads of mocked types
addMockFunctionsToSchema({
schema: mockSchema,
mocks: {
Boolean: () => false,
ID: () => '1',
Int: () => 1,
Float: () => 12.34,
String: () => 'Dog',
}
});
test('has valid type definitions', async () => {
expect(async () => {
const MockServer = mockServer(typeDefs);
await MockServer.query(`{ __schema { types { name } } }`);
}).not.toThrow();
});
cases.forEach(obj => {
const { id, query, variables, context: ctx, expected } = obj;
test(`query: ${id}`, async () => {
return await expect(
graphql(mockSchema, query, null, { ctx }, variables)
).resolves.toEqual(expected);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment