Skip to content

Commit 519fd2f

Browse files
committed
added new handler for test section, addressed PR feedback
1 parent 035757a commit 519fd2f

File tree

6 files changed

+75
-23
lines changed

6 files changed

+75
-23
lines changed

docs/utilities/parser.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -252,35 +252,35 @@ We recommend to use the types provided by the parser utility. If you encounter a
252252

253253
## Testing your code
254254

255-
When testing your handler with **parser decorator** you need to use double assetion to bypass TypeScript type checking in your tests.
255+
When testing your handler with [**parser decorator**](#parse-events) you need to use double assetion to bypass TypeScript type checking in your tests.
256256
This is useful when you want to test the handler for invalid payloads or when you want to test the error handling.
257257
If you are you use middy middleware, you don't need to do this.
258258

259259
=== "handlerDecorator.test.ts"
260260

261-
```typescript hl_lines="29"
261+
```typescript hl_lines="26"
262262
--8<-- "examples/snippets/parser/unitTestDecorator.ts"
263263
```
264-
264+
265265
1. Use double assertion `as unknown as X` to bypass TypeScript type checking in your tests
266266

267267
=== "handlerDecorator.ts"
268268

269269
```typescript
270-
--8<-- "examples/snippets/parser/decorator.ts"
270+
--8<-- "examples/snippets/parser/handlerDecorator.ts"
271271
```
272272

273273
=== "schema.ts"
274274

275-
```typescript"
275+
```typescript
276276
--8<-- "examples/snippets/parser/schema.ts"
277277
```
278278

279279
This also works when using `safeParse` option.
280280

281281
=== "handlerSafeParse.test.ts"
282282

283-
```typescript hl_lines="24-32 38 48"
283+
```typescript hl_lines="21-29 35 45"
284284
--8<-- "examples/snippets/parser/unitTestSafeParse.ts"
285285
```
286286

@@ -289,11 +289,11 @@ This also works when using `safeParse` option.
289289
=== "handlerSafeParse.ts"
290290

291291
```typescript
292-
--8<-- "examples/snippets/parser/safeParseDecorator.ts"
292+
--8<-- "examples/snippets/parser/handlerSafeParseDecorator.ts"
293293
```
294294

295295
=== "schema.ts"
296296

297-
```typescript"
297+
```typescript
298298
--8<-- "examples/snippets/parser/schema.ts"
299299
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { Context } from 'aws-lambda';
2+
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
3+
import { parser } from '@aws-lambda-powertools/parser';
4+
import { Logger } from '@aws-lambda-powertools/logger';
5+
import { orderSchema, type Order } from './schema.js';
6+
7+
const logger = new Logger();
8+
9+
class Lambda implements LambdaInterface {
10+
@parser({ schema: orderSchema })
11+
public async handler(event: Order, _context: Context): Promise<number> {
12+
logger.info('Processing event', { event });
13+
14+
// ... business logic
15+
return event.id;
16+
}
17+
}
18+
19+
const myFunction = new Lambda();
20+
export const handler = myFunction.handler.bind(myFunction);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { Context } from 'aws-lambda';
2+
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
3+
import { parser } from '@aws-lambda-powertools/parser';
4+
import { Logger } from '@aws-lambda-powertools/logger';
5+
import { orderSchema, type Order } from './schema.js';
6+
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes';
7+
import type {
8+
ParsedResult,
9+
EventBridgeEvent,
10+
} from '@aws-lambda-powertools/parser/types';
11+
12+
const logger = new Logger();
13+
14+
class Lambda implements LambdaInterface {
15+
@parser({
16+
schema: orderSchema,
17+
envelope: EventBridgeEnvelope,
18+
safeParse: true,
19+
})
20+
public async handler(
21+
event: ParsedResult<EventBridgeEvent, Order>,
22+
_context: Context
23+
): Promise<number> {
24+
logger.info('Processing event', { event });
25+
if (event.success) {
26+
// ... business logic
27+
return event.data.id;
28+
} else {
29+
logger.error('Failed to parse event', { event });
30+
throw new Error('Failed to parse event');
31+
}
32+
}
33+
}
34+
35+
const myFunction = new Lambda();
36+
export const handler = myFunction.handler.bind(myFunction);

examples/snippets/parser/schema.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ const orderSchema = z.object({
1313
optionalField: z.string().optional(),
1414
});
1515

16-
export { orderSchema };
16+
type Order = z.infer<typeof orderSchema>;
17+
18+
export { orderSchema, type Order };

examples/snippets/parser/unitTestDecorator.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import { orderSchema } from 'examples/snippets/parser/schema';
2-
import { Context } from 'aws-lambda';
3-
import { handler } from 'examples/snippets/parser/decorator';
4-
import { z } from 'zod';
1+
import type { Context } from 'aws-lambda';
2+
import type { Order } from './schema.js';
3+
import { handler } from './decorator.js';
54

65
describe('Test handler', () => {
7-
type Order = z.infer<typeof orderSchema>;
8-
96
it('should parse event successfully', async () => {
107
const testEvent = {
118
id: 123,
@@ -19,7 +16,7 @@ describe('Test handler', () => {
1916
],
2017
};
2118

22-
await expect(handler(testEvent, {} as Context)).resolves.not.toThrow();
19+
await expect(handler(testEvent, {} as Context)).resolves.toEqual(123);
2320
});
2421

2522
it('should throw error if event is invalid', async () => {

examples/snippets/parser/unitTestSafeParse.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { orderSchema } from 'examples/snippets/parser/schema';
2-
import { Context } from 'aws-lambda';
3-
import { handler } from 'examples/snippets/parser/safeParseDecorator';
4-
import { z } from 'zod';
1+
import type { Order } from './schema.js';
2+
import type { Context } from 'aws-lambda';
3+
import { handler } from './safeParseDecorator.js';
54
import {
65
ParsedResult,
76
EventBridgeEvent,
87
} from '@aws-lambda-powertools/parser/types';
98

109
describe('Test handler', () => {
11-
type Order = z.infer<typeof orderSchema>;
12-
1310
it('should parse event successfully', async () => {
1411
const testEvent = {
1512
version: '0',
@@ -38,7 +35,7 @@ describe('Test handler', () => {
3835
testEvent as unknown as ParsedResult<EventBridgeEvent, Order>, // (1)!
3936
{} as Context
4037
)
41-
).resolves.not.toThrow();
38+
).resolves.toEqual(10876546789);
4239
});
4340

4441
it('should throw error if event is invalid', async () => {

0 commit comments

Comments
 (0)