-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathApolloServer.test.ts
More file actions
152 lines (137 loc) · 4.41 KB
/
Copy pathApolloServer.test.ts
File metadata and controls
152 lines (137 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import micro from 'micro';
import listen from 'test-listen';
import { createApolloFetch } from 'apollo-server-integration-testsuite';
import { Config, gql } from 'apollo-server-core';
import rp from 'request-promise';
import { ApolloServer } from '../ApolloServer';
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'hi',
},
};
async function createServer(
options: object = {},
config: Config = {},
): Promise<any> {
const apolloServer = new ApolloServer({
typeDefs,
resolvers,
stopOnTerminationSignals: false,
...config,
});
await apolloServer.start();
const service = micro(apolloServer.createHandler(options));
const uri = await listen(service);
return {
service,
uri,
};
}
describe('apollo-server-micro', function () {
describe('constructor', function () {
it('should accepts typeDefs and resolvers', function () {
const apolloServer = new ApolloServer({ typeDefs, resolvers });
expect(apolloServer).toBeDefined();
});
});
describe('#createHandler', function () {
describe('querying', function () {
it(
'should be queryable using the default /graphql path, if no path ' +
'is provided',
async function () {
const { service, uri } = await createServer();
const apolloFetch = createApolloFetch({ uri: `${uri}/graphql` });
const result = await apolloFetch({ query: '{hello}' });
expect(result.data.hello).toEqual('hi');
service.close();
},
);
it(
'should only be queryable at the default /graphql path, if no path ' +
'is provided',
async function () {
const { service, uri } = await createServer();
const apolloFetch = createApolloFetch({ uri: `${uri}/nopath` });
let errorThrown = false;
try {
await apolloFetch({ query: '{hello}' });
} catch (error) {
errorThrown = true;
}
expect(errorThrown).toBe(true);
service.close();
},
);
it('should be queryable using a custom path', async function () {
const { service, uri } = await createServer({ path: '/data' });
const apolloFetch = createApolloFetch({ uri: `${uri}/data` });
const result = await apolloFetch({ query: '{hello}' });
expect(result.data.hello).toEqual('hi');
service.close();
});
it('should render a landing page when a browser sends in a request', async function () {
const { service, uri } = await createServer(
{},
{ __testing_nodeEnv__: undefined },
);
const body = await rp({
uri: `${uri}/graphql`,
method: 'GET',
headers: {
accept:
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
},
});
expect(body).toMatch(
/apollo-server-landing-page.cdn.apollographql.com\/_latest/,
);
service.close();
});
});
describe('health checks', function () {
it('should create a healthcheck endpoint', async function () {
const { service, uri } = await createServer();
const body = await rp(`${uri}/.well-known/apollo/server-health`);
expect(body).toEqual(JSON.stringify({ status: 'pass' }));
service.close();
});
it('should support a health check callback', async function () {
const { service, uri } = await createServer({
async onHealthCheck() {
throw Error("can't connect to DB");
},
});
let error;
try {
await rp(`${uri}/.well-known/apollo/server-health`);
} catch (err) {
error = err;
}
expect(error).toBeDefined();
expect(error.statusCode).toEqual(503);
expect(error.error).toEqual(JSON.stringify({ status: 'fail' }));
service.close();
});
it('should be able to disable the health check', async function () {
const { service, uri } = await createServer({
disableHealthCheck: true,
});
let error;
try {
await rp(`${uri}/.well-known/apollo/server-health`);
} catch (err) {
error = err;
}
expect(error).toBeDefined();
expect(error.statusCode).toEqual(404);
service.close();
});
});
});
});