2

I've got an existing working test that is testing a route in an Express app (trimmed code):

const AWS = require('aws-sdk-mock');
const AWS_SDK = require('aws-sdk');
AWS.setSDKInstance(AWS_SDK);

...

  before(() => { 
    sendEmailMock = sinon.stub().callsArgWith(1, null, 'All is well');
    AWS.mock('SES', 'sendEmail', sendEmailMock);
    server = rewire('../../../..');

...

  describe('POST:/feedback', () => { 
    it('Returns 200 with a fully formed request', (done) => {

      request(app)
        .post('/gethelp/api/v1/feedback')
        .send({
          thumbsUp: 'true',
          title: 'Abcdef ghi',
          url: 'http://google.com',
          comments: 'lkajsdj  lkajsdkjf aslkjdfa asjdflasjd lkfj',
        })
        .expect(200, () => {
          const args = sendEmailMock.args[0][0];
          ... etc

This is a working test. But I need to refactor it to not use the full server (because it's doing some integration stuff on startup). So I'm bringing in node-mocks-http:

const httpMocks = require('node-mocks-http');
const feedbackRouteHandler = require('./feedback');

...

 before(() => {
    sendEmailMock = sinon.stub().callsArgWith(1, null, 'All is well');
    AWS.mock('SES', 'sendEmail', sendEmailMock);
  });

... 

     const mockRequest = httpMocks.createRequest({
        method: 'POST',
        url: '/gethelp/api/v1/feedback',
        body: {
          thumbsUp: 'true',
          title: 'Abcdef ghi',
          url: 'http://google.com',
          comments: 'lkajsdj  lkajsdkjf aslkjdfa asjdflasjd lkfj',
        },
      });

      const mockResponse = httpMocks.createResponse();

      feedbackRouteHandler(mockRequest, mockResponse);

      expect(mockResponse.statusCode).to.equal(200);

      expect(sendEmailMock.args).to.exist;

The problem is that adding in node-mocks-http appears to have broken the mocking of the AWS SDK. When sendEmail is hit it's hitting the actual AWS SDK, not the mocked version. It was hitting the mocked version in the previous version of the test.

How can I use node-mocks-http with aws-sdk-mock?

jcollum
  • 43,623
  • 55
  • 191
  • 321

0 Answers0