sompnd
sompnd

Reputation: 651

How to stub AWSXray.captureHTTPsGlobal using Sinon?

In my nodejs lambda function I use AWSXray to simply capture https calls made by the function.

const AWSXRay = require("aws-xray-sdk-core");
AWSXRay.captureHTTPsGlobal(require("https"));

In my unit tests how do I mock this?

I have tried mocking it with sinon by:

before(async () =>
  await sandbox.stub(AWSXRay, 'captureHTTPsGlobal').returns({})
);

after(async () => {
  await sandbox.restore();
});

Getting error in test as:

OperationalError: Failed to get the current sub/segment from the context.
    at Object.contextMissingRuntimeError [as contextMissing]

Upvotes: 0

Views: 1048

Answers (1)

sompnd
sompnd

Reputation: 651

The problem was not with Xray, but the way I was writing the tests.

AWSXray will try to capture all https calls. But this scenario should ideally not arise in tests because in an ideal test suite all http/https calls anyway should be stubbed.

I solved the problem by properly mocking the units where https calls were being made.

Upvotes: 1

Related Questions