Skip to content

Commit

Permalink
test: refactor test modularity
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Sep 3, 2020
1 parent 9c36d68 commit 10c23fa
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 71 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"dripip": "^0.10.0",
"express": "^4.17.1",
"fetch-cookie": "0.7.2",
"get-port": "^5.1.1",
"graphql": "^15.3.0",
"graphql-tag": "^2.11.0",
"graphql-upload": "^11.0.0",
Expand Down
15 changes: 11 additions & 4 deletions tests/__helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApolloServer } from 'apollo-server-express'
import body from 'body-parser'
import express, { Application, Request } from 'express'
import getPort from 'get-port'
import { graphqlUploadExpress } from 'graphql-upload'
import { createServer, Server } from 'http'
import { JsonObject } from 'type-fest'
Expand Down Expand Up @@ -34,17 +35,19 @@ type MockResult<Spec extends MockSpec> = {

export function setupTestServer() {
const ctx = {} as Context
beforeAll((done) => {
beforeAll(async (done) => {
const port = await getPort()
ctx.server = express()
ctx.server.use(body.json())
ctx.nodeServer = createServer()
ctx.nodeServer.listen({ port: 3210 })
ctx.url = 'http://localhost:3210'
ctx.nodeServer.listen({ port })
ctx.nodeServer.on('request', ctx.server)
ctx.nodeServer.once('listening', done)
ctx.url = `http://localhost:${port}`
ctx.res = (spec) => {
const requests: CapturedRequest[] = []
ctx.server.use('*', function mock(req, res) {
req.headers.host = 'DYNAMIC'
requests.push({
method: req.method,
headers: req.headers,
Expand Down Expand Up @@ -105,7 +108,11 @@ export function createApolloServerContext({ typeDefs, resolvers }: ApolloServerC
}
})

afterEach((done) => ctx?.server.close(done))
afterEach(async () => {
await new Promise((res, rej) => {
ctx.server.close((e) => (e ? rej(e) : res()))
})
})

return ctx
}
35 changes: 35 additions & 0 deletions tests/__snapshots__/document-node.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`accepts graphql DocumentNode as alternative to raw string 1`] = `
Object {
"requests": Array [
Object {
"body": Object {
"query": "{
query {
users
}
}
",
},
"headers": Object {
"accept": "*/*",
"accept-encoding": "gzip,deflate",
"connection": "close",
"content-length": "45",
"content-type": "application/json",
"host": "DYNAMIC",
"user-agent": "node-fetch/1.0 (+https://github.com/bitinn/node-fetch)",
},
"method": "POST",
},
],
"spec": Object {
"body": Object {
"data": Object {
"foo": 1,
},
},
},
}
`;
35 changes: 35 additions & 0 deletions tests/__snapshots__/gql.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`gql passthrough allowing benefits of tooling for gql template tag 1`] = `
Object {
"requests": Array [
Object {
"body": Object {
"query": "{
query {
users
}
}
",
},
"headers": Object {
"accept": "*/*",
"accept-encoding": "gzip,deflate",
"connection": "close",
"content-length": "45",
"content-type": "application/json",
"host": "DYNAMIC",
"user-agent": "node-fetch/1.0 (+https://github.com/bitinn/node-fetch)",
},
"method": "POST",
},
],
"spec": Object {
"body": Object {
"data": Object {
"foo": 1,
},
},
},
}
`;
20 changes: 20 additions & 0 deletions tests/document-node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import graphqlTag from 'graphql-tag'
import { request } from '../src'
import { setupTestServer } from './__helpers'

const ctx = setupTestServer()

it('accepts graphql DocumentNode as alternative to raw string', async () => {
const mock = ctx.res({ body: { data: { foo: 1 } } })
await request(
ctx.url,
graphqlTag`
{
query {
users
}
}
`
)
expect(mock).toMatchSnapshot()
})
72 changes: 9 additions & 63 deletions tests/index.test.ts → tests/general.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import graphqlTag from 'graphql-tag'
import { gql, GraphQLClient, rawRequest, request } from '../src'
import { GraphQLClient, rawRequest, request } from '../src'
import { setupTestServer } from './__helpers'

const ctx = setupTestServer()
Expand All @@ -8,21 +7,21 @@ test('minimal query', async () => {
const { data } = ctx.res({
body: {
data: {
viewer: {
me: {
id: 'some-id',
},
},
},
}).spec.body

expect(await request(ctx.url, `{ viewer { id } }`)).toEqual(data)
expect(await request(ctx.url, `{ me { id } }`)).toEqual(data)
})

test('minimal raw query', async () => {
const { extensions, data } = ctx.res({
body: {
data: {
viewer: {
me: {
id: 'some-id',
},
},
Expand All @@ -31,7 +30,7 @@ test('minimal raw query', async () => {
},
},
}).spec.body
const { headers, ...result } = await rawRequest(ctx.url, `{ viewer { id } }`)
const { headers, ...result } = await rawRequest(ctx.url, `{ me { id } }`)
expect(result).toEqual({ data, extensions, status: 200 })
})

Expand All @@ -46,7 +45,7 @@ test('minimal raw query with response headers', async () => {
},
body: {
data: {
viewer: {
me: {
id: 'some-id',
},
},
Expand All @@ -55,7 +54,7 @@ test('minimal raw query with response headers', async () => {
},
},
}).spec
const { headers, ...result } = await rawRequest(ctx.url, `{ viewer { id } }`)
const { headers, ...result } = await rawRequest(ctx.url, `{ me { id } }`)

expect(result).toEqual({ data, extensions, status: 200 })
expect(headers.get('X-Custom-Header')).toEqual(reqHeaders['X-Custom-Header'])
Expand All @@ -66,14 +65,14 @@ test('content-type with charset', async () => {
// headers: { 'Content-Type': 'application/json; charset=utf-8' },
body: {
data: {
viewer: {
me: {
id: 'some-id',
},
},
},
}).spec.body

expect(await request(ctx.url, `{ viewer { id } }`)).toEqual(data)
expect(await request(ctx.url, `{ me { id } }`)).toEqual(data)
})

test('basic error', async () => {
Expand Down Expand Up @@ -152,56 +151,3 @@ test.skip('extra fetch options', async () => {
]
`)
})

describe('DocumentNode', () => {
it('accepts graphql DocumentNode as alternative to raw string', async () => {
const mock = ctx.res({ body: { data: { foo: 1 } } })
await request(
ctx.url,
graphqlTag`
{
query {
users
}
}
`
)
expect(mock.requests[0].body).toMatchInlineSnapshot(`
Object {
"query": "{
query {
users
}
}
",
}
`)
})
})

describe('gql', () => {
it('passthrough allowing benefits of tooling for gql template tag', async () => {
const mock = ctx.res({ body: { data: { foo: 1 } } })
await request(
ctx.url,
gql`
{
query {
users
}
}
`
)
expect(mock.requests[0].body).toMatchInlineSnapshot(`
Object {
"query": "
{
query {
users
}
}
",
}
`)
})
})
22 changes: 22 additions & 0 deletions tests/gql.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import gql from 'graphql-tag'
import { request } from '../src'
import { setupTestServer } from './__helpers'

const ctx = setupTestServer()

describe('gql', () => {
it('passthrough allowing benefits of tooling for gql template tag', async () => {
const mock = ctx.res({ body: { data: { foo: 1 } } })
await request(
ctx.url,
gql`
{
query {
users
}
}
`
)
expect(mock).toMatchSnapshot()
})
})
10 changes: 6 additions & 4 deletions tests/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ beforeEach(() => {
;(global as any).FormData = undefined
})

const fileName = 'upload.test.ts'

test('file upload using global.FormData', async () => {
;(global as any).FormData = FormData

Expand All @@ -54,10 +56,10 @@ test('file upload using global.FormData', async () => {
`

const result = await request(ctx.url, query, {
file: createReadStream(join(__dirname, 'index.test.ts')),
file: createReadStream(join(__dirname, fileName)),
})

expect(result).toEqual({ uploadFile: 'index.test.ts' })
expect(result).toEqual({ uploadFile: fileName })
})

test('file upload still works if no global.FormData provided', async () => {
Expand All @@ -68,8 +70,8 @@ test('file upload still works if no global.FormData provided', async () => {
`

const result = await request(ctx.url, query, {
file: createReadStream(join(__dirname, 'index.test.ts')),
file: createReadStream(join(__dirname, fileName)),
})

expect(result).toEqual({ uploadFile: 'index.test.ts' })
expect(result).toEqual({ uploadFile: fileName })
})
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2813,6 +2813,11 @@ get-package-type@^0.1.0:
resolved "https://registry.yarnpkg.com/get-package-type/-/get-package-type-0.1.0.tgz#8de2d803cff44df3bc6c456e6668b36c3926e11a"
integrity sha512-pjzuKtY64GYfWizNAJ0fr9VqttZkNiK2iS430LtIHzjBEr6bX8Am2zm4sW4Ro5wjWW5cAlRL1qAMTcXbjNAO2Q==

get-port@^5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/get-port/-/get-port-5.1.1.tgz#0469ed07563479de6efb986baf053dcd7d4e3193"
integrity sha512-g/Q1aTSDOxFpchXC4i8ZWvxA1lnPqx/JHqcpIw0/LX9T8x/GBbi6YnlN5nhaKIFkT8oFsscUKgDJYxfwfS6QsQ==

get-stream@^4.0.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-4.1.0.tgz#c1b255575f3dc21d59bfc79cd3d2b46b1c3a54b5"
Expand Down

0 comments on commit 10c23fa

Please sign in to comment.