ServerlessSpy

DynamoDB - ServerlessSpy

DynamoDB

DynamoDB integration subscribes to DynamoDb Stream and intercepts events. The event has the following properties:

  • keys
  • newImage
  • oldImage
  • eventName (INSERT, MODIFY, REMOVE)

You can use eventName and compare newImage and oldImage to validate if the correct operation has been executed.

Basic example:

await serverlessSpyListener.waitForDynamoDBMyTable<TestData>();

You can use conditions to wait for a particular event. You can also use generic (TestData) to strongly type the event.

await serverlessSpyListener.waitForDynamoDBMyTable<TestData>({
  condition: (d) => d.keys.pk === id,
});

Validating the data with Jest would look like this:

(
  await serverlessSpyListener.waitForDynamoDBMyTable<TestData>({
    condition: (d) => d.keys.pk === id,
  })
).toMatchObject({
  eventName: 'INSERT',
  newImage: data,
});

**⚠️Warning: DynamoDB can have only two Lambdas subscribed to DynamoDB Streams. If your primary stack already has two, you can not use DynamoDB interception. You should exclude DynamoDB interception in your CDK stack:

serverlessSpy.spy({
  spyDynamoDB: false,
});

Check this test.