-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlambdaToDynamoDb.test.ts
More file actions
71 lines (60 loc) · 2.04 KB
/
lambdaToDynamoDb.test.ts
File metadata and controls
71 lines (60 loc) · 2.04 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import * as fs from 'fs';
import * as path from 'path';
import { LambdaClient, InvokeCommand } from '@aws-sdk/client-lambda';
import { v4 as uuidv4 } from 'uuid';
import { TestData } from './TestData';
import { createServerlessSpyListener } from '../../../listener/createServerlessSpyListener';
import { ServerlessSpyListener } from '../../../listener/ServerlessSpyListener';
import { ServerlessSpyEvents } from '../serverlessSpyEvents/ServerlessSpyEventsLambdaToDynamoDbStack';
jest.setTimeout(30000);
describe('Lambda to DynamoDB', () => {
const exportLocation = path.join(__dirname, '../cdkOutput.json');
let serverlessSpyListener: ServerlessSpyListener<ServerlessSpyEvents>;
if (!fs.existsSync(exportLocation)) {
throw new Error(`File ${exportLocation} does not exists.`);
}
const output = JSON.parse(fs.readFileSync(exportLocation).toString())[
'ServerlessSpyLambdaToDynamoDb'
];
beforeEach(async () => {
serverlessSpyListener =
await createServerlessSpyListener<ServerlessSpyEvents>({
serverlessSpyWsUrl: output.ServerlessSpyWsUrl,
});
});
afterEach(async () => {
serverlessSpyListener.stop();
});
test('Basic test', async () => {
const lambdaClient = new LambdaClient({});
const id = uuidv4();
const data = <TestData>{
id,
message: 'Hello',
};
const command = new InvokeCommand({
FunctionName: output.FunctionNameMyLambda,
InvocationType: 'RequestResponse',
LogType: 'Tail',
Payload: JSON.stringify(data) as any,
});
await lambdaClient.send(command);
(
await (
await serverlessSpyListener.waitForFunctionMyLambdaRequest<TestData>({
condition: (d) => d.request.id === id,
})
)
.toMatchObject({ request: data })
.followedByResponse({})
).toMatchObject({ response: data });
(
await serverlessSpyListener.waitForDynamoDBMyTable<TestData>({
condition: (d) => d.keys.pk === id,
})
).toMatchObject({
eventName: 'INSERT',
newImage: data,
});
});
});