Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alexstrat/supertest-graphql
Extends supertest to test a GraphQL endpoint
https://github.com/alexstrat/supertest-graphql
Last synced: 2 months ago
JSON representation
Extends supertest to test a GraphQL endpoint
- Host: GitHub
- URL: https://github.com/alexstrat/supertest-graphql
- Owner: alexstrat
- Created: 2021-12-17T21:51:48.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-01-24T09:39:09.000Z (almost 2 years ago)
- Last Synced: 2024-10-11T23:30:08.277Z (3 months ago)
- Language: TypeScript
- Homepage:
- Size: 633 KB
- Stars: 32
- Watchers: 2
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
- awesome-graphql - supertest-graphql - Extends [supertest](https://github.com/visionmedia/supertest) to easily test a GraphQL endpoint (Implementations / JavaScript/TypeScript)
README
# supertest-graphql
> Extends [supertest](https://www.npmjs.com/package/supertest) to test a GraphQL endpoint
## Install
```sh
npm install supertest-graphql
```## Usage
```ts
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.```ts
await request(app)
.query('blooop')
.expectNoErrors()
// expected no errors but got 1 error(s) in GraphQL response: Syntax Error: Unexpected Name "blooop".
```
### Variables
```ts
const { data } = await request(app)
.query(gql`
query GetPets($first: Int){
pets(first: $first) {
name
petType
}
}
`)
.variables({ first: 4 })
```### Mutation
```ts
const { data } = await request(app)
.mutate(gql`
mutation PetAnimal($petId: ID!) {
petAnimal(petId: $petId) {
name
petType
}
}
`)
.variables({petId: 'my-cat' })
```### Subscriptions with WebScoket
```ts
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
```ts
const { data } = await request(app)
.auth('username', 'password')
.query(...)
```or via headers:
```ts
const { data } = await request(app)
.set('authorization', 'my token')
.query(...)
```For WebSocket with `connectionParams`:
```ts
import { supertestWs } from 'supertest-graphql'const sub = await supertestWs(app)
.connectionParams({
token: 'my token'
})
.subscribe(...)
```
### Change GraphQL endpoint pathBy default, the execution are sent to `/graphql`.
You can change this with `.path()`:
```ts
const { data } = await request(app)
.path('/new-graphql')
.query(...)
```### Use WebSocket legacy protocol
```ts
import { supertestWs, LEGACY_WEBSOCKET_PROTOCOL } from 'supertest-graphql'const sub = await supertestWs(app)
.protocol(LEGACY_WEBSOCKET_PROTOCOL)
.subscribe(...)
```