Registry / testing / supertest-graphql

supertest-graphql

JSON →
library1.1.4jsnpmunverified

supertest-graphql extends the popular `supertest` library to provide a streamlined API for testing GraphQL endpoints. It is currently at version 1.1.4 and appears to be in maintenance mode, with minor bug fixes and dependency updates being the primary release activity. The library simplifies sending GraphQL queries, mutations, and even subscriptions over WebSockets, abstracting away the HTTP/WebSocket request details. Key differentiators include its direct integration with `supertest`'s familiar chaining API for assertions and request configuration, explicit methods for queries (`.query()`) and mutations (`.mutate()`), and specific helpers like `.expectNoErrors()` for GraphQL error validation. It also offers dedicated support for testing GraphQL subscriptions via WebSockets, making it a comprehensive tool for end-to-end GraphQL API testing, particularly within a Node.js testing environment like Jest.

npm install supertest-graphql
INSTALL
IMPORT
SIG · SUPERTEST-GRAPHQL
S
supertest-graphql
testingjavascriptv1.1.4
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

request
✓ import request from 'supertest-graphql'
✗ const request = require('supertest-graphql')
This is the default export for HTTP-based GraphQL requests (queries/mutations). `supertest-graphql` ships with ESM and CJS support, but modern usage in testing often leans towards ESM.
supertestWs
✓ import { supertestWs } from 'supertest-graphql'
✗ import request, { supertestWs } from 'supertest-graphql'
`supertestWs` is a named export specifically for testing GraphQL subscriptions over WebSockets. It is separate from the default `request` export.
gql
✓ import gql from 'graphql-tag'
✗ import { gql } from 'supertest-graphql'
While `gql` is often used with `supertest-graphql`, it's an export from the `graphql-tag` package, not `supertest-graphql` itself. Remember to install `graphql-tag` separately.

This quickstart demonstrates how to use `supertest-graphql` to test both GraphQL queries and mutations against an Express-based GraphQL server, including variable passing and setting custom headers for authentication.

import request from 'supertest-graphql' import gql from 'graphql-tag' import express from 'express' import { graphqlHTTP } from 'express-graphql' import { buildSchema } from 'graphql' // Minimal GraphQL server setup for demonstration const schema = buildSchema(` type Pet { name: String petType: String } type Query { pets: [Pet] } type Mutation { addPet(name: String!, petType: String!): Pet } `) const root = { pets: () => [ { name: 'Buddy', petType: 'Dog' }, { name: 'Whiskers', petType: 'Cat' } ], addPet: ({ name, petType }) => ({ name, petType }) } const app = express() app.use('/graphql', graphqlHTTP({ schema: schema, rootValue: root, graphiql: false })) // --- supertest-graphql usage --- describe('GraphQL API tests', () => { test('should fetch pets via query', async () => { const { data } = await request(app) .query(gql` query { pets { name petType } } `) .expectNoErrors() .expect(200) expect(data.pets).toHaveLength(2) expect(data.pets[0].name).toBe('Buddy') }) test('should add a pet via mutation', async () => { const newPetName = 'Fido' const newPetType = 'Dog' const { data } = await request(app) .mutate(gql` mutation AddPet($name: String!, $petType: String!) { addPet(name: $name, petType: $petType) { name petType } } `) .variables({ name: newPetName, petType: newPetType }) .expectNoErrors() .expect(200) expect(data.addPet.name).toBe(newPetName) expect(data.addPet.petType).toBe(newPetType) }) test('should handle authorization via headers', async () => { const authToken = process.env.AUTH_TOKEN ?? 'some_secret_token' const { data } = await request(app) .set('Authorization', `Bearer ${authToken}`) .query(gql` query { pets { name } } `) .expectNoErrors() .expect(200) expect(data.pets).toHaveLength(2) }) })
Debug
Known issues
gotcha`supertest-graphql` relies on `supertest` for HTTP assertions. When testing, remember to use both `supertest-graphql`'s specific assertions (e.g., `.expectNoErrors()`) and `supertest`'s general HTTP assertions (e.g., `.expect(200)`, `.expect('Content-Type', /json/)`).
fix
Always chain both GraphQL-specific and HTTP-specific assertions to fully validate responses. For example: `.expectNoErrors().expect(200)`.
affects: >=1.0.0
gotchaWhen testing GraphQL subscriptions, `supertest-graphql` requires a different entry point (`supertestWs`) and expects the server to be manually started and closed (e.g., with `beforeEach`/`afterEach` hooks in your test runner), unlike HTTP requests where `supertest` can often handle a direct application instance.
fix
Use `import { supertestWs } from 'supertest-graphql'` and ensure your WebSocket server is properly managing its lifecycle around subscription tests. Refer to the `supertest-graphql` documentation for detailed subscription test patterns.
affects: >=1.1.3
gotcha`supertest-graphql` does not bundle `graphql-tag` or `graphql`. While it works with raw string queries, using `gql` tagged template literals (from `graphql-tag`) is common for better syntax highlighting and parsing. `graphql` itself is a peer dependency.
fix
Install `graphql-tag` (e.g., `npm install graphql-tag`) if you plan to use `gql` for query definitions. Ensure `graphql` is installed and meets the peer dependency requirements of `supertest-graphql`.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: request(...).query is not a function
You are likely trying to use the `request` function imported from `supertest` directly, instead of `supertest-graphql`'s extended `request` function.
fix
Ensure you are importing `request` from `supertest-graphql`: `import request from 'supertest-graphql'`.
expected no errors but got 1 error(s) in GraphQL response: Syntax Error: Unexpected Name "blooop".
The GraphQL query string provided to `.query()` or `.mutate()` is syntactically incorrect or malformed, causing the GraphQL server to return a parsing error.
fix
Review your GraphQL query or mutation string for syntax errors. Use `gql` from `graphql-tag` for better IDE support and error checking during development.
Error: GraphQL error: Cannot query field "nonExistentField" on type "MyType".
The GraphQL query is valid syntactically but requests a field that does not exist on the specified type in your GraphQL schema.
fix
Verify that the fields requested in your query or mutation precisely match the fields defined in your GraphQL schema. This often indicates a mismatch between the test query and the actual API schema.
Upgrade
Version history
1.1.4latest on npm
Audit
Dependencies
graphqlrequiredRequired for parsing GraphQL queries (e.g., with `gql` tag) and understanding GraphQL types.
supertestrequiredThe core HTTP assertion library that `supertest-graphql` extends. Essential for its functionality.
graphql-tagoptionalCommonly used alongside `supertest-graphql` for parsing GraphQL query strings, especially for `gql` tagged template literals.
Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
1
Resources