Registry / testing / chai-graphql

chai-graphql

JSON →
library4.0.0jsnpmunverified

chai-graphql v4.0.0 is a Chai plugin for asserting GraphQL responses, including data matching and error checking. It unwraps response.data and response.errors automatically and supports deep equals, subset matching, and error message validation with strings or regexes. Actively maintained with monthly releases, it works with both parsed JSON and local objects. Differentiated by its Chai integration and simple API for common GraphQL testing patterns.

npm install chai-graphql
INSTALL
IMPORT
SIG · CHAI-GRAPHQL
C
chai-graphql
testingjavascriptv4.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

default
import chaiGraphQL from 'chai-graphql'
const chaiGraphQL = require('chai-graphql')
ESM import is recommended; CommonJS require is also supported.
chai.use
chai.use(chaiGraphQL)
chai.use(chaiGraphQL.default)
chaiGraphQL is the plugin function; no .default needed.
assert.graphQL
assert.graphQL(response, expectedData)
assert.graphQl(response, expectedData)
Case-sensitive: method is graphQL (capital Q, capital L).

Shows how to install the plugin and use assert.graphQL, assert.graphQLSubset, expect.graphQl, and graphQLError.

import chai from 'chai'; import chaiGraphQL from 'chai-graphql'; chai.use(chaiGraphQL); const { assert, expect } = chai; const goodResponse = { data: { foo: 'bar' } }; assert.graphQL(goodResponse, { foo: 'bar' }); // passes assert.graphQLSubset(goodResponse, { foo: 'bar' }); // passes expect(goodResponse).to.be.graphQl({ foo: 'bar' }); // passes const badResponse = { errors: [{ message: 'Error message' }] }; assert.graphQLError(badResponse, /Error/); // passes expect(badResponse).to.be.graphQLError(); // passes
Debug
Known issues
gotchaMethod naming inconsistency: assert.graphQL vs expect.to.be.graphQl vs assert.graphQLError
fix
Always use exact casing: graphQL (capital Q, capital L), graphQl (capital Q lowercase L), graphQLError (capital Q, capital L, capital E).
affects: >=1.0.0
breakingChai v5+ compatibility: chai-graphql may not work with Chai v5 due to API changes
fix
Use Chai v4.x; check plugin compatibility before upgrading.
affects: >=4.0.0
gotchaassert.graphQL does not check for errors if expectedData is provided — throws only if errors exist in response
fix
To check both data and errors, call assert.graphQLError after assert.graphQL for error presence.
affects: >=1.0.0
gotchaassert.graphQLSubset with empty expectedData ({}) will fail because empty object is subset of anything but deep equal mismatch? Actually it passes: deep equal with empty object fails but subset match passes
fix
Use subset with care: {} matches any data, so use expectedData to narrow assertions.
affects: >=1.0.0
deprecatedNode engine >=4 is outdated; modern Node versions (12+) may have undetected issues
fix
Update engine requirement or test on your Node version; consider migrating to chai-graphql v5 if available.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: chaiGraphQL is not a function
Incorrect import: default exported as object due to CommonJS usage.
fix
Use: const chaiGraphQL = require('chai-graphql').default; (CJS) or import chaiGraphQL from 'chai-graphql' (ESM).
AssertionError: expected { data: ... } to have property 'graphQl'
Forgot to call chai.use(chaiGraphQL) before using expect.
fix
Add chai.use(chaiGraphQL) in setup file before tests.
TypeError: Cannot read properties of undefined (reading 'data')
Response object is undefined or malformed.
fix
Ensure response argument is an object with data and/or errors properties.
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
17 hits · last 30 days
node
14
Amazon
1
OpenAI (training)
1
Resources
chai-graphql — npm install chai-graphql · libregistry