You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importrequestfrom'supertest-graphql'importgqlfrom'graphql-tag'test('should get pets',async()=>{const{ data }=awaitrequest(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.
awaitrequest(app).query('blooop').expectNoErrors()// expected no errors but got 1 error(s) in GraphQL response: Syntax Error: Unexpected Name "blooop".
Variables
const{ data }=awaitrequest(app).query(gql` query GetPets($first: Int){ pets(first: $first) { name petType } } `).variables({first: 4})
Mutation
const{ data }=awaitrequest(app).mutate(gql` mutation PetAnimal($petId: ID!) { petAnimal(petId: $petId) { name petType } } `).variables({petId: 'my-cat'})
Subscriptions with WebScoket
import{supertestWs}from'supertest-graphql'importgqlfrom'graphql-tag'// for websocket the server needs to be started and stopped manuallybeForeEach(()=>server.listen(0,"localhost"))afterEach(()=>server.close())test('should get pets',async()=>{constsub=awaitsupertestWs(app).subscribe(gql` subscription { newPetAdded { name petType } } `)// will wait or pop the next valueconst{ data }=awaitsub.next().expectNoErrors()expect(data.newPetAdded.name).toEqual('Fifi')})
Authentication
const{ data }=awaitrequest(app).auth('username','password').query(...)
or via headers:
const{ data }=awaitrequest(app).set('authorization','my token').query(...)