Skip to content

Commit 27e09d2

Browse files
Merge pull request #36 from andresWeitzel/testing-04-add-unit-test-for-dynamodb-config
testing-04-add-unit-test-for-dynamodb-config
2 parents 2f6b0fc + 7ca3b9c commit 27e09d2

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
"format-remark": "remark . --quiet --frail --output",
1313
"format-md": "remark . --output",
1414
"test": "jest --verbose",
15-
"test:watch": "jest --watch --verbose",
16-
"test:cov": "jest --coverage --verbose",
17-
"test:dates-helper": "jest --verbose ./src/test/helpers/date-time/*"
15+
"test:watch": "jest --watch --verbose --detectOpenHandles",
16+
"test:cov": "jest --coverage --verbose --detectOpenHandles",
17+
"test:dates-helper": "jest --verbose --detectOpenHandles ./src/test/helpers/date-time/*",
18+
"test:dates-dynamodb": "jest --verbose --detectOpenHandles ./src/test/helpers/dynamodb/*"
1819
},
1920
"repository": {
2021
"type": "git",

src/helpers/dynamodb/config/client.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//External
22
const { DynamoDBDocumentClient } = require('@aws-sdk/lib-dynamodb');
33
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb');
4-
//Const-vars
4+
//Const
5+
const DYNAMO_DB_CLIENT_ERROR = 'ERROR in dynamoDBClient helper function.';
6+
//Vars
57
let client;
68
let dynamo;
79
let msgResponse;
@@ -24,7 +26,7 @@ const dynamoDBClient = async () => {
2426

2527
return dynamo;
2628
} catch (error) {
27-
msgResponse = 'ERROR in dynamoDBClient() function.';
29+
msgResponse = DYNAMO_DB_CLIENT_ERROR;
2830
msgLog = msgResponse + `Caused by ${error}`;
2931
console.log(msgLog);
3032
return msgResponse;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"use strict";
2+
//Helpers
3+
const {
4+
dynamoDBClient,
5+
} = require("../../../../helpers/dynamodb/config/client");
6+
//Const
7+
const MOCK_OBJECT = {};
8+
//Vars
9+
let msg;
10+
let dynamoDBClientResult;
11+
12+
describe("- dynamoDBClient helper (Unit Test)", () => {
13+
describe("1) Check cases for arguments.", () => {
14+
msg =
15+
"Should return a object type if no arguments are passed (This function does not expect arguments)";
16+
it(msg, async () => {
17+
dynamoDBClientResult = await dynamoDBClient();
18+
await expect(typeof dynamoDBClientResult == "object").toBe(true);
19+
});
20+
21+
msg =
22+
"Should return a object type if one argument is passed (This function does not expect arguments)";
23+
it(msg, async () => {
24+
dynamoDBClientResult = await dynamoDBClient(MOCK_OBJECT);
25+
await expect(typeof dynamoDBClientResult == "object").toBe(true);
26+
});
27+
28+
msg =
29+
"Should return a object type if two argument are passed (This function does not expect arguments)";
30+
it(msg, async () => {
31+
dynamoDBClientResult = await dynamoDBClient(MOCK_OBJECT, MOCK_OBJECT);
32+
await expect(typeof dynamoDBClientResult == "object").toBe(true);
33+
});
34+
35+
msg =
36+
"Should return a object type if a null value is passed (This function does not expect arguments)";
37+
it(msg, async () => {
38+
dynamoDBClientResult = await dynamoDBClient(null);
39+
await expect(typeof dynamoDBClientResult == "object").toBe(true);
40+
});
41+
42+
msg =
43+
"Should return a object type if a undefined value is passed (This function does not expect arguments)";
44+
it(msg, async () => {
45+
dynamoDBClientResult = await dynamoDBClient(undefined);
46+
await expect(typeof dynamoDBClientResult == "object").toBe(true);
47+
});
48+
});
49+
50+
describe("2) Check cases for error.", () => {
51+
msg =
52+
"Should not return a error message and not throw an Error if no argument is passed to the function (This function does not expect arguments).";
53+
it(msg, async () => {
54+
await expect(async () => await dynamoDBClient()).not.toThrow(Error);
55+
});
56+
msg =
57+
"Should not return a error message and not throw an Error if a new Error is passed to the function (This function does not expect arguments).";
58+
it(msg, async () => {
59+
await expect(async () => await dynamoDBClient(new Error())).not.toThrow(
60+
Error
61+
);
62+
});
63+
});
64+
});

0 commit comments

Comments
 (0)