-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
apollo-server-testing
package. (#1909)
- Loading branch information
1 parent
422609e
commit e3d5be9
Showing
13 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Changelog | ||
|
||
### vNEXT | ||
|
||
* `apollo-server-testing`: Added createTestClient function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# apollo-server-testing | ||
|
||
[![npm version](https://badge.fury.io/js/apollo-server-testing.svg)](https://badge.fury.io/js/apollo-server-testing) | ||
[![Build Status](https://circleci.com/gh/apollographql/apollo-server.svg?style=svg)](https://circleci.com/gh/apollographql/apollo-server) | ||
|
||
This is the testing module of the Apollo community GraphQL Server. [Read the docs.](https://www.apollographql.com/docs/apollo-server/) | ||
[Read the CHANGELOG.](https://github.com/apollographql/apollo-server/blob/master/CHANGELOG.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const config = require('../../jest.config.base'); | ||
|
||
module.exports = Object.assign(Object.create(null), config); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "apollo-server-testing", | ||
"version": "2.2.0-alpha.0", | ||
"description": "Test utils for apollo-server", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/apollographql/apollo-server/tree/master/packages/apollo-server-testing" | ||
}, | ||
"keywords": [ | ||
"GraphQL", | ||
"Apollo", | ||
"Server", | ||
"Javascript" | ||
], | ||
"author": "Jonas Helfer <jonas@helfer.email>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/apollographql/apollo-server/issues" | ||
}, | ||
"homepage": "https://github.com/apollographql/apollo-server#readme", | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"dependencies": { | ||
"apollo-server-core": "file:../apollo-server-core" | ||
}, | ||
"peerDependencies": { | ||
"graphql": "^0.12.0 || ^0.13.0 || ^14.0.0" | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
packages/apollo-server-testing/src/__tests__/createTestClient.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { ApolloServerBase, gql } from 'apollo-server-core'; | ||
import createTestClient from '../createTestClient'; | ||
|
||
describe('createTestClient', () => { | ||
const typeDefs = gql` | ||
type Query { | ||
test(echo: String): String | ||
# this resolver uses context | ||
hello: String | ||
} | ||
type Mutation { | ||
increment: Int! | ||
} | ||
`; | ||
|
||
const resolvers = { | ||
Query: { | ||
test: (_, { echo }) => echo, | ||
hello: (_, __, { person }) => { | ||
return `hello ${person}`; | ||
}, | ||
}, | ||
Mutation: { | ||
increment: () => 1, | ||
}, | ||
}; | ||
|
||
const myTestServer = new ApolloServerBase({ | ||
typeDefs, | ||
context: () => ({ person: 'tom' }), | ||
resolvers, | ||
}); | ||
|
||
it('allows queries', async () => { | ||
const query = `{ test(echo: "foo") }`; | ||
const client = createTestClient(myTestServer); | ||
const res = await client.query({ query }); | ||
expect(res.data).toEqual({ test: 'foo' }); | ||
}); | ||
|
||
it('allows mutations', async () => { | ||
const mutation = `mutation increment { increment }`; | ||
const client = createTestClient(myTestServer); | ||
const res = await client.mutate({ mutation }); | ||
expect(res.data).toEqual({ increment: 1 }); | ||
}); | ||
|
||
it('allows variables to be passed', async () => { | ||
const query = `query test($echo: String){ test(echo: $echo) }`; | ||
const client = createTestClient(myTestServer); | ||
const res = await client.query({ query, variables: { echo: 'wow' } }); | ||
expect(res.data).toEqual({ test: 'wow' }); | ||
}); | ||
|
||
it('resolves with context', async () => { | ||
const query = `{ hello }`; | ||
const client = createTestClient(myTestServer); | ||
const res = await client.query({ query }); | ||
expect(res.data).toEqual({ hello: 'hello tom' }); | ||
}); | ||
|
||
it('allows query documents as input', async () => { | ||
const query = gql` | ||
{ | ||
test(echo: "foo") | ||
} | ||
`; | ||
const client = createTestClient(myTestServer); | ||
const clientRes = await client.query({ query }); | ||
expect(clientRes.data).toEqual({ test: 'foo' }); | ||
|
||
const mutation = gql` | ||
mutation increment { | ||
increment | ||
} | ||
`; | ||
const mutationRes = await client.mutate({ mutation }); | ||
expect(mutationRes.data).toEqual({ increment: 1 }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "../../../../tsconfig.test.base", | ||
"include": ["**/*"], | ||
"references": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { ApolloServerBase } from 'apollo-server-core'; | ||
import { print, DocumentNode } from 'graphql'; | ||
|
||
type StringOrAst = string | DocumentNode; | ||
interface QueryOrMutation { | ||
query?: StringOrAst; | ||
mutation?: StringOrAst; | ||
} | ||
|
||
export default (server: ApolloServerBase) => { | ||
const executeOperation = server.executeOperation.bind(server); | ||
const test = ({ query, mutation, ...args }: QueryOrMutation) => { | ||
const operation = query || mutation; | ||
|
||
if ((!query && !mutation) || (query && mutation)) { | ||
throw new Error('Either `query` or `mutation` must be passed'); | ||
} | ||
|
||
return executeOperation({ | ||
// Convert ASTs, which are produced by `graphql-tag` but not currently | ||
// used by `executeOperation`, to a String using `graphql/language/print`. | ||
query: typeof operation === 'string' ? operation : print(operation), | ||
...args, | ||
}); | ||
}; | ||
|
||
return { query: test, mutate: test }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as createTestClient } from './createTestClient'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": "../../tsconfig.base", | ||
"compilerOptions": { | ||
"rootDir": "./src", | ||
"outDir": "./dist" | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["**/__tests__", "**/__mocks__"], | ||
"references": [ | ||
{ "path": "../apollo-server-core" } | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters