1.1.4 • Published 2 years ago
supertest-graphql v1.1.4
supertest-graphql
Extends supertest to test a GraphQL endpoint
Install
npm install supertest-graphql
Usage
import request from 'supertest-graphql'
import gql from 'graphql-tag'
test('should get pets', async () => {
const { data } = await request(app)
.query(gql`
query {
pets {
name
petType
}
}
`)
.expectNoErrors()
expect(data.pets).toHaveLength(2)
})
Set expectations
expectNoErrors
will verify that the API response has no errors
in
its result payload.
await request(app)
.query('blooop')
.expectNoErrors()
// expected no errors but got 1 error(s) in GraphQL response: Syntax Error: Unexpected Name "blooop".
Variables
const { data } = await request(app)
.query(gql`
query GetPets($first: Int){
pets(first: $first) {
name
petType
}
}
`)
.variables({ first: 4 })
Mutation
const { data } = await request(app)
.mutate(gql`
mutation PetAnimal($petId: ID!) {
petAnimal(petId: $petId) {
name
petType
}
}
`)
.variables({petId: 'my-cat' })
Subscriptions with WebScoket
import { supertestWs } from 'supertest-graphql'
import gql from 'graphql-tag'
// for websocket the server needs to be started and stopped manually
beForeEach(() => server.listen(0, "localhost"))
afterEach(() => server.close())
test('should get pets', async () => {
const sub = await supertestWs(app)
.subscribe(gql`
subscription {
newPetAdded {
name
petType
}
}
`)
// will wait or pop the next value
const { data } = await sub.next().expectNoErrors()
expect(data.newPetAdded.name).toEqual('Fifi')
})
Authentication
const { data } = await request(app)
.auth('username', 'password')
.query(...)
or via headers:
const { data } = await request(app)
.set('authorization', 'my token')
.query(...)
For WebSocket with connectionParams
:
import { supertestWs } from 'supertest-graphql'
const sub = await supertestWs(app)
.connectionParams({
token: 'my token'
})
.subscribe(...)
Change GraphQL endpoint path
By default, the execution are sent to /graphql
.
You can change this with .path()
:
const { data } = await request(app)
.path('/new-graphql')
.query(...)
Use WebSocket legacy protocol
import { supertestWs, LEGACY_WEBSOCKET_PROTOCOL } from 'supertest-graphql'
const sub = await supertestWs(app)
.protocol(LEGACY_WEBSOCKET_PROTOCOL)
.subscribe(...)
1.1.3--canary.6.2005663092.0
2 years ago
1.1.3--canary.6.2004790247.0
2 years ago
1.1.3--canary.6.2004793411.0
2 years ago
1.1.4
2 years ago
1.1.3
2 years ago
1.1.3--canary.6.2004542793.0
2 years ago
1.1.4--canary.6.2024109149.0
2 years ago
1.1.3--canary.6.1973672715.0
2 years ago
1.1.3--canary.6.1931002463.0
2 years ago
1.1.3--canary.6.1825335232.0
2 years ago
1.1.3--canary.6.1755592997.0
2 years ago
1.1.3--canary.6.1755540175.0
2 years ago
1.1.3--canary.6.1697850741.0
2 years ago
1.1.2
2 years ago
1.1.2--canary.4.1616023792.0
2 years ago
1.1.1
2 years ago
1.1.0
2 years ago
1.0.1--canary.3.1595028265.0
2 years ago
1.0.0
2 years ago
0.0.0-beta
2 years ago