Skip to content

Commit ad0410f

Browse files
feat: upgrade to middy v2.x
1 parent 98ee3b4 commit ad0410f

File tree

29 files changed

+178
-493
lines changed

29 files changed

+178
-493
lines changed

packages/lambda-powertools-middleware-correlation-ids/__tests__/dynamodb.js

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const captureCorrelationIds = require('../index')
77

88
global.console.log = jest.fn()
99

10-
const invokeDynamoHandler = (event, awsRequestId, sampleDebugLogRate, handlerF, recordF, done) => {
11-
const handler = middy((event, context, cb) => {
10+
const invokeDynamoHandler = async (event, awsRequestId, sampleDebugLogRate, handlerF, recordF) => {
11+
const handler = middy(async (event, context) => {
1212
// check the correlation IDs outside the context of a record are correct
1313
handlerF(CorrelationIds.get())
1414

@@ -18,18 +18,10 @@ const invokeDynamoHandler = (event, awsRequestId, sampleDebugLogRate, handlerF,
1818

1919
// check the correlation IDs outside the context of a record are correct
2020
handlerF(CorrelationIds.get())
21-
22-
cb(null)
2321
})
2422
handler.use(captureCorrelationIds({ sampleDebugLogRate }))
2523

26-
handler(event, { awsRequestId }, (err, result) => {
27-
if (err) {
28-
throw err
29-
}
30-
31-
if (done) done()
32-
})
24+
await handler(event, { awsRequestId })
3325
}
3426

3527
const dynamo = require('./event-templates/dynamo-new-old.json')
@@ -59,9 +51,9 @@ const genDynamoEventWithoutNewImage = () => {
5951

6052
const dynamoTests = () => {
6153
describe('when sampleDebugLogRate = 0', () => {
62-
it('always sets debug-log-enabled to false', () => {
54+
it('always sets debug-log-enabled to false', async () => {
6355
const requestId = uuid()
64-
invokeDynamoHandler(genDynamoEvent(), requestId, 0,
56+
await invokeDynamoHandler(genDynamoEvent(), requestId, 0,
6557
x => {
6658
expect(x['awsRequestId']).toBe(requestId)
6759
expect(x['debug-log-enabled']).toBe('false')
@@ -75,9 +67,9 @@ const dynamoTests = () => {
7567
})
7668

7769
describe('when event lacks NewImage', () => {
78-
it('should set default correlation id', () => {
70+
it('should set default correlation id', async () => {
7971
const requestId = uuid()
80-
invokeDynamoHandler(genDynamoEventWithoutNewImage(), requestId, 0,
72+
await invokeDynamoHandler(genDynamoEventWithoutNewImage(), requestId, 0,
8173
x => {
8274
expect(x['awsRequestId']).toBe(requestId)
8375
expect(x['debug-log-enabled']).toBe('false')
@@ -92,9 +84,9 @@ const dynamoTests = () => {
9284
})
9385

9486
describe('when sampleDebugLogRate = 1', () => {
95-
it('always sets debug-log-enabled to true', () => {
87+
it('always sets debug-log-enabled to true', async () => {
9688
const requestId = uuid()
97-
invokeDynamoHandler(genDynamoEvent(), requestId, 1,
89+
await invokeDynamoHandler(genDynamoEvent(), requestId, 1,
9890
x => {
9991
expect(x['awsRequestId']).toBe(requestId)
10092
expect(x['debug-log-enabled']).toBe('true')
@@ -108,9 +100,9 @@ const dynamoTests = () => {
108100
})
109101

110102
describe('when correlation ID is not provided in the event', () => {
111-
it('sets it to the AWS Request ID', () => {
103+
it('sets it to the AWS Request ID', async () => {
112104
const requestId = uuid()
113-
invokeDynamoHandler(genDynamoEvent(), requestId, 0,
105+
await invokeDynamoHandler(genDynamoEvent(), requestId, 0,
114106
x => {
115107
// correlation IDs at the handler level
116108
expect(x['x-correlation-id']).toBe(requestId)
@@ -126,9 +118,9 @@ const dynamoTests = () => {
126118
})
127119

128120
describe('when call-chain-length is not provided in the event', () => {
129-
it('sets it to 1', () => {
121+
it('sets it to 1', async () => {
130122
const requestId = uuid()
131-
invokeDynamoHandler(genDynamoEvent(), requestId, 0,
123+
await invokeDynamoHandler(genDynamoEvent(), requestId, 0,
132124
x => {}, // n/a
133125
record => {
134126
const x = record.correlationIds.get()
@@ -144,7 +136,7 @@ const dynamoTests = () => {
144136
let userId
145137
let requestId
146138

147-
beforeEach((done) => {
139+
beforeEach(async () => {
148140
id = uuid()
149141
userId = uuid()
150142

@@ -157,11 +149,11 @@ const dynamoTests = () => {
157149

158150
const event = genDynamoEvent(correlationIds)
159151
requestId = uuid()
160-
invokeDynamoHandler(event, requestId, 0, x => {
152+
await invokeDynamoHandler(event, requestId, 0, x => {
161153
handlerCorrelationIds = x
162154
}, aRecord => {
163155
record = aRecord
164-
}, done)
156+
})
165157
})
166158

167159
it('still has the correct handler correlation IDs', () => {
@@ -195,7 +187,7 @@ const dynamoTests = () => {
195187
let record
196188
let id
197189

198-
beforeEach((done) => {
190+
beforeEach(async () => {
199191
id = uuid()
200192

201193
const correlationIds = {
@@ -204,10 +196,9 @@ const dynamoTests = () => {
204196
}
205197

206198
const event = genDynamoEvent(correlationIds)
207-
invokeDynamoHandler(event, uuid(), 0,
199+
await invokeDynamoHandler(event, uuid(), 0,
208200
() => {},
209-
aRecord => { record = aRecord },
210-
done)
201+
aRecord => { record = aRecord })
211202
})
212203

213204
it('increments it by 1', () => {

packages/lambda-powertools-middleware-correlation-ids/__tests__/firehose.js

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const captureCorrelationIds = require('../index')
66

77
global.console.log = jest.fn()
88

9-
const invokeFirehoseHandler = (event, awsRequestId, sampleDebugLogRate, handlerF, recordF, done) => {
10-
const handler = middy((event, context, cb) => {
9+
const invokeFirehoseHandler = async (event, awsRequestId, sampleDebugLogRate, handlerF, recordF) => {
10+
const handler = middy(async (event, context) => {
1111
// check the correlation IDs outside the context of a record are correct
1212
handlerF(CorrelationIds.get())
1313

@@ -17,18 +17,10 @@ const invokeFirehoseHandler = (event, awsRequestId, sampleDebugLogRate, handlerF
1717

1818
// check the correlation IDs outside the context of a record are correct
1919
handlerF(CorrelationIds.get())
20-
21-
cb(null)
2220
})
2321
handler.use(captureCorrelationIds({ sampleDebugLogRate }))
2422

25-
handler(event, { awsRequestId }, (err, result) => {
26-
if (err) {
27-
throw err
28-
}
29-
30-
if (done) done()
31-
})
23+
await handler(event, { awsRequestId })
3224
}
3325

3426
const firehose = require('./event-templates/firehose.json')
@@ -52,9 +44,9 @@ const genFirehoseEventWithoutJSON = (correlationIDs = {}) => {
5244

5345
const firehoseTests = () => {
5446
describe('when sampleDebugLogRate = 0', () => {
55-
it('always sets debug-log-enabled to false', () => {
47+
it('always sets debug-log-enabled to false', async () => {
5648
const requestId = uuid()
57-
invokeFirehoseHandler(genFirehoseEvent(), requestId, 0,
49+
await invokeFirehoseHandler(genFirehoseEvent(), requestId, 0,
5850
x => {
5951
expect(x['awsRequestId']).toBe(requestId)
6052
expect(x['debug-log-enabled']).toBe('false')
@@ -68,9 +60,9 @@ const firehoseTests = () => {
6860
})
6961

7062
describe('when event lacks JSON payload', () => {
71-
it('should ignore the event', () => {
63+
it('should ignore the event', async () => {
7264
const requestId = uuid()
73-
invokeFirehoseHandler(genFirehoseEventWithoutJSON(), requestId, 0,
65+
await invokeFirehoseHandler(genFirehoseEventWithoutJSON(), requestId, 0,
7466
x => {
7567
expect(x['awsRequestId']).toBe(requestId)
7668
expect(x['debug-log-enabled']).toBe('false')
@@ -83,9 +75,9 @@ const firehoseTests = () => {
8375
})
8476

8577
describe('when sampleDebugLogRate = 1', () => {
86-
it('always sets debug-log-enabled to true', () => {
78+
it('always sets debug-log-enabled to true', async () => {
8779
const requestId = uuid()
88-
invokeFirehoseHandler(genFirehoseEvent(), requestId, 1,
80+
await invokeFirehoseHandler(genFirehoseEvent(), requestId, 1,
8981
x => {
9082
expect(x['awsRequestId']).toBe(requestId)
9183
expect(x['debug-log-enabled']).toBe('true')
@@ -99,9 +91,9 @@ const firehoseTests = () => {
9991
})
10092

10193
describe('when correlation ID is not provided in the event', () => {
102-
it('sets it to the AWS Request ID', () => {
94+
it('sets it to the AWS Request ID', async () => {
10395
const requestId = uuid()
104-
invokeFirehoseHandler(genFirehoseEvent(), requestId, 0,
96+
await invokeFirehoseHandler(genFirehoseEvent(), requestId, 0,
10597
x => {
10698
// correlation IDs at the handler level
10799
expect(x['x-correlation-id']).toBe(requestId)
@@ -117,9 +109,9 @@ const firehoseTests = () => {
117109
})
118110

119111
describe('when call-chain-length is not provided in the event', () => {
120-
it('sets it to 1', () => {
112+
it('sets it to 1', async () => {
121113
const requestId = uuid()
122-
invokeFirehoseHandler(genFirehoseEvent(), requestId, 0,
114+
await invokeFirehoseHandler(genFirehoseEvent(), requestId, 0,
123115
x => {}, // n/a
124116
record => {
125117
const x = record.correlationIds.get()
@@ -135,7 +127,7 @@ const firehoseTests = () => {
135127
let userId
136128
let requestId
137129

138-
beforeEach((done) => {
130+
beforeEach(async () => {
139131
id = uuid()
140132
userId = uuid()
141133

@@ -148,11 +140,11 @@ const firehoseTests = () => {
148140

149141
const event = genFirehoseEvent(correlationIds)
150142
requestId = uuid()
151-
invokeFirehoseHandler(event, requestId, 0, x => {
143+
await invokeFirehoseHandler(event, requestId, 0, x => {
152144
handlerCorrelationIds = x
153145
}, aRecord => {
154146
record = aRecord
155-
}, done)
147+
})
156148
})
157149

158150
it('still has the correct handler correlation IDs', () => {
@@ -186,7 +178,7 @@ const firehoseTests = () => {
186178
let record
187179
let id
188180

189-
beforeEach((done) => {
181+
beforeEach(async () => {
190182
id = uuid()
191183

192184
const correlationIds = {
@@ -195,10 +187,9 @@ const firehoseTests = () => {
195187
}
196188

197189
const event = genFirehoseEvent(correlationIds)
198-
invokeFirehoseHandler(event, uuid(), 0,
190+
await invokeFirehoseHandler(event, uuid(), 0,
199191
() => {},
200-
aRecord => { record = aRecord },
201-
done)
192+
aRecord => { record = aRecord })
202193
})
203194

204195
it('increments it by 1', () => {

0 commit comments

Comments
 (0)