Jest test issue, aws secret manager .then method

Viewed 33

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.

1 Answers

The error you are getting is caused because the promise method returned by getSecretValue is supposed to return a promise (that's why you can chain the then method.

In your mock you are returning a then method but it is not at the correct level.

You can return a promise in your mockgetSecretValue method:

const mockgetSecretValue = jest.fn((SecretId) => {
  switch (SecretId) {
    default:
      return Promise.resolve({
        SecretString: ('Super duper secret'),
      });
  }
});
Related