DataDog / datadog-lambda-js

The Datadog AWS Lambda Library for Node
Apache License 2.0
110 stars 35 forks source link

Error while testing handler wrapper by datadog() #536

Closed DanielScharfsteinGit closed 5 months ago

DanielScharfsteinGit commented 5 months ago

Expected Behavior

Tests should pass locally and on CI when DD_LOCAL_TEST or DD_LOCAL_TESTING is set to true.

Actual Behavior

Encounter an error in every test when wrapping the handlers with datadog().

Steps to Reproduce the Problem

  1. Added datadog() from datadog-lambda-js to my handlers: Example: export const handler = datadog( middy(cancelPaymentHandler) );
  2. When running local or CI testing, received the error Cannot set properties of undefined (setting 'done').
  3. When the code is deployed to the AWS environment, it runs perfectly and custom metrics are working.

Specifications

Stacktrace

Error:

Cannot set properties of undefined (setting 'done')
TypeError: Cannot set properties of undefined (setting 'done')
at /*datadog-lambda-js/src/utils/handler.ts:115:17
at /*datadog-lambda-js/src/index.ts:187:21
at step (/*datadog-lambda-js/dist/index.js:44:23)
at Object.next (/*datadog-lambda-js/dist/index.js:25:53)
at /*datadog-lambda-js/dist/index.js:19:71
at new Promise (<anonymous>)
at Object.<anonymous>.__awaiter (/*datadog-lambda-js/dist/index.js:15:12)
at traceListenerOnWrap (/*datadog-lambda-js/dist/index.js:204:36)
at /*datadog-lambda-js/src/index.ts:203:58
at step (/*datadog-lambda-js/dist/index.js:44:23)
at Object.next (/*datadog-lambda-js/dist/index.js:25:53)
at fulfilled (**/*datadog-lambda-js/dist/index.js:16:58)
DanielScharfsteinGit commented 5 months ago

I discovered that we needed to mock the library, as outlined in this link: https://github.com/DataDog/datadog-lambda-js/issues/431#issuecomment-1802554371.

Implementing jest.mock in the global setup resolved the issue.