Skip to content

Commit 3fccbe1

Browse files
committed
It's 2020. Time to use async / await ;)
1 parent 18f9302 commit 3fccbe1

File tree

2 files changed

+211
-220
lines changed

2 files changed

+211
-220
lines changed

test/integration.deferred.test.ts

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
import app from './app';
22
import lambda from '../src/lambda';
33

4-
let __test = 0;
5-
const handler = lambda.deferred(() => new Promise(resolve => {
6-
__test = __test + 1;
7-
setTimeout(() => {
8-
resolve(app);
9-
}, 10);
10-
}));
4+
5+
const testEnvironment = () => {
6+
let __test = 0;
7+
const _handler = lambda.deferred(() => new Promise(resolve => {
8+
__test = __test + 1;
9+
setTimeout(() => {
10+
resolve(app);
11+
}, 10);
12+
}));
13+
14+
return {
15+
getValue: () => __test,
16+
handler: _handler,
17+
}
18+
}
1119

1220
describe('integration for deferred app', () => {
13-
it('returns static file', () => {
21+
it('returns static file', async () => {
1422
const myEvent = {
1523
path: "/static/file.png",
1624
httpMethod: "GET",
@@ -19,15 +27,15 @@ describe('integration for deferred app', () => {
1927
isBase64Encoded: false,
2028
body: null
2129
}
22-
return handler(myEvent).then(response => {
23-
expect(response.statusCode).toEqual(200);
24-
expect(response.isBase64Encoded).toEqual(true);
25-
expect(response.multiValueHeaders!["content-type"][0]).toEqual('image/png');
26-
expect(response.multiValueHeaders!["content-length"][0]).toEqual('178');
27-
});
30+
const t = testEnvironment()
31+
const response = await t.handler(myEvent)
32+
expect(response.statusCode).toEqual(200);
33+
expect(response.isBase64Encoded).toEqual(true);
34+
expect(response.multiValueHeaders!["content-type"][0]).toEqual('image/png');
35+
expect(response.multiValueHeaders!["content-length"][0]).toEqual('178');
2836
});
2937

30-
it('resolves the app promise only once', () => {
38+
it('resolves the app promise only once', async () => {
3139
const myEvent = {
3240
path: "/static/file.png",
3341
httpMethod: "GET",
@@ -36,16 +44,14 @@ describe('integration for deferred app', () => {
3644
isBase64Encoded: false,
3745
body: null
3846
}
39-
return handler(myEvent)
40-
.then(() => {
41-
expect(__test).toEqual(1);
42-
return handler(myEvent);
43-
}).then(() => {
44-
expect(__test).toEqual(1);
45-
});
47+
const t = testEnvironment()
48+
await t.handler(myEvent)
49+
expect(t.getValue()).toEqual(1);
50+
await t.handler(myEvent);
51+
expect(t.getValue()).toEqual(1);
4652
});
4753

48-
it('handler returns rejected promise if app cannot be initialized', () => {
54+
it('handler returns rejected promise if app cannot be initialized', async () => {
4955
const failingHandler = lambda.deferred(() => Promise.reject(new Error('failed to initialize app')));
5056
const myEvent = {
5157
path: "/static/file.png",
@@ -55,15 +61,15 @@ describe('integration for deferred app', () => {
5561
isBase64Encoded: false,
5662
body: null
5763
}
58-
return failingHandler(myEvent)
59-
.then(
60-
() => Promise.reject(new Error('should have failed')),
61-
e => {
62-
expect(e.message).toEqual('failed to initialize app')
63-
})
64+
try {
65+
await failingHandler(myEvent)
66+
fail(new Error('should have failed'))
67+
} catch (e) {
68+
expect(e.message).toEqual('failed to initialize app')
69+
}
6470
})
6571

66-
it('returns 500 if there is a problem with the request', () => {
72+
it('returns 500 if there is a problem with the request', async () => {
6773
const failingApp = lambda.deferred(() => Promise.resolve(() => {throw new Error('failed')}));
6874
const myEvent = {
6975
path: "/static/file.png",
@@ -73,15 +79,13 @@ describe('integration for deferred app', () => {
7379
isBase64Encoded: false,
7480
body: null
7581
}
76-
return failingApp(myEvent)
77-
.then((res) => {
78-
expect(res).toEqual({
79-
body: "",
80-
isBase64Encoded: false,
81-
multiValueHeaders: {},
82-
statusCode: 500,
83-
})
84-
});
82+
const res = await failingApp(myEvent)
83+
expect(res).toEqual({
84+
body: "",
85+
isBase64Encoded: false,
86+
multiValueHeaders: {},
87+
statusCode: 500,
88+
})
8589
})
8690

8791
})

0 commit comments

Comments
 (0)