Last active
June 7, 2018 02:19
-
-
Save coleturner/8a2c7486009fddbf0cbd30ff0f0ceec3 to your computer and use it in GitHub Desktop.
Quick Bootstrapping of Testing for Apollo Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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