-
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.
- Loading branch information
1 parent
9deb176
commit 3537173
Showing
11 changed files
with
201 additions
and
70 deletions.
There are no files selected for viewing
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
68 changes: 0 additions & 68 deletions
68
packages/apollo-server-core/src/__tests__/test-utils.test.ts
This file was deleted.
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
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 <[email protected]>", | ||
"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" | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
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,70 @@ | ||
const { ApolloServerBase, gql } = require('apollo-server-core'); | ||
const createTestClient = require('../createTestClient'); | ||
|
||
describe('createTestClient', () => { | ||
const typeDefs = gql` | ||
type Query { | ||
test(echo: String): String | ||
# this resolver uses context | ||
hello: String | ||
} | ||
type Mutation { | ||
increment: Int! | ||
} | ||
`; | ||
|
||
let num = 0; | ||
const resolvers = { | ||
Query: { | ||
test: (_, { echo }) => echo, | ||
hello: (_, __, { person }) => { | ||
return `hello ${person}`; | ||
}, | ||
}, | ||
Mutation: { | ||
increment: () => ++num, | ||
}, | ||
}; | ||
|
||
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('uses default context function if not overwritten', async () => { | ||
const query = `{ hello }`; | ||
const client = createTestClient(myTestServer); | ||
const res = await client.query({ query }); | ||
expect(res.data).toEqual({ hello: 'hello tom' }); | ||
}); | ||
|
||
it('allows mocking of context', async () => { | ||
const query = `{ hello }`; | ||
const client = createTestClient(myTestServer, () => ({ person: 'mary' })); | ||
const res = await client.query({ query }); | ||
expect(res.data).toEqual({ hello: 'hello mary' }); | ||
}); | ||
}); |
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,26 @@ | ||
import { ApolloServerBase, gql } from 'apollo-server-core'; | ||
import { print } from 'graphql'; | ||
|
||
const createTestClient = (server: ApolloServerBase, ctxFn: any) => { | ||
// if a context fn is required, overwrite the old one | ||
// allows easy mocking of the context | ||
if (ctxFn) server.context = ctxFn; | ||
|
||
const executeOperation = server.executeOperation.bind(server); | ||
return { | ||
query: ({ query, ...rest }) => | ||
executeOperation({ | ||
// print the query document if it isn't a string | ||
query: typeof query === 'string' ? query : print(query), | ||
...rest, | ||
}), | ||
mutate: ({ mutation, ...rest }) => | ||
executeOperation({ | ||
// print the query document if it isn't a string | ||
query: typeof mutation === 'string' ? mutation : print(mutation), | ||
...rest, | ||
}), | ||
}; | ||
}; | ||
|
||
module.exports = 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,5 @@ | ||
const createTestClient = require('./createTestClient'); | ||
|
||
module.exports = { | ||
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" } | ||
] | ||
} |