Jest 中文网

Jest 中文网

  • 中文文档
  • API
  • 帮助
  • Blog
  • GitHub
  • 英文官网

›使用指南

简介

  • 快速入门
  • Using Matchers
  • Testing Asynchronous Code
  • Setup and Teardown
  • Mock Functions
  • Jest Platform
  • Jest Community
  • More Resources

使用指南

  • Snapshot Testing
  • An Async Example
  • Timer Mocks
  • Manual Mocks
  • ES6 Class Mocks
  • Bypassing module mocks
  • ECMAScript Modules
  • Using with webpack
  • Using with puppeteer
  • Using with MongoDB
  • Using with DynamoDB
  • DOM Manipulation
  • Watch Plugins
  • Migrating to Jest
  • Troubleshooting
  • Architecture

框架指南

  • Testing React Apps
  • Testing React Native Apps
  • Testing Web Frameworks

API 手册

  • Globals
  • Expect
  • Mock Functions
  • The Jest Object
  • Configuring Jest
  • Jest CLI Options
  • Environment Variables
  • Code Transformation
Edit

Using with DynamoDB

With the Global Setup/Teardown and Async Test Environment APIs, Jest can work smoothly with DynamoDB.

Use jest-dynamodb Preset

Jest DynamoDB provides all required configuration to run your tests using DynamoDB.

  1. First, install @shelf/jest-dynamodb
yarn add @shelf/jest-dynamodb --dev
  1. Specify preset in your Jest configuration:
{
  "preset": "@shelf/jest-dynamodb"
}
  1. Create jest-dynamodb-config.js and define DynamoDB tables

See Create Table API

module.exports = {
  tables: [
    {
      TableName: `files`,
      KeySchema: [{AttributeName: 'id', KeyType: 'HASH'}],
      AttributeDefinitions: [{AttributeName: 'id', AttributeType: 'S'}],
      ProvisionedThroughput: {ReadCapacityUnits: 1, WriteCapacityUnits: 1},
    },
    // etc
  ],
};
  1. Configure DynamoDB client
const {DocumentClient} = require('aws-sdk/clients/dynamodb');

const isTest = process.env.JEST_WORKER_ID;
const config = {
  convertEmptyValues: true,
  ...(isTest && {
    endpoint: 'localhost:8000',
    sslEnabled: false,
    region: 'local-env',
  }),
};

const ddb = new DocumentClient(config);
  1. Write tests
it('should insert item into table', async () => {
  await ddb
    .put({TableName: 'files', Item: {id: '1', hello: 'world'}})
    .promise();

  const {Item} = await ddb.get({TableName: 'files', Key: {id: '1'}}).promise();

  expect(Item).toEqual({
    id: '1',
    hello: 'world',
  });
});

There's no need to load any dependencies.

See documentation for details.

← Using with MongoDBDOM Manipulation →
  • Use jest-dynamodb Preset
Jest 中文网
中文文档
快速入门使用指南API 参考手册
社区
Stack OverflowReactifluxTwitter
更多
BlogGitHubStar
友链
Bootstrap中文网ReactNext.jsNuxt.jsBlitz.jsDocusaurusGatsbyWebpackNPMYarn
Facebook Open Source
Copyright © 2020 Facebook Inc.
Jest 项目及相关资源的版权归 Facebook 或项目贡献者所有
Jest 中文文档遵循 MIT 开源协议
京ICP备15031610号-38