Skip to content

Commit

Permalink
fix: kill dynamodb connection after use
Browse files Browse the repository at this point in the history
  • Loading branch information
freshollie committed Jan 3, 2021
1 parent d515f71 commit 3b9f324
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,17 @@ yarn add jest-dynalite -D
Please follow the [below config](#config) to setup your tests to use `jest-dynalite`. However, if you are looking for
some example project structures, please see the [examples](https://github.com/freshollie/jest-dynalite/tree/master/e2e).

### aws-sdk v3

With `@aws-sdk/client-dynamodb` it seems that the connection handler stays active (that is, it doesn't die) for a few seconds
after your tests have finished. Make sure you run `client.destroy()` on your client after every test suite
or your tests suites will take much longer than they should to run. See an example [here](##Update-your-sourcecode)

## Timeouts

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](###Advanced-setup).

Expand Down Expand Up @@ -123,7 +130,7 @@ module.exports = {
## Update your sourcecode

```javascript
const client = new DocumentClient({
const client = new DynamoDB({
...yourConfig,
...(process.env.MOCK_DYNAMODB_ENDPOINT && {
endpoint: process.env.MOCK_DYNAMODB_ENDPOINT,
Expand All @@ -135,6 +142,15 @@ const client = new DocumentClient({

`process.env.MOCK_DYNAMODB_ENDPOINT` is unqiue to each test runner.

After all your tests, make sure you destroy your client.
You can even do this by adding this after all in a [`setupFilesAfterEnv`](https://jestjs.io/docs/en/configuration#setupfilesafterenv-array) file.

```javascript
afterAll(() => {
client.destroy();
});
```

## Jest config

### Simple usage (preset)
Expand Down
1 change: 0 additions & 1 deletion jest.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ module.exports = {
coveragePathIgnorePatterns: ["/tests/", "/__testdir__/"],
collectCoverageFrom: ["<rootDir>/src/**/*.ts", "!<rootDir>/**/*.js"],
testPathIgnorePatterns: ["/node_modules/", "/e2e/", "/src/"],
testTimeout: 10000,
};
3 changes: 3 additions & 0 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export const start = async (): Promise<void> => {
};

export const stop = async (): Promise<void> => {
if (hasV3()) {
dynamodbv3.killConnection();
}
if (dynaliteInstance.listening) {
await new Promise<void>((resolve) =>
dynaliteInstance.close(() => resolve())
Expand Down
4 changes: 4 additions & 0 deletions src/dynamodb/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,7 @@ export const createTables = (
)
);
});

export const killConnection = (): void => {
connection?.dynamoDB.destroy();
};

0 comments on commit 3b9f324

Please sign in to comment.