1 | const { invoke } = require('../../test-helpers')
|
2 | const middy = require('../../core')
|
3 | const doNotWaitForEmptyEventLoop = require('../')
|
4 |
|
5 | describe('🥃 Do Not Wait For Empty Event Loop', () => {
|
6 | describe('👌 With Default Options', () => {
|
7 | test('It should set callbackWaitsForEmptyEventLoop to false by default', async () => {
|
8 | const handler = middy((event, context, cb) => {
|
9 | cb()
|
10 | }).use(doNotWaitForEmptyEventLoop())
|
11 |
|
12 | const context = {}
|
13 |
|
14 | await invoke(handler, {}, context)
|
15 |
|
16 | expect(context.callbackWaitsForEmptyEventLoop).toEqual(false)
|
17 | })
|
18 |
|
19 | test('callbackWaitsForEmptyEventLoop should remain true if was overridden by user in handler', async () => {
|
20 | const handler = middy((event, context, cb) => {
|
21 | context.callbackWaitsForEmptyEventLoop = true
|
22 | cb()
|
23 | }).use(doNotWaitForEmptyEventLoop())
|
24 |
|
25 | const context = {}
|
26 |
|
27 | await invoke(handler, {}, context)
|
28 |
|
29 | expect(context.callbackWaitsForEmptyEventLoop).toEqual(true)
|
30 | })
|
31 |
|
32 | test('callbackWaitsForEmptyEventLoop should stay false if handler has error', async () => {
|
33 | const handler = middy((event, context, cb) => {
|
34 | cb(new Error('!'))
|
35 | })
|
36 |
|
37 | handler.use(doNotWaitForEmptyEventLoop())
|
38 |
|
39 | const context = {}
|
40 |
|
41 | try {
|
42 | await invoke(handler, {}, context)
|
43 | } catch (e) {}
|
44 |
|
45 | expect(context.callbackWaitsForEmptyEventLoop).toEqual(false)
|
46 | })
|
47 | })
|
48 |
|
49 | describe('✍️ With Overridden Options', () => {
|
50 | test('callbackWaitsForEmptyEventLoop should be false when runOnAfter is true in options', async () => {
|
51 | const handler = middy((event, context, cb) => {
|
52 | context.callbackWaitsForEmptyEventLoop = true
|
53 | cb()
|
54 | })
|
55 |
|
56 | handler.use(doNotWaitForEmptyEventLoop({
|
57 | runOnAfter: true
|
58 | }))
|
59 |
|
60 | const context = {}
|
61 |
|
62 | await invoke(handler, {}, context)
|
63 |
|
64 | expect(context.callbackWaitsForEmptyEventLoop).toEqual(false)
|
65 | })
|
66 |
|
67 | test('callbackWaitsForEmptyEventLoop should remain true when error occurs even if runOnAfter is true', async () => {
|
68 | const handler = middy((event, context, cb) => {
|
69 | context.callbackWaitsForEmptyEventLoop = true
|
70 | cb(new Error('!'))
|
71 | })
|
72 |
|
73 | handler.use(doNotWaitForEmptyEventLoop({
|
74 | runOnAfter: true
|
75 | }))
|
76 |
|
77 | const context = {}
|
78 |
|
79 | try {
|
80 | await invoke(handler, {}, context)
|
81 | } catch (e) {}
|
82 |
|
83 | expect(context.callbackWaitsForEmptyEventLoop).toEqual(true)
|
84 | })
|
85 |
|
86 | test('callbackWaitsForEmptyEventLoop should be false when error occurs but runOnError is true', async () => {
|
87 | const handler = middy((event, context, cb) => {
|
88 | context.callbackWaitsForEmptyEventLoop = true
|
89 | cb(new Error('!'))
|
90 | })
|
91 |
|
92 | handler.use(doNotWaitForEmptyEventLoop({
|
93 | runOnAfter: true,
|
94 | runOnError: true
|
95 | }))
|
96 |
|
97 | const context = {}
|
98 |
|
99 | try {
|
100 | await invoke(handler, {}, context)
|
101 | } catch (e) {}
|
102 |
|
103 | expect(context.callbackWaitsForEmptyEventLoop).toEqual(false)
|
104 | })
|
105 |
|
106 | test('thrown error should be propagated when it occurs & runOnError is true', async () => {
|
107 | expect.assertions(1)
|
108 |
|
109 | const handler = middy((event, context, cb) => {
|
110 | context.callbackWaitsForEmptyEventLoop = true
|
111 | cb(new Error('!'))
|
112 | })
|
113 |
|
114 | handler.use(doNotWaitForEmptyEventLoop({
|
115 | runOnAfter: true,
|
116 | runOnError: true
|
117 | }))
|
118 |
|
119 | const context = {}
|
120 |
|
121 | try {
|
122 | await invoke(handler, {}, context)
|
123 | } catch (error) {
|
124 | expect(error.message).toEqual('!')
|
125 | }
|
126 | })
|
127 |
|
128 | test('callbackWaitsForEmptyEventLoop should be false in handler but true after if set by options', async () => {
|
129 | expect.assertions(2)
|
130 |
|
131 | const handler = middy((event, context, cb) => {
|
132 | expect(context.callbackWaitsForEmptyEventLoop).toEqual(true)
|
133 | cb()
|
134 | })
|
135 |
|
136 | handler.use(doNotWaitForEmptyEventLoop({
|
137 | runOnBefore: false,
|
138 | runOnAfter: true
|
139 | }))
|
140 |
|
141 | const context = {
|
142 | callbackWaitsForEmptyEventLoop: true
|
143 | }
|
144 |
|
145 | await invoke(handler, {}, context)
|
146 |
|
147 | expect(context.callbackWaitsForEmptyEventLoop).toEqual(false)
|
148 | })
|
149 | })
|
150 | })
|