Simple Mocking of DynamoDb data mapper in the Jest code

DynamoDB Data Mapper is an awslab’s open source project. This is very helpful as the ORM library for your application. But for testing, there is something tricky because Query and Scan return QueryIterator(nearby AsyncIterator)

DynamoDB data mapper


import { DataMapper } from '@aws/dynamodb-data-mapper'

describe('test', () => {
  beforeEach(() => {
    const asyncIteratorMock = new Object()
    asyncIteratorMock[Symbol.asyncIterator] = async function*() {
      yield 'hoge'
      yield 'hage'
      yield 'huge'
    jest.spyOn(DataMapper.prototype, 'query').mockImplementation(() => {
      return asyncIteratorMock

  afterEach(() => {

I’m happy to become this example can be helpful to you.