I am trying to test the following code with Jest:
exports.getSecret = (secretId) => {
const client = new AWS.SecretsManager({
region: process.env.AWS_REGION,
});
console.log(`Getting secret "${secretId}" from secret manager`);
console.log(client);
return client
.getSecretValue({ SecretId: secretId })
.promise()
.then((data) => JSON.parse(data.SecretString));
};
The test I have written is:
const AWS = require('aws-sdk');
const { getSecret } = require('./secrets');
const data = 'super duper secret';
const mockgetSecretValue = jest.fn((SecretId) => {
switch (SecretId) {
default:
return {
SecretString: ('Super duper secret'),
};
}
});
jest.mock('aws-sdk', () => {
return {
client: {
update() {
return {};
},
},
SecretsManager: jest.fn(() => {
return {
getSecretValue: jest.fn(({ SecretId }) => {
return {
promise: () => mockgetSecretValue(SecretId),
then: () => (data)
};
}),
};
}),
};
});
describe('utilities/src/aws/secrets', () => {
it('the correct response is returned', async () => {
const spy = jest.spyOn(AWS, 'SecretsManager');
const getTestSecret = getSecret(mockgetSecretValue);
const res = await getTestSecret;
expect(spy).toHaveBeenCalledTimes(1);
expect(mockgetSecretValue).toHaveBeenCalledTimes(1);
// expect(client.data).toHaveBeenCalledTimes(1);
});
});
And I keep receiving the following error:
FAIL utilities/src/aws/secrets.test.js utilities/src/aws/secrets ✕ the correct response is returned (12 ms)
● utilities/src/aws/secrets › the correct response is returned
TypeError: client.getSecretValue(...).promise(...).then is not a function
11 | .getSecretValue({ SecretId: secretId })
12 | .promise()
> 13 | .then((data) => JSON.parse(data.SecretString));
| ^
14 | };
15 |
at then (utilities/src/aws/secrets.js:13:6)
at Object.getSecret (utilities/src/aws/secrets.test.js:39:27)
------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------|---------|----------|---------|---------|-------------------
All files | 85.71 | 100 | 50 | 85.71 |
secrets.js | 85.71 | 100 | 50 | 85.71 | 13
------------|---------|----------|---------|---------|-------------------
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 0.175 s, estimated 1 s
Ran all test suites matching /secrets.test.js/i.