- Getting Started
- 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
- 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
- Expect
- Mock Functions
- The Jest Object
- Configuring Jest
- Jest CLI Options
- Globals
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
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.
- First install
@shelf/jest-dynamodb
yarn add @shelf/jest-dynamodb --dev
- Specify preset in your Jest configuration:
{
"preset": "@shelf/jest-dynamodb"
}
- 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
],
};
- 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);
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论