1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

DynamoDbDataMapperのQuery(やScan)をJestでシンプルにMockする(typescript)

Last updated at Posted at 2019-06-19

背景

DynamoDB data mapper は、annotationなど、ORMな機能が一通りついていて扱いやすいのはよいのですが、
Query,Scanなどのメソッドが QueryIterator(=> AsyncIterator)型で返却され、テストでのMockingを少し考える必要がありました。
以下のコードでシンプルにMockできるので、お役に立てば幸いです。

DynamoDB data mapper
https://github.com/awslabs/dynamodb-data-mapper-js

Mock

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'
    }
    // scanも同じです。
    jest.spyOn(DataMapper.prototype, 'query').mockImplementation(() => {
      return asyncIteratorMock
    })
  })

  afterEach(() => {
    jest.restoreAllMocks()
  })
})
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?