chai-graphql npm version Build Status devDependency Status

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

  • assert.graphQl(response, [expectedData]) performs a deep equals on the response.data, throws if there are response.errors, returns the data
  • assert.graphQLError(response) throws if there are not response.errors, returns the errors

Usage

In your setup

import chai from 'chai'
import chaiGraphQL from 'chai-graphql'
chai.use(chaiGraphQL)

in your spec.js

var goodResponse = {
  data: {
    foo: 'bar'
  }
}

// Passes
assert.graphQL(goodResponse, { foo: 'bar' })
assert.graphQL(goodResponse)
assert.notGraphQLError(goodResponse)
expect(goodResponse).to.be.graphQl({ foo: 'bar' })

// Fails
assert.graphQLError(goodResponse)
expect(goodResponse).to.be.graphQLError()

const badResponse = {
  errors: [
    {
      message: 'Error message',
      stack: 'Prints if present'
    },
    new GraphQLError('GraphQL Error Object'),
    new Error('Regular Error')
  ]
}

// Passes
assert.graphQLError(badResponse)
expect(badResponse).to.be.graphQLError()

// fails
assert.graphQL(badResponse, { foo: 'bar' })
assert.graphQL(badResponse)
assert.notGraphQLError(badResponse)
expect(badResponse).to.be.graphQl({ foo: 'bar' })