title: How to mock sns?
body: <p>I faced to some issue when I wanted to test my handler which publishes a message on SNS.</p>
  <p>Here is my code:</p>
  <pre><code>// my handler 
  export const handler = (event) =&gt; {
      try { 
          await emitDeletionComplete(classified.metadata.classifiedRequestId);
       }                         
  catch(e) { 
          console.error(e);
           throw new Error(e); 
      }
  }
  
  // my SNS service 
  import SNS from 'aws-sdk/clients/sns';
  const snsClient = new SNS({region: process.env.AWS_REGION});
  
  export const emitDeletionComplete = async (id) =&gt; {
    try {
      await snsClient.publish({
        Message: JSON.stringify({
          type: 'DELETE_COMPLETE',
          data: {
            id
          }
        }),
        TopicArn: process.env.SNS_ARN
      }).promise();
    } catch(err) {
      console.error(err, err.stack);
      throw new Error('We do not succeed to publish the message DELETE_COMPLETE to ARN: ' + process.env.SNS_ARN);
    }
  };
  </code></pre>
  <p>When i want to test, i try to do :</p>
  <pre><code>import { handler } from '../../../src/handler/dispatch-deletion-to-legacy';
  import SNS from 'aws-sdk/clients/sns';
  
  jest.mock('aws-sdk/clients/sns', () =&gt; {
    return {
      __esModule: true,
      default: jest.fn(() =&gt; {
        return {
          publish: jest.fn().mockReturnThis(),
          promise: jest.fn(),
        }
      }),
    };
  });
  
  [...]
  it('should delete', () =&gt; {
      let sns = new SNS();
      const event = {
        Records: [
          {
            body: JSON.stringify({...some event...})
          }
        ]
      }
  
      handler(event);
  
      expect(sns.publish().promise).toBeCalledTimes(1);
  });
  </code></pre>
  <p>Apparently, it is never called. I don't get why.Maybe my mock is completely wrong.</p>
  <p>i'm stuck with it for few hours now...Any idea how can I mock correctly ?</p>
  <p>EDIT 1 : <a href="https://github.com/JLGouwy/aws-sns-mock-test" rel="nofollow noreferrer">https://github.com/JLGouwy/aws-sns-mock-test</a></p>
  <p>thanks</p>