0.0.12 • Published 4 years ago
@os-utils/graphql-test-client v0.0.12
@os-utils/graphql-test-client
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.
Examples
Base request
import TestClient from '@os-utils/graphql-test-client'
// Example of mutation
const signInMutation = `
mutation SignInMutation($input: SignInInput!) {
signIn(input: $input) {
ok
}
}
`
it('Should make a request successfully', async () => {
// Example of input data
const input = {
email: 'name@domain.com',
password: 'password',
}
// Create an instance of TestClient ans pass the GraphQL url
const client = new TestClient({
url: 'http://localhost:4000/graphql',
})
// Make a GraphQL request and pass the variables
const res = await client.query({
query: signInMutation,
variables: { input },
})
expect(res.body.errors).toBeUndefined()
expect(res.body.data).toBeDefined()
})
Request with cookies
import TestClient from '@os-utils/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: 'name@domain.com',
password: 'password',
}
const client = new TestClient({
url: 'http://localhost:4000/graphql',
saveCookies: true,
})
await client.query({
query: signInMutation,
variables: { input },
})
const res = await client.query({
query: profileQuery,
})
expect(res.body.errors).toBeUndefined()
expect(res.body.data).toBeDefined()
})