2

I am trying to mock AWS SSM using aws-sdk-mock with the code below but not working. Does not throw error, fetch the values from Actual store when getParametersByPath is called.

I had a look at the aws-sdk-mock documentation but does not seem to have an example for mocking ssm, is it supported or not.

AWSMock.mock('SSM', 'getParametersByPath', (params, callback) => {
  callback(null, mockResponse);
});
Mandar
  • 31
  • 1
  • 3
  • Richard, I do not think it supports SSM so I had to mock the whole library if that helps you. I referred https://jestjs.io/docs/en/manual-mocks.html – Mandar Oct 29 '19 at 11:45
  • in the end I used localstack docker image.. works well and good for outside in tests :) – Richard Parkins Nov 01 '19 at 14:36
  • Hi @RichardParkins, I know this is a bit old but I was wondering if you had an example of using localstack SSM in you tests? I'm obviously missing something simple but I can't get it to work (I can get S3 working but not SSM). Thanks in advance – Jim M. Jan 14 '20 at 14:22
  • Hi @JimM. I got waylaid on S3 and am picking up SSM in a few days hopefully. I'll keep you up to date. You may find that localstack has been updated since my post and has better support for SSM? – Richard Parkins Jan 15 '20 at 15:29
  • Hi @RichardParkins, Turns out the problem with LocalStack & SSM was that we are using the 'spring-boot-parameter-store-integration' (https://mvnrepository.com/artifact/com.coveo/spring-boot-parameter-store-integration/1.2.0) and they start their code in 'bootstrap', and create their own default credential AWS handler. A co-worker figured out how to handle this and was able to get it working. Thanks! – Jim M. Feb 03 '20 at 14:59

1 Answers1

4

I ran across this when trying to do a similar operation: When trying to mock SSM functionality the resources were still attempting to make requests to AWS and were not using the mock functionality.

Example:

    import { mock } from 'aws-sdk-mock';
    import { SSM } from 'aws-sdk';
    import { GetParameterRequest, GetParameterResult } from 'aws-sdk/clients/ssm';
    import 'mocha'

    ...
    const ssm: SSM = new SSM();
    mock('SSM', 'getParameter', async (request: GetParameterRequest) => {
        return { Parameter: { Value: 'value' } } as GetParameterResult;
    })

    const request: GetParameterRequest = { Name: 'parameter', WithDecryption: true};

    const result: GetParameterResult = await ssm.getParameter(request).promise();
    expect(result.Parameter.Value).to.equal('value');
    ...

The error occurred when making the call to getParameter.

Turns out that the reason for our error was that we were instantiating the integration prior to declaring our mock. So the fix was to switch the order of execution and declare the mock before instantiating the integration.

Example:

    import { mock } from 'aws-sdk-mock';
    import { SSM } from 'aws-sdk';
    import { GetParameterRequest, GetParameterResult } from 'aws-sdk/clients/ssm';
    import 'mocha'

    ...
    mock('SSM', 'getParameter', async (request: GetParameterRequest) => {
        return { Parameter: { Value: 'value' } } as GetParameterResult;
    });
    // -> Note the following line was moved below the mock declaration.
    const ssm: SSM = new SSM();

    const request: GetParameterRequest = { Name: 'parameter', WithDecryption: true};

    const result: GetParameterResult = await ssm.getParameter(request).promise();
    expect(result.Parameter.Value).to.equal('value');
    ...

Isaiah
  • 484
  • 5
  • 16
  • Hi, I have been struggling with this. I have another file amazon.js where I have function getParameterFromStore(param) which uses ssm.getParameter(). I mock amozon.js with jest.mock('amazon.js'), then I mock SSM as you do in the example and when I do expect(amazon.getParameterFromStore....) it seems that SSM is not mocked. any suggestions ? – ROKIKOKI May 18 '20 at 14:09
  • It is likely an order of operations issue. Depending on if you are using classes or direct functions, the actual implementation may be a bit different, but you have to declare the mock _before_ `new SSM()`. The mock only affects instances that are created _after_ the mock is declared. If you are importing `amazon.js` at the top of your test file and somewhere in the `amazon.js` file is a `new SSM()` then that will execute before any of your test code and therefore your mock wouldn't apply. – Isaiah May 20 '20 at 17:33