Skip to content

Commit ac0116f

Browse files
authored
clean up the unused wrap method (#682)
1 parent 546e007 commit ac0116f

File tree

3 files changed

+27
-299
lines changed

3 files changed

+27
-299
lines changed

src/utils/handler.spec.ts

Lines changed: 26 additions & 235 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Context, Handler } from "aws-lambda";
22

33
import { didFunctionColdStart } from "./cold-start";
4-
import { wrap } from "./handler";
4+
import { promisifiedHandler } from "./handler";
55
import { LogLevel, setLogLevel } from "./log";
66

77
const mockContext = {
@@ -12,180 +12,51 @@ beforeEach(() => {
1212
setLogLevel(LogLevel.NONE);
1313
});
1414

15-
describe("wrap", () => {
15+
describe("promisifiedHandler", () => {
1616
it("returns a promise when callback used by the handler", async () => {
1717
const handler: Handler = (event, context, callback) => {
1818
callback(null, { statusCode: 200, body: "The body of the response" });
1919
};
2020

21-
let calledStart = false;
22-
let calledComplete = false;
23-
let calledOriginalHandler = false;
24-
25-
const wrappedHandler = wrap(
26-
handler,
27-
async () => {
28-
calledStart = true;
29-
},
30-
async () => {
31-
calledComplete = true;
32-
},
33-
);
34-
35-
const result = await wrappedHandler({}, mockContext, () => {
36-
calledOriginalHandler = true;
37-
});
38-
expect(result).toEqual({ statusCode: 200, body: "The body of the response" });
39-
expect(calledStart).toBeTruthy();
40-
expect(calledComplete).toBeTruthy();
41-
expect(calledOriginalHandler).toBeFalsy();
42-
});
43-
44-
it("recovers from onStart throwing an error and invokes the original lambda callback", async () => {
45-
const handler: Handler = (event, context, callback) => {
46-
callback(null, { statusCode: 200, body: "The body of the response" });
47-
};
48-
49-
let calledStart = false;
50-
let calledComplete = false;
51-
let calledOriginalHandler = false;
52-
53-
const wrappedHandler = wrap(
54-
handler,
55-
() => {
56-
calledStart = true;
57-
throw Error("Some Error");
58-
},
59-
async () => {
60-
calledComplete = true;
61-
},
62-
);
63-
64-
const result = await wrappedHandler({}, mockContext, () => {
65-
calledOriginalHandler = true;
66-
});
67-
expect(result).toEqual({ statusCode: 200, body: "The body of the response" });
68-
expect(calledStart).toBeTruthy();
69-
expect(calledComplete).toBeTruthy();
70-
expect(calledOriginalHandler).toBeFalsy();
71-
});
72-
73-
it("recovers from onComplete throwing an error and invokes the original lambda callback", async () => {
74-
const handler: Handler = (event, context, callback) => {
75-
callback(null, { statusCode: 200, body: "The body of the response" });
76-
};
21+
const promHandler = promisifiedHandler(handler) as any;
7722

78-
let calledStart = false;
79-
let calledComplete = false;
80-
let calledOriginalHandler = false;
81-
82-
const wrappedHandler = wrap(
83-
handler,
84-
async () => {
85-
calledStart = true;
86-
},
87-
async () => {
88-
calledComplete = true;
89-
throw Error("Some Error");
90-
},
91-
);
92-
93-
const result = await wrappedHandler({}, mockContext, () => {
94-
calledOriginalHandler = true;
95-
});
23+
const result = await promHandler({}, mockContext);
9624
expect(result).toEqual({ statusCode: 200, body: "The body of the response" });
97-
expect(calledStart).toBeTruthy();
98-
expect(calledComplete).toBeTruthy();
99-
expect(calledOriginalHandler).toBeFalsy();
10025
});
10126

10227
it("returns a promise when the original handler returns a promise", async () => {
10328
const handler: Handler = async (event, context, callback) => {
10429
return { statusCode: 200, body: "The body of the response" };
10530
};
10631

107-
let calledStart = false;
108-
let calledComplete = false;
109-
let calledOriginalHandler = false;
32+
const promHandler = promisifiedHandler(handler) as any;
11033

111-
const wrappedHandler = wrap(
112-
handler,
113-
async () => {
114-
calledStart = true;
115-
},
116-
async () => {
117-
calledComplete = true;
118-
},
119-
);
120-
121-
const result = await wrappedHandler({}, mockContext, () => {
122-
calledOriginalHandler = true;
123-
});
34+
const result = await promHandler({}, mockContext);
12435

12536
expect(result).toEqual({ statusCode: 200, body: "The body of the response" });
126-
expect(calledStart).toBeTruthy();
127-
expect(calledComplete).toBeTruthy();
128-
expect(calledOriginalHandler).toBeFalsy();
12937
});
13038

13139
it("throws an error when the original lambda gives the callback an error", async () => {
13240
const handler: Handler = (event, context, callback) => {
13341
return callback(new Error("Some error"), { statusCode: 200, body: "The body of the response" });
13442
};
13543

136-
let calledStart = false;
137-
let calledComplete = false;
138-
let calledOriginalHandler = false;
139-
140-
const wrappedHandler = wrap(
141-
handler,
142-
async () => {
143-
calledStart = true;
144-
},
145-
async () => {
146-
calledComplete = true;
147-
throw Error("An error");
148-
},
149-
);
150-
151-
const result = wrappedHandler({}, mockContext, () => {
152-
calledOriginalHandler = true;
153-
});
44+
const promHandler = promisifiedHandler(handler) as any;
15445

155-
await expect(result).rejects.toEqual(new Error("Some error"));
46+
const result = promHandler({}, mockContext);
15647

157-
expect(calledStart).toBeTruthy();
158-
expect(calledComplete).toBeTruthy();
159-
expect(calledOriginalHandler).toBeFalsy();
48+
await expect(result).rejects.toEqual(new Error("Some error"));
16049
});
16150

16251
it("throws an error when the original lambda throws an error", async () => {
16352
const handler: Handler = async (event, context, callback) => {
16453
throw Error("Some error");
16554
};
16655

167-
let calledStart = false;
168-
let calledComplete = false;
169-
let calledOriginalHandler = false;
170-
171-
const wrappedHandler = wrap(
172-
handler,
173-
async () => {
174-
calledStart = true;
175-
},
176-
async () => {
177-
calledComplete = true;
178-
},
179-
);
180-
181-
const result = wrappedHandler({}, mockContext, () => {
182-
calledOriginalHandler = true;
183-
});
184-
await expect(result).rejects.toEqual(Error("Some error"));
56+
const promHandler = promisifiedHandler(handler) as any;
18557

186-
expect(calledStart).toBeTruthy();
187-
expect(calledComplete).toBeTruthy();
188-
expect(calledOriginalHandler).toBeFalsy();
58+
const result = promHandler({}, mockContext);
59+
await expect(result).rejects.toEqual(Error("Some error"));
18960
});
19061

19162
it("returns the first result to complete between the callback and the handler promise", async () => {
@@ -194,20 +65,11 @@ describe("wrap", () => {
19465
return { statusCode: 200, body: "The promise response" };
19566
};
19667

197-
let calledOriginalHandler = false;
198-
199-
const wrappedHandler = wrap(
200-
handler,
201-
async () => {},
202-
async () => {},
203-
);
68+
const promHandler = promisifiedHandler(handler) as any;
20469

205-
const result = await wrappedHandler({}, mockContext, () => {
206-
calledOriginalHandler = true;
207-
});
70+
const result = await promHandler({}, mockContext);
20871

20972
expect(result).toEqual({ statusCode: 204, body: "The callback response" });
210-
expect(calledOriginalHandler).toBeFalsy();
21173
});
21274

21375
it("doesn't complete using non-promise return values", async () => {
@@ -218,112 +80,58 @@ describe("wrap", () => {
21880
return { statusCode: 200, body: "The promise response" } as unknown as void;
21981
};
22082

221-
let calledOriginalHandler = false;
222-
223-
const wrappedHandler = wrap(
224-
handler,
225-
async () => {},
226-
async () => {},
227-
);
83+
const promHandler = promisifiedHandler(handler) as any;
22884

229-
const result = await wrappedHandler({}, mockContext, () => {
230-
calledOriginalHandler = true;
231-
});
85+
const result = await promHandler({}, mockContext);
23286

23387
expect(result).toEqual({ statusCode: 204, body: "The callback response" });
234-
expect(calledOriginalHandler).toBeFalsy();
23588
});
23689

23790
it("completes when calling context.done", async () => {
23891
const handler: Handler = async (event, context, callback) => {
23992
context.done(undefined, { statusCode: 204, body: "The callback response" });
24093
};
24194

242-
let calledOriginalHandler = false;
95+
const promHandler = promisifiedHandler(handler) as any;
24396

244-
const wrappedHandler = wrap(
245-
handler,
246-
async () => {},
247-
async () => {},
248-
);
249-
250-
const result = await wrappedHandler({}, mockContext, () => {
251-
calledOriginalHandler = true;
252-
});
97+
const result = await promHandler({}, mockContext);
25398

25499
expect(result).toEqual({ statusCode: 204, body: "The callback response" });
255-
expect(calledOriginalHandler).toBeFalsy();
256100
});
257101
it("completes when calling context.succeed", async () => {
258102
const handler: Handler = async (event, context, callback) => {
259103
context.succeed({ statusCode: 204, body: "The callback response" });
260104
};
261105

262-
let calledOriginalHandler = false;
263-
264-
const wrappedHandler = wrap(
265-
handler,
266-
async () => {},
267-
async () => {},
268-
);
106+
const promHandler = promisifiedHandler(handler) as any;
269107

270-
const result = await wrappedHandler({}, mockContext, () => {
271-
calledOriginalHandler = true;
272-
});
108+
const result = await promHandler({}, mockContext);
273109

274110
expect(result).toEqual({ statusCode: 204, body: "The callback response" });
275-
expect(calledOriginalHandler).toBeFalsy();
276111
});
277112

278113
it("throws error when calling context.fail", async () => {
279114
const handler: Handler = async (event, context, callback) => {
280115
context.fail(new Error("Some error"));
281116
};
282117

283-
let calledOriginalHandler = false;
284-
285-
const wrappedHandler = wrap(
286-
handler,
287-
async () => {},
288-
async () => {},
289-
);
118+
const promHandler = promisifiedHandler(handler) as any;
290119

291-
const result = wrappedHandler({}, mockContext, () => {
292-
calledOriginalHandler = true;
293-
});
120+
const result = promHandler({}, mockContext);
294121

295122
await expect(result).rejects.toEqual(new Error("Some error"));
296-
297-
expect(calledOriginalHandler).toBeFalsy();
298123
});
299124

300125
it("completes when handler returns undefined", async () => {
301126
const handler: Handler = (event, context) => {
302127
// No return statement, implicitly returns undefined
303128
};
304129

305-
let calledStart = false;
306-
let calledComplete = false;
307-
let calledOriginalHandler = false;
308-
309-
const wrappedHandler = wrap(
310-
handler,
311-
async () => {
312-
calledStart = true;
313-
},
314-
async () => {
315-
calledComplete = true;
316-
},
317-
);
130+
const promHandler = promisifiedHandler(handler) as any;
318131

319-
const result = await wrappedHandler({}, mockContext, () => {
320-
calledOriginalHandler = true;
321-
});
132+
const result = await promHandler({}, mockContext);
322133

323134
expect(result).toEqual(undefined);
324-
expect(calledStart).toBeTruthy();
325-
expect(calledComplete).toBeTruthy();
326-
expect(calledOriginalHandler).toBeFalsy();
327135
});
328136

329137
it("completes when handler returns a value directly (sync handler)", async () => {
@@ -332,27 +140,10 @@ describe("wrap", () => {
332140
return { statusCode: 200, body: "Sync response" };
333141
};
334142

335-
let calledStart = false;
336-
let calledComplete = false;
337-
let calledOriginalHandler = false;
338-
339-
const wrappedHandler = wrap(
340-
handler as any,
341-
async () => {
342-
calledStart = true;
343-
},
344-
async () => {
345-
calledComplete = true;
346-
},
347-
);
143+
const promHandler = promisifiedHandler(handler as any) as any;
348144

349-
const result = await wrappedHandler({}, mockContext, () => {
350-
calledOriginalHandler = true;
351-
});
145+
const result = await promHandler({}, mockContext);
352146

353147
expect(result).toEqual({ statusCode: 200, body: "Sync response" });
354-
expect(calledStart).toBeTruthy();
355-
expect(calledComplete).toBeTruthy();
356-
expect(calledOriginalHandler).toBeFalsy();
357148
});
358149
});

0 commit comments

Comments
 (0)