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
https://github.com/awslabs/dynamodb-data-mapper-js

Mocking

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(() => {
    jest.restoreAllMocks()
  })
})

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

Categories: Others

Leave a Reply

Your email address will not be published. Required fields are marked *