Skip to content

Commit cf8c87f

Browse files
committed
Updated to ensure proper async handling and added tests
1 parent ad11c5a commit cf8c87f

2 files changed

Lines changed: 51 additions & 27 deletions

File tree

src/ApolloServer.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ async function send(req, res, statusCode, data, responseType = "application/json
1818
if (route.onAfterCall) {
1919
data = await route.onAfterCall.call(this, ctx, route, req, res, data);
2020
}
21-
2221

2322
const service = res.$service;
2423
service.sendResponse(req, res, data);
@@ -69,22 +68,22 @@ class ApolloServer extends ApolloServerBase {
6968
endpoint: this.graphqlPath,
7069
subscriptionEndpoint: this.subscriptionsPath,
7170
},
72-
this.playgroundOptions,
71+
this.playgroundOptions
7372
);
7473
return send(
7574
req,
7675
res,
7776
200,
7877
renderPlaygroundPage(middlewareOptions),
79-
"text/html",
78+
"text/html"
8079
);
8180
}
8281
}
8382

8483
// Handle incoming GraphQL requests using Apollo Server.
8584
const graphqlHandler = moleculerApollo(() => this.createGraphQLServerOptions(req, res));
8685
const responseData = await graphqlHandler(req, res);
87-
send(req, res, 200, responseData);
86+
return send(req, res, 200, responseData);
8887
};
8988
}
9089

@@ -102,10 +101,10 @@ class ApolloServer extends ApolloServerBase {
102101
onHealthCheck = onHealthCheck || (() => undefined);
103102
try {
104103
const result = await onHealthCheck(req);
105-
send(req, res, 200, { status: "pass", result }, "application/health+json");
104+
return send(req, res, 200, { status: "pass", result }, "application/health+json");
106105
} catch (error) {
107106
const result = error instanceof Error ? error.toString() : error;
108-
send(req, res, 503, { status: "fail", result }, "application/health+json");
107+
return send(req, res, 503, { status: "fail", result }, "application/health+json");
109108
}
110109
}
111110
}

test/unit/ApolloServer.spec.js

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ const ApolloServer = require("../../src/ApolloServer").ApolloServer;
1717
//ApolloServerCore.convertNodeHttpToRequest.mockImplementation(() => "convertedRequest");
1818

1919
describe("Test ApolloServer", () => {
20-
it("should support Uploads", () => {
20+
test("should support Uploads", () => {
2121
const apolloServer = new ApolloServer({});
2222
expect(apolloServer.supportsUploads()).toBe(true);
2323
});
2424

25-
it("should support subscriptions", () => {
25+
test("should support subscriptions", () => {
2626
const apolloServer = new ApolloServer({});
2727
expect(apolloServer.supportsSubscriptions()).toBe(true);
2828
});
2929

30-
it("should call super graphQLServerOptions", () => {
30+
test("should call super graphQLServerOptions", () => {
3131
const apolloServer = new ApolloServer({});
3232
ApolloServerBase.prototype.graphQLServerOptions = jest.fn();
3333

@@ -54,9 +54,10 @@ describe("Test ApolloServer", () => {
5454
const fakeRes = {
5555
$ctx: fakeCtx,
5656
$service: fakeService,
57+
$route: {},
5758
};
5859

59-
it("should return 200 'pass'", async () => {
60+
test("should return 200 'pass'", async () => {
6061
const onHealthCheck = jest.fn(() => "Everything OK");
6162

6263
await apolloServer.handleHealthCheck({ req: fakeReq, res: fakeRes, onHealthCheck });
@@ -75,7 +76,7 @@ describe("Test ApolloServer", () => {
7576
expect(fakeCtx.meta.$responseType).toBe("application/health+json");
7677
});
7778

78-
it("should return 503 'fail'", async () => {
79+
test("should return 503 'fail'", async () => {
7980
fakeService.sendResponse.mockClear();
8081

8182
const onHealthCheck = jest.fn(() => Promise.reject(new Error("Something wrong")));
@@ -96,7 +97,7 @@ describe("Test ApolloServer", () => {
9697
expect(fakeCtx.meta.$responseType).toBe("application/health+json");
9798
});
9899

99-
it("should call an empty healthcheck function", async () => {
100+
test("should call an empty healthcheck function", async () => {
100101
fakeService.sendResponse.mockClear();
101102
fakeCtx.meta.$responseType = "application/json";
102103

@@ -130,15 +131,21 @@ describe("Test ApolloServer", () => {
130131
sendResponse: jest.fn(),
131132
};
132133

133-
let fakeReq = {
134-
headers: {},
135-
};
136-
let fakeRes = {
137-
$ctx: fakeCtx,
138-
$service: fakeService,
139-
};
134+
let fakeReq;
135+
let fakeRes;
140136

141-
it("should handle as a request", async () => {
137+
beforeEach(() => {
138+
fakeReq = {
139+
headers: {},
140+
};
141+
fakeRes = {
142+
$ctx: fakeCtx,
143+
$service: fakeService,
144+
$route: {},
145+
};
146+
});
147+
148+
test("should handle as a request", async () => {
142149
const handler = apolloServer.createHandler();
143150

144151
await handler(fakeReq, fakeRes);
@@ -155,7 +162,7 @@ describe("Test ApolloServer", () => {
155162
expect(fakeService.sendResponse).toBeCalledWith(
156163
fakeReq,
157164
fakeRes,
158-
"GraphQL Response Data",
165+
"GraphQL Response Data"
159166
);
160167

161168
expect(fakeCtx.meta.$responseType).toBe("application/json");
@@ -166,7 +173,25 @@ describe("Test ApolloServer", () => {
166173
expect(apolloServer.createGraphQLServerOptions).toBeCalledWith(fakeReq, fakeRes);
167174
});
168175

169-
it("should handle as a file upload request", async () => {
176+
test("should call onAfterCall function", async () => {
177+
const handler = apolloServer.createHandler();
178+
const onAfterCall = jest.fn();
179+
const $route = { onAfterCall };
180+
fakeRes.$route = $route;
181+
182+
await handler(fakeReq, fakeRes);
183+
184+
expect(onAfterCall).toHaveBeenCalledTimes(1);
185+
expect(onAfterCall).toHaveBeenCalledWith(
186+
fakeCtx,
187+
$route,
188+
fakeReq,
189+
fakeRes,
190+
"GraphQL Response Data"
191+
);
192+
});
193+
194+
test("should handle as a file upload request", async () => {
170195
// Clear mocks
171196
moleculerApollo.mockClear();
172197
fakeGraphqlHandler.mockClear();
@@ -190,7 +215,7 @@ describe("Test ApolloServer", () => {
190215
expect(GraphqlUpload.processRequest).toBeCalledWith(
191216
fakeReq,
192217
fakeRes,
193-
apolloServer.uploadsConfig,
218+
apolloServer.uploadsConfig
194219
);
195220

196221
expect(moleculerApollo).toBeCalledTimes(1);
@@ -205,13 +230,13 @@ describe("Test ApolloServer", () => {
205230
expect(fakeService.sendResponse).toBeCalledWith(
206231
fakeReq,
207232
fakeRes,
208-
"GraphQL Response Data",
233+
"GraphQL Response Data"
209234
);
210235

211236
expect(fakeCtx.meta.$responseType).toBe("application/json");
212237
});
213238

214-
it("should handle as health-check request", async () => {
239+
test("should handle as health-check request", async () => {
215240
// Clear mocks
216241
moleculerApollo.mockClear();
217242
fakeGraphqlHandler.mockClear();
@@ -241,7 +266,7 @@ describe("Test ApolloServer", () => {
241266
expect(moleculerApollo).toBeCalledTimes(0);
242267
});
243268

244-
it("should not handle as health-check request if disabled", async () => {
269+
test("should not handle as health-check request if disabled", async () => {
245270
// Clear mocks
246271
moleculerApollo.mockClear();
247272
fakeGraphqlHandler.mockClear();
@@ -265,7 +290,7 @@ describe("Test ApolloServer", () => {
265290
expect(moleculerApollo).toBeCalledTimes(1);
266291
});
267292

268-
it("should handle as playground request", async () => {
293+
test("should handle as playground request", async () => {
269294
// Clear mocks
270295
moleculerApollo.mockClear();
271296
fakeService.sendResponse.mockClear();

0 commit comments

Comments
 (0)