Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking ApiGatewayManagementApi not working #225

Open
kstro21 opened this issue Oct 2, 2020 · 3 comments
Open

Mocking ApiGatewayManagementApi not working #225

kstro21 opened this issue Oct 2, 2020 · 3 comments

Comments

@kstro21
Copy link

kstro21 commented Oct 2, 2020

Here is a reduced example of what I'm trying to do.

I have a class with a function that looks like this:

// at the top of the file
const ApiGatewayManagementApi = require('aws-sdk/clients/apigatewaymanagementapi');

// function definition
async sendSummary(summary, { endpoint, connectionId }) {
    const apiGwManagementApi = new ApiGatewayManagementApi({ endpoint });

    return apiGwManagementApi
      .postToConnection({
        ConnectionId: connectionId,
        Data: JSON.stringify(summary),
      })
      .promise()
      .catch((err) => {
        console.error('Error while sending', err);
        throw err;
      });
  }

That works as expected. Now comes the part of the test

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

it('send summary', async () => {
    AWS.mock('ApiGatewayManagementApi', 'postToConnection', function (
      params,
      callback
    ) {
      expect(params).toEqual(
      jasmine.objectContaining({
         ConnectionId: connectionId,
        Data: JSON.stringify(summary),
      })
    );
      callback(null, {});
    });

    const { service } = require('../my-service');

    await expectAsync(
      service.sendSummary(summaryMock, connectionMock)
    ).toBeResolved();

    AWS.restore('ApiGatewayManagementApi');
  });

But when running the tests it throws the error

UnknownEndpoint: Inaccessible host: `some.execute-api.us-east-1.amazonaws.com'

Seems like the mock is not working. Any help is appreciated.

@sukruavcuoglu
Copy link

from READ.me

"Also note that if you initialise an AWS service inside a callback from an async function inside the handler function, that won't work either."

@kstro21
Copy link
Author

kstro21 commented Apr 23, 2021

So, @sukruavcuoglu, it is not possible?

@nmq64
Copy link

nmq64 commented Feb 9, 2023

The solution is to replace this
const ApiGatewayManagementApi = require('aws-sdk/clients/apigatewaymanagementapi');
and this
const apiGwManagementApi = new ApiGatewayManagementApi({ endpoint });

with this
const AWS = require('aws-sdk');
and this
const apiGwManagementApi = new AWS.ApiGatewayManagementApi(({ endpoint });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants