Skip to content

GraphQL Default Response Override Test Example #11

Description

@orthimnas

I'd like to see an example test that resets the handlers between tests and configures a unique response per test.

My understanding is that I should be able to add an "override" handler in individual tests that tweak a default configuration.

Here is some pseudo-code that represents what I think is needed for setup / use.

setupTests.js

import { handlers } from './mocks'
const server = setupServer(...handlers)
beforeAll(() => {
  server.listen()
})
afterEach(() => {
  server.resetHandlers()
})
afterAll(() => {
  server.close()
})

Test:

// Appropriate imports here to get access to server
it('Uses default setup', () => {
  // Test here
}

it('Uses custom setup', () => {
  server.use(
    graphql.query('SameQueryAsSetup`, (req, res, ctx) => {
      // Return different response from default
    });
  // Rest of test
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions