Skip to content

Commit

Permalink
Adds GraphQL errors integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Mar 9, 2020
1 parent 4436075 commit 680c53a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/graphql-api/errors.mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { composeMocks, graphql } from 'msw'

const { start } = composeMocks(
graphql.query({ operation: 'Login' }, (req, res, ctx) => {
return res(
ctx.errors([
{
message: 'This is a mocked error',
locations: [
{
line: 1,
column: 2,
},
],
},
]),
)
}),
)

start()
56 changes: 56 additions & 0 deletions test/graphql-api/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as path from 'path'
import { Response } from 'puppeteer'
import { TestAPI, runBrowserWith } from '../support/runBrowserWith'
import { executeOperation } from './utils/executeOperation'

describe('GraphQL: Errors', () => {
let api: TestAPI

beforeAll(async () => {
api = await runBrowserWith(path.resolve(__dirname, 'errors.mocks.ts'))
})

afterAll(() => {
return api.cleanup()
})

describe('given mocked query errors', () => {
let res: Response
let body: Record<string, any>

beforeAll(async () => {
res = await executeOperation(api.page, {
query: `
query Login {
user {
id
}
}
`,
})
body = await res.json()
})

it('should return 200 status code', () => {
expect(res.status()).toBe(200)
})

it('should not return any data', () => {
expect(body).not.toHaveProperty('data')
})

it('should mock the error', () => {
expect(body).toHaveProperty('errors', [
{
message: 'This is a mocked error',
locations: [
{
line: 1,
column: 2,
},
],
},
])
})
})
})

0 comments on commit 680c53a

Please sign in to comment.