Skip to content

Commit

Permalink
chore: refactor test file
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Feb 3, 2024
1 parent 0f1b7b5 commit fff4731
Showing 1 changed file with 19 additions and 46 deletions.
65 changes: 19 additions & 46 deletions tests/errorPolicy.test.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,36 @@
import { GraphQLClient } from '../src/entrypoints/main.js'
import { setupMockServer } from './__helpers.js'
import { expect, test } from 'vitest'
import { describe, expect, test } from 'vitest'

const ctx = setupMockServer()
const errors = {
message: `Syntax Error GraphQL request (1:1) Unexpected Name "x"\n\n1: x\n ^\n`,
locations: [
{
line: 1,
column: 1,
},
],
locations: [{ line: 1, column: 1 }],
}

test(`should throw error when error policy not set`, async () => {
ctx.res({
body: {
data: {},
errors,
},
describe(`should throw error`, () => {
test(`should throw error when error policy not set`, async () => {
ctx.res({ body: { data: {}, errors } })
await expect(() => new GraphQLClient(ctx.url).rawRequest(`x`)).rejects.toThrow(`GraphQL Error`)
})

await expect(() => new GraphQLClient(ctx.url).rawRequest(`x`)).rejects.toThrow(`GraphQL Error`)
})

test(`should throw error when error policy set to "none"`, async () => {
ctx.res({
body: {
data: {},
errors,
},
test(`when error policy set to "none"`, async () => {
ctx.res({ body: { data: {}, errors } })
await expect(() => new GraphQLClient(ctx.url).rawRequest(`x`)).rejects.toThrow(`GraphQL Error`)
})

await expect(() => new GraphQLClient(ctx.url).rawRequest(`x`)).rejects.toThrow(`GraphQL Error`)
})

test(`should not throw error when error policy set to "ignore" and return only data`, async () => {
ctx.res({
body: {
data: { test: {} },
errors,
},
describe(`should not throw error`, () => {
test(`when error policy set to "ignore" and return only data`, async () => {
ctx.res({ body: { data: { test: {} }, errors } })
const res = await new GraphQLClient(ctx.url, { errorPolicy: `ignore` }).rawRequest(`x`)
expect(res).toEqual(expect.objectContaining({ data: { test: {} } }))
expect(res).toEqual(expect.not.objectContaining({ errors }))
})

const res = await new GraphQLClient(ctx.url, { errorPolicy: `ignore` }).rawRequest(`x`)

expect(res).toEqual(expect.objectContaining({ data: { test: {} } }))
expect(res).toEqual(expect.not.objectContaining({ errors }))
})

test(`should not throw error when error policy set to "all" and return both data and error`, async () => {
ctx.res({
body: {
data: { test: {} },
errors,
},
test(`when error policy set to "all" and return both data and error`, async () => {
ctx.res({ body: { data: { test: {} }, errors } })
const res = await new GraphQLClient(ctx.url, { errorPolicy: `all` }).rawRequest(`x`)
expect(res).toEqual(expect.objectContaining({ data: { test: {} }, errors }))
})

const res = await new GraphQLClient(ctx.url, { errorPolicy: `all` }).rawRequest(`x`)

expect(res).toEqual(expect.objectContaining({ data: { test: {} }, errors }))
})

0 comments on commit fff4731

Please sign in to comment.