title: Mocking Secrets Manager module for JavaScript jest unit tests
body: <p>I'm having trouble getting the AWS Secrets Manager module mocked for the jest unit tests...  The part it errors on is the .promise().  When I remove that, the code doesn't work for the real Secrets Manager so I think it needs to stay there.  How do I mock the getSecretData function so that getSecretData.promise() will work for the mock?</p>
  
  <p>Here is the SecretsManager.js code:</p>
  
  <pre><code>import AWS from 'aws-sdk';
  
  export class SecretsManager {
    constructor() {
      AWS.config.update({
        region: 'us-east-1',
      });
      this.secretsManager = new AWS.SecretsManager();
    }
  
    async getSecretData(secretName) {
      try {
        const response = await this.secretsManager.getSecretValue({
          SecretId: secretName,
        }).promise();
        const secretString = response.SecretString;
        const parsedSecret = JSON.parse(secretString);
        return parsedSecret;
      } catch (e) {
        console.log('Failed to get data from AWS Secrets Manager.');
        console.log(e);
        throw new Error('Unable to retrieve data.');
      }
    }
  }
  
  </code></pre>
  
  <p>Here is the SecretsManager.test.js code:</p>
  
  <pre><code>import { SecretsManager } from '../utils/SecretsManager';
  
  jest.mock('aws-sdk', () =&gt; {
    return {
      config: {
        update(val) {
  
        },
      },
      SecretsManager: function () {
        return {
          async getSecretValue({
            SecretId: secretName
          }) {
            return {
              promise: function () {
                return {
                   UserName: 'test',
                   Password: 'password',
                };
              }
            };
          }
        };
      }
    }
  
  });
  
  
  describe('SecretsManager.js', () =&gt; {
    describe('Given I have a valid secret name', () =&gt; {
      describe('When I send a request for test_creds', () =&gt; {
        it('Then the correct data is returned.', async () =&gt; {
          const mockReturnValue = {
            UserName: 'test',
            Password: 'password',
          };
          const logger = getLogger();
          const secretManager = new SecretsManager();
          const result = await secretManager.getSecretData('test_creds');
          expect(result).toEqual(mockReturnValue)
        });
      });
      describe('When I send a request without data', () =&gt; {
        it('Then an error is thrown.', async () =&gt; {
        const secretManager = new SecretsManager();
        await expect(secretManager.getSecretData()).rejects.toThrow();
        });
      });
    });
  });
  
  </code></pre>
  
  <p>This is the error I get when running the tests:</p>
  
  <pre><code> this.secretsManager.getSecretValue(...).promise is not a function
  </code></pre>
  
  <p>Any suggestions or pointers are greatly appreciated!<br>
  Thank you for looking at my post.</p>