Skip to content

Instantly share code, notes, and snippets.

@ErrorPro
Last active October 15, 2021 21:12
Show Gist options
  • Select an option

  • Save ErrorPro/756c0f3b1d5e5233422950bd90c6c9fa to your computer and use it in GitHub Desktop.

Select an option

Save ErrorPro/756c0f3b1d5e5233422950bd90c6c9fa to your computer and use it in GitHub Desktop.
// create a graphlhttp middleware
const middleware = graphqlHTTP({
schema,
rootValue: {
id: 'viewer',
loaders: {
countries: () => countriesMock,
},
},
});
// create a mocked request
const request = {
method: 'POST',
headers: {},
body: { query: '{ viewer { countries { name } } }' },
};
// create a mocked response, graphql middleware calls json function to set response data, so we stub it.
const response = {
setHeader: jest.fn(),
end: jest.fn(),
json: jest.fn(),
};
// call middleware function with mocked response and request
await middleware(request, response);
// get json's stub function arguments, this is actually a data returned by graphql middleware
const responseData = response.json.mock.calls[0][0];
//now we can do two things: check that the data returned is equal what we passed as a mock
expect(responseData).toEqual(countriesMock);
// or just use jest.snapshot to snapshot
expect(responseData).toMatchSnapshot();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment