chai-graphql
GraphQL response matcher for Chai assertion library
Works with both parsed JSON responses and local object responses.
Installation
npm install --save-dev chai-graphql
API
Methods will "unwrap" the data and/or payload from a response to make testing less repetitive.
assert.graphQl(response, [expectedData])
performs a deep equals on theresponse.data
orresponse.data.payload
andexpectedData
if present. Throws if there are any errors inresponse.errors
. Returnsresponse.data
assert.graphQLSubset(response, [subsetOfExpectedData])
performs a subset match ofresponse.data
orresponse.data.payload
and expectedData if present. Throws if there are any errors inresponse.errors
. Returnsresponse.data
assert.graphQLError(response, [errorMatcher])
throws if there are not anyresponse.errors
, returns theresponse.errors
.errorMatcher
can be a string, regex or an array of strings or regexes. In the string or regex form the error's message property will bematch()
by theerrorMatcher
. In the array form, eacherrorMatcher
is tested against each error in order. If there a greater or fewer number of matchers than errors an it will throw.
Usage
In your setup
chai
in your spec.js
var goodResponse = data: foo: 'bar' // Passesassertassertassertassertassertasserttobe // Failsassertassertasserttobe const badResponse = errors: message: 'Error message' stack: 'Prints if present' 'GraphQL Error Object' 'Regular Error' // Passesasserttobe assertassertassert // failsassertassertasserttobeassertassert