Skip to content

Commit

Permalink
chore: switch from ava to jest
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed May 28, 2020
1 parent 0bcaa05 commit 483119e
Show file tree
Hide file tree
Showing 3 changed files with 2,344 additions and 857 deletions.
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,34 @@
"homepage": "https://github.com/prisma/graphql-request",
"scripts": {
"dev": "rm -rf dist && tsc --watch",
"dev:test": "jest --watch",
"format": "prettier --write .",
"prepublish": "npm run build",
"build": "rm -rf dist && tsc -d",
"lint": "tslint --project . {examples,src,test}/**/*.ts",
"test": "yarn lint && yarn build && ava --serial",
"test": "yarn lint && yarn build && jest",
"test:ci": "yarn test && bundlesize",
"semantic-release": "semantic-release"
},
"dependencies": {},
"devDependencies": {
"@prisma-labs/prettier-config": "^0.1.0",
"@types/fetch-mock": "5.12.2",
"@types/jest": "^25.2.3",
"@types/node": "10.17.24",
"ava": "^3.8.2",
"bundlesize": "^0.18.0",
"fetch-cookie": "0.7.2",
"fetch-mock": "5.13.1",
"jest": "^26.0.1",
"prettier": "^2.0.5",
"semantic-release": "17.0.8",
"ts-jest": "^26.0.0",
"tslint": "5.11.0",
"tslint-config-standard": "8.0.1",
"typescript": "^3.9.3"
},
"prettier": "@prisma-labs/prettier-config"
"prettier": "@prisma-labs/prettier-config",
"jest": {
"preset": "ts-jest"
}
}
100 changes: 46 additions & 54 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import test from 'ava'
import * as fetchMock from 'fetch-mock'
import { ClientError, GraphQLClient, rawRequest, request } from '../src/index'
import { GraphQLClient, rawRequest, request } from '../src'

test('minimal query', async (t) => {
test('minimal query', async () => {
const data = {
viewer: {
id: 'some-id',
},
}

await mock({ body: { data } }, async () => {
t.deepEqual(await request('https://mock-api.com/graphql', `{ viewer { id } }`), data)
})
mock({ body: { data } })
expect(await request('https://mock-api.com/graphql', `{ viewer { id } }`)).toEqual(data)
})

test('minimal raw query', async (t) => {
test('minimal raw query', async () => {
const data = {
viewer: {
id: 'some-id',
Expand All @@ -25,13 +23,12 @@ test('minimal raw query', async (t) => {
version: '1',
}

await mock({ body: { data, extensions } }, async () => {
const { headers, ...result } = await rawRequest('https://mock-api.com/graphql', `{ viewer { id } }`)
t.deepEqual(result, { data, extensions, status: 200 })
})
mock({ body: { data, extensions } })
const { headers, ...result } = await rawRequest('https://mock-api.com/graphql', `{ viewer { id } }`)
expect(result).toEqual({ data, extensions, status: 200 })
})

test('minimal raw query with response headers', async (t) => {
test('minimal raw query with response headers', async () => {
const data = {
viewer: {
id: 'some-id',
Expand All @@ -47,15 +44,14 @@ test('minimal raw query with response headers', async (t) => {
'X-Custom-Header': 'test-custom-header',
}

await mock({ headers: reqHeaders, body: { data, extensions } }, async () => {
const { headers, ...result } = await rawRequest('https://mock-api.com/graphql', `{ viewer { id } }`)
mock({ headers: reqHeaders, body: { data, extensions } })
const { headers, ...result } = await rawRequest('https://mock-api.com/graphql', `{ viewer { id } }`)

t.deepEqual(result, { data, extensions, status: 200 })
t.deepEqual(headers.get('X-Custom-Header'), reqHeaders['X-Custom-Header'])
})
expect(result).toEqual({ data, extensions, status: 200 })
expect(headers.get('X-Custom-Header')).toEqual(reqHeaders['X-Custom-Header'])
})

test('basic error', async (t) => {
test('basic error', async () => {
const errors = {
message: 'Syntax Error GraphQL request (1:1) Unexpected Name "x"\n\n1: x\n ^\n',
locations: [
Expand All @@ -66,13 +62,13 @@ test('basic error', async (t) => {
],
}

await mock({ body: { errors } }, async () => {
const err = await t.throwsAsync<ClientError>(request('https://mock-api.com/graphql', `x`))
t.deepEqual<any>(err.response.errors, errors)
})
mock({ body: { errors } })
expect(() => request('https://mock-api.com/graphql', `x`)).rejects.toThrowErrorMatchingInlineSnapshot(
`"GraphQL Error (Code: 200): {\\"response\\":{\\"errors\\":{\\"message\\":\\"Syntax Error GraphQL request (1:1) Unexpected Name \\\\\\"x\\\\\\"\\\\n\\\\n1: x\\\\n ^\\\\n\\",\\"locations\\":[{\\"line\\":1,\\"column\\":1}]},\\"status\\":200},\\"request\\":{\\"query\\":\\"x\\"}}"`
)
})

test('raw request error', async (t) => {
test('raw request error', async () => {
const errors = {
message: 'Syntax Error GraphQL request (1:1) Unexpected Name "x"\n\n1: x\n ^\n',
locations: [
Expand All @@ -83,53 +79,49 @@ test('raw request error', async (t) => {
],
}

await mock({ body: { errors } }, async () => {
const err = await t.throwsAsync<ClientError>(rawRequest('https://mock-api.com/graphql', `x`))
t.deepEqual<any>(err.response.errors, errors)
})
mock({ body: { errors } })
expect(rawRequest('https://mock-api.com/graphql', `x`)).rejects.toThrowErrorMatchingInlineSnapshot(
`"GraphQL Error (Code: 200): {\\"response\\":{\\"errors\\":{\\"message\\":\\"Syntax Error GraphQL request (1:1) Unexpected Name \\\\\\"x\\\\\\"\\\\n\\\\n1: x\\\\n ^\\\\n\\",\\"locations\\":[{\\"line\\":1,\\"column\\":1}]},\\"status\\":200,\\"headers\\":{\\"_headers\\":{\\"content-type\\":[\\"application/json\\"]}}},\\"request\\":{\\"query\\":\\"x\\"}}"`
)
})

test('content-type with charset', async (t) => {
test('content-type with charset', async () => {
const data = {
viewer: {
id: 'some-id',
},
}

await mock(
{
headers: { 'Content-Type': 'application/json; charset=utf-8' },
body: { data },
},
async () => {
t.deepEqual(await request('https://mock-api.com/graphql', `{ viewer { id } }`), data)
}
)
mock({
headers: { 'Content-Type': 'application/json; charset=utf-8' },
body: { data },
})
expect(await request('https://mock-api.com/graphql', `{ viewer { id } }`)).toEqual(data)
})

test('extra fetch options', async (t) => {
test('extra fetch options', async () => {
const options: RequestInit = {
credentials: 'include',
mode: 'cors',
cache: 'reload',
}

const client = new GraphQLClient('https://mock-api.com/graphql', options)
await mock(
{
body: { data: { test: 'test' } },
},
async () => {
await client.request('{ test }')
const actualOptions = fetchMock.lastCall()[1]
for (let name in options) {
t.deepEqual(actualOptions[name], options[name])
}
}
)
mock({
body: { data: { test: 'test' } },
})
await client.request('{ test }')
const actualOptions = fetchMock.lastCall()[1]
for (let name in options) {
expect(actualOptions[name]).toEqual(options[name])
}
})

async function mock(response: any, testFn: () => Promise<void>) {
/**
* Helpers
*/

async function mock(response: any) {
fetchMock.mock({
matcher: '*',
response: {
Expand All @@ -140,8 +132,8 @@ async function mock(response: any, testFn: () => Promise<void>) {
body: JSON.stringify(response.body),
},
})
}

await testFn()

afterEach(() => {
fetchMock.restore()
}
})
Loading

0 comments on commit 483119e

Please sign in to comment.