Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
jest-dynalite
Advanced tools
Enchaned unit testing, with a mock DynamoDB instance
jest-dynalite
is a fork of @shelf/jest-dynamodb that allows unit tests to execute real
queries against a local DynamoDB instance. It was created in an attempt to address some of the most important missing
features of @shelf/jest-dynamodb
, such as the fact that it uses a single shared database, which makes it hard to keep tests independent while also potentially causing race conditions because of jest's parallel execution of tests (see this issue for more information).
Using this jest-dynalite
makes writing queries with DynamoDB very easy, your tests can really
check if your data is manipulated in the way you expect it to be. This means that queries and mutations
can be developed without ever having to deploy or run your application, and significantly speeds up
writing code which interacts with DynamoDB.
This in turn makes your tests much more robust, because a change to a data structure or db query in your application will be reflected by failing tests, instead of using mocks to check if calls were made correctly.
This library could almost be seen as an integration test, but the lines are very blurred these days and I'd definitely place this within the unit testing boundary because it can easily integrate with unit tests.
java
requirementFrom v2.0.0
jest-dynalite
now uses a JavaScript file for table configuration. This change makes it possible to set the dynalite config programatically (enabling things such as reading the parameters from a cloudformation template) while also improving compatibility with jest-dynamodb. Thanks to @corollari for this change.
From v3.0.0
you can now use the preset in a monorepo. The jest-dynalite-config.js
will be picked up from your jest <rootDir>
, which should be the same directory as your jest config.
yarn add jest-dynalite -D
Because jest has a default timeout of 5000ms per test, jest-dynalite
can sometimes cause failures due to the timeout
being exceeded. This can happen when there are many tests or lots of tables to create between tests.
If this happens, try increasing your test timeouts jest.setTimeout(10000)
. Another option is to selectively
run the database only for suites which use it. Please see advanced config.
In your jest project root (next to your jest.config.js
), create a jest-dynalite-config.js
with the tables schemas,
and an optional basePort
to run dynalite on:
module.exports = {
tables: [
{
TableName: "table",
KeySchema: [{ AttributeName: "id", KeyType: "HASH" }],
AttributeDefinitions: [{ AttributeName: "id", AttributeType: "S" }],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1,
},
},
],
basePort: 8000,
};
Some data can be given to exist in the table before each test:
module.exports = {
tables: [
{
TableName: "table",
KeySchema: [{ AttributeName: "id", KeyType: "HASH" }],
AttributeDefinitions: [{ AttributeName: "id", AttributeType: "S" }],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1,
},
data: [
{
id: "a",
someattribute: "hello world",
},
],
},
],
basePort: 8000,
};
Your tables can also be resolved from an optionally async function:
module.exports = {
// Please note, this function is resolved
// once per test file
tables: async () => {
const myTables = await someFunction();
if (myTables.find((table) => ...)) {
return someOtherFunction();
}
return myTables;
},
basePort: 8000
};
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.js
module.exports = {
...
preset: "jest-dynalite"
}
The simple preset config will use the config and clear tables between tests.
Important: Only use this option if you don't have a custom testEnvironment
set in your jest.config.js
file.
If you are using your own testEnvironment
in your Jest configuration, then you must setup
jest-dynalite
manually. You should also use this manual configuration if you don't want a DynamoDB mock to run
for all your tests (faster).
setupBeforeEnv.js
import { setup } from "jest-dynalite";
// You must give it a config directory
setup(__dirname);
In every test suite where you are using DynamoDB, apply import "jest-dynalite/withDb"
to the top of
that test suite to run the db for all the tests in the suite.
If you want the tables to exist for all your suites, create a
setupAfterEnv.js
file with the content:
import "jest-dynalite/withDb";
You then must add the setup files to your jest config
jest.config.js
module.exports = {
...
setupFiles: ["./setupBeforeEnv.js"],
setupFilesAfterEnv: ["./setupAfterEnv.js"]
}
If you want to be even more granular, you can start the db yourself at any point.
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 = {
...
testEnvironment: "jest-dynalite/environment",
setupFilesAfterEnv: [
"jest-dynalite/setupTables",
// Optional (but recommended)
"jest-dynalite/clearAfterEach"
]
}
This setup should be used if you want to override the default config of clearAfterEach
, but still want to use the most simple configuration.
Clone the repo and install dependencies
yarn
Run tests
yarn test
Tests are designed as a mix of unit and integration tests.
Each run of the project is configured by .jest/features
for each different
type of configuration for jest-dynalite
MIT
FAQs
Run your tests using Jest & Dynalite
The npm package jest-dynalite receives a total of 42,316 weekly downloads. As such, jest-dynalite popularity was classified as popular.
We found that jest-dynalite demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.