Skip to content

Instantly share code, notes, and snippets.

@marcusgadbem
Last active November 8, 2022 20:36
Show Gist options
  • Save marcusgadbem/ff1dd87004c26ca2e1a66f659dc394ba to your computer and use it in GitHub Desktop.
Save marcusgadbem/ff1dd87004c26ca2e1a66f659dc394ba to your computer and use it in GitHub Desktop.
aws-sdk - dynamodb client + test
import { DynamoDBClient, DynamoDBClientConfig } from '@aws-sdk/client-dynamodb'
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'
// @aws-sdk (v3) has keepAlive enabled by default
export const DynamoClient = new DynamoDBClient({});
const marshallOptions = {
convertEmptyValues: false,
removeUndefinedValues: false,
convertClassInstanceToMap: false,
};
const unmarshallOptions = {
wrapNumbers: false,
};
const translateConfig = { marshallOptions, unmarshallOptions };
export const DynamoDocument = DynamoDBDocumentClient.from(DynamoClient, translateConfig);
import 'aws-sdk-client-mock-jest' // provides `toHaveReceivedCommandWith` assertion
import { mockClient } from 'aws-sdk-client-mock'
import { DynamoDocument } from './adapter-dynamodb'
// mock either your dynamo client instance or `DynamoClient` directly from @aws-sdk/client-dynamodb
const ddbMock = mockClient(DynamoDocument)
beforeEach(() => {
ddbMock.reset()
}
it('expects PutCommand to be called', async () => {
const payload = { amount: 4382490 }
await createLoan(payload)
await expect(ddbMock).toHaveReceivedCommandWith(PutCommand, {
TableName: 'loans',
Item: expect.objectContaining(payload)
})
})
import { PutCommand } from '@aws-sdk/lib-dynamodb'
import { DynamoDocument } from './adapter-dynamodb'
export async function createLoan(payload): Promise<any> {
await DynamoDocument.send(
new PutCommand({
TableName: 'loans',
Item: payload,
})
)
return payload
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment