Skip to content

falsy reason in tests when using serverless AWSLambda.wrapHandler #3133

Closed

Description

@miladr0

I'm writing tests for my lambda function but when I'm using @sentry/serverless and wrap my lambda function with AWSLambda.wrapHandler mocha shows Promise rejected with no or falsy reason.

Sample Lambda function:

const lambdaFn = async (event, context) => {
  context.callbackWaitsForEmptyEventLoop = false
  try {
    return  {
    200,
    body: JSON.stringify(body)
  }
  } catch (error) {
    console.error(error)
    sentry.captureException(error)
    await sentry.flush(2500)
  }
}

exports.actualLambda = lambdaFn
exports.handler = sentry.AWSLambda.wrapHandler(lambdaFn)

Sample test(not working as expected):

  it('status must be 200', async () => {
       const result = await app.handler(null, context)
       expect(result.statusCode).to.equal(200)
  })

But if I directly call exports.actualLambda function test will be passed. like below:
Sample test:

  it('status must be 200', async () => {
       const result = await app.actualLambda(null, context)
       expect(result.statusCode).to.equal(200)
  })

Activity

  1. G-Rath commented on Apr 2, 2021

    @G-Rath
    Contributor

    I hit upon a very similar issue that I suspect is the same - in my case, I was getting errors around it promises rejecting with just false.

    The answer is that you have to call Sentry.init(); yourself - otherwise a client doesn't get initialized, which means flush will reject with false:

    export async function flush(timeout?: number): Promise<boolean> {
    const client = getCurrentHub().getClient<NodeClient>();
    if (client) {
    return client.flush(timeout);
    }
    return Promise.reject(false);
    }

    IMO this is really bad, because it makes it almost impossible to know what the issue is without hunting around in the code, since there's no stack trace that'll point to this line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions