/* File: examples/api-routes-apollo-server-and-client/tests/integration.test.js */ import { testApiHandler } from 'next-test-api-route-handler'; // Import the handler under test from the pages/api directory import * as pagesHandler from '../pages/api/graphql'; describe('my-test (pages router)', () => { it('does what I want 1', async () => { expect.hasAssertions(); await testApiHandler({ pagesHandler, url: '/api/graphql', // Set the request url to the path graphql expects test: async ({ fetch }) => { const query = `query ViewerQuery { viewer { id name status } }`; const res = await fetch({ method: 'POST', headers: { 'content-type': 'application/json' // Must use correct content type }, body: JSON.stringify({ query }) }); await expect(res.json()).resolves.toStrictEqual({ data: { viewer: { id: '1', name: 'John Smith', status: 'cached' } } }); } }); }); it('does what I want 2', async () => { expect.hasAssertions(); await testApiHandler({ pagesHandler, url: '/api/graphql', // Set the request url to the path graphql expects test: async ({ fetch }) => { const query = `query ViewerQuery { viewer { id name status } }`; const res = await fetch({ method: 'POST', headers: { 'content-type': 'application/json' // Must use correct content type }, body: JSON.stringify({ query }) }); await expect(res.json()).resolves.toStrictEqual({ data: { viewer: { id: '1', name: 'John Smith', status: 'cached' } } }); } }); }); it('does what I want 3', async () => { expect.hasAssertions(); await testApiHandler({ pagesHandler, url: '/api/graphql', // Set the request url to the path graphql expects test: async ({ fetch }) => { const query = `query ViewerQuery { viewer { id name status } }`; const res = await fetch({ method: 'POST', headers: { 'content-type': 'application/json' // Must use correct content type }, body: JSON.stringify({ query }) }); await expect(res.json()).resolves.toStrictEqual({ data: { viewer: { id: '1', name: 'John Smith', status: 'cached' } } }); } }); }); });