Bruke med DynamoDB
Unofficial Beta Translation
This page was AI-translated by PageTurner (beta). Not officially endorsed by the project. Found an error? Report issue →
Med Global oppsett/opprydding og Asynkron testmiljø API-er kan Jest fungere sømløst med DynamoDB.
Bruk jest-dynamodb forhåndsinnstilling
Jest DynamoDB tilbyr all nødvendig konfigurasjon for å kjøre testene dine med DynamoDB.
- Først installer
@shelf/jest-dynamodb
- npm
- Yarn
- pnpm
- Bun
npm install --save-dev @shelf/jest-dynamodb
yarn add --dev @shelf/jest-dynamodb
pnpm add --save-dev @shelf/jest-dynamodb
bun add --dev @shelf/jest-dynamodb
- Spesifiser forhåndsinnstillingen i Jest-konfigurasjonen din:
{
"preset": "@shelf/jest-dynamodb"
}
- Opprett
jest-dynamodb-config.jsog definer DynamoDB-tabeller
module.exports = {
tables: [
{
TableName: `files`,
KeySchema: [{AttributeName: 'id', KeyType: 'HASH'}],
AttributeDefinitions: [{AttributeName: 'id', AttributeType: 'S'}],
ProvisionedThroughput: {ReadCapacityUnits: 1, WriteCapacityUnits: 1},
},
// etc
],
};
- Konfigurer DynamoDB-klienten
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);
- Skriv tester
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',
});
});
Det er ikke nødvendig å laste inn noen avhengigheter.
Se dokumentasjonen for detaljer.