Skip to content

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

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

Closed
miladr0 opened this issue Dec 18, 2020 · 1 comment
Closed

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

miladr0 opened this issue Dec 18, 2020 · 1 comment
Labels
Package: serverless Issues related to the Sentry Serverless SDK

Comments

@miladr0
Copy link

miladr0 commented Dec 18, 2020

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)
  })
@G-Rath
Copy link

G-Rath commented Apr 2, 2021

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
Labels
Package: serverless Issues related to the Sentry Serverless SDK
Projects
None yet
Development

No branches or pull requests

4 participants