-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathunitTestSafeParse.ts
51 lines (48 loc) · 1.32 KB
/
unitTestSafeParse.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import type {
EventBridgeEvent,
ParsedResult,
} from '@aws-lambda-powertools/parser/types';
import type { Context } from 'aws-lambda';
import { describe, expect, it } from 'vitest';
import { handler } from './safeParseDecorator.js';
import type { Order } from './schema.js';
describe('Test handler', () => {
it('should parse event successfully', async () => {
const testEvent = {
version: '0',
id: '6a7e8feb-b491-4cf7-a9f1-bf3703467718',
'detail-type': 'OrderPurchased',
source: 'OrderService',
account: '111122223333',
time: '2020-10-22T18:43:48Z',
region: 'us-west-1',
resources: ['some_additional'],
detail: {
id: 10876546789,
description: 'My order',
items: [
{
id: 1015938732,
quantity: 1,
description: 'item xpto',
},
],
},
};
await expect(
handler(
testEvent as unknown as ParsedResult<EventBridgeEvent, Order>, // (1)!
{} as Context
)
).resolves.toEqual(10876546789);
});
it('should throw error if event is invalid', async () => {
const testEvent = { foo: 'bar' };
await expect(
handler(
testEvent as unknown as ParsedResult<EventBridgeEvent, Order>,
{} as Context
)
).rejects.toThrow();
});
});