Package Exports
- jest-dynalite
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jest-dynalite) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
jest-dynalite
Enchaned unit testing, with a mock DynamoDB instance
jest-dynalite
is a fork of @shelf/jest-dynamodb
, and allows unit tests to execute real
queries against a local DynamoDB instance.
Behaviour
jest-dynalite
runs a dynalite instance per test runner, which means
test runners do not interfere.
jest-dynalite
clears tables between tests by default.
Installation
yarn add jest-dynalite -D
Config
In your package root, create a jest-dynalite-config.json
with the tables schemas,
and an optional basePort
to run dynalite on:
{
"tables": [
{
"TableName": "table",
"KeySchema": [{ "AttributeName": "id", "KeyType": "HASH" }],
"AttributeDefinitions": [{ "AttributeName": "id", "AttributeType": "S" }],
"ProvisionedThroughput": {
"ReadCapacityUnits": 1,
"WriteCapacityUnits": 1
}
}
],
"basePort": 8000
}
Update your sourcecode
const client = new DocumentClient({
...yourConfig,
...(process.env.MOCK_DYNAMODB_ENDPOINT && {
endpoint: process.env.MOCK_DYNAMODB_ENDPOINT,
sslEnabled: false,
region: "local"
})
});
process.env.MOCK_DYNAMODB_ENDPOINT
is unqiue to each test runner.
Jest config
Simple usage (preset)
jest.config.js
module.exports = {
...
preset: "jest-dynalite"
}
The simple preset config will use the config and clear tables between tests by default.
This the recommended usage, unless you have custom setupFilesAfterEnv
or testEnvironment
set.
More advanced
setup.js
import "jest-dynalite/setupTables";
// Optional (but recommended)
import "jest-dynalite/clearAfterEach";
jest.config.js
module.exports = {
...
testEnvironment: "jest-dynalite/environment",
setupFilesAfterEnv: ["./setup.js"]
}
This setup should be used if you want to override the default config of clearAfterEach
.
Most advanced
Specify the config dir
setupBeforeEnv.js
import { setup } from "jest-dynalite";
// You must give it a config directory
setup(__dirname);
setupAfterEnv.js
import { startDb, stopDb, createTables, deleteTables } from "jest-dynalite";
beforeAll(startDb);
// Create tables but don't delete them after tests
beforeAll(createTables);
// or
beforeEach(createTables);
afterEach(deleteTables);
afterAll(stopDb);
jest.config.js
module.exports = {
...
setupFiles: ["./setupBeforeEnv.js"],
setupFilesAfterEnv: ["./setupAfterEnv.js"]
}
This is by far the most complicated setup, but provides the ability to specifiy
an environment other than jest-dynalite
, and also allows you to specify a config directory.
License
MIT