When implementing unit testing with the help of the JEST framework, I have problems with the AWS SDK.
Attached is the api.test.js which calls the handler function of the api.js which makes an AWS SDK call to SSM to get the credentials to a DB connect in the config.js.
The unit test runs fine and pass, but the test cannot be finished with the message "Jest has detected the following 1 open handle potentially keeping Jest from exiting: --> TLSWRAP", which causes by the call to the SSM API.
Is there a possible solution to close this open connection?
api.test.js
test('API GET/{ID}', async () => {
const event = {httpMethod: "GET", pathParameters: {id: 1234}};
const getResult = await api.handler(event);
expect(getResult).toBeDefined();
expect(typeof getResult).toBe('object');
});
api.js
module.exports.handler = async event => {
if (typeof connection === undefined) {
let config = await getDBConfigFromSSM();
/* CREATE DB CONNECTION */
}
/* MAKE DB CALL */
};
config.js
const AWS = require('aws-sdk');
const ssm = new AWS.SSM();
module.exports.getDBConfigFromSSM = async () => {
const params = {
Name: `PATH/TO/db-config`,
WithDecryption: true,
};
try {
return await ssm.getParameter(params).promise();
} catch (ex) {
console.error(ex);
console.log('ERROR', ex);
return false;
}
};