{"id":12861,"library":"aws-lambda-test-utils","title":"AWS Lambda Test Utilities","description":"aws-lambda-test-utils is a JavaScript utility library designed to simplify the local testing of AWS Lambda functions. It provides helper functions to mock the `context` object and various AWS service `event` objects (e.g., S3, SNS, DynamoDB, API Gateway) that a Lambda function typically receives. This allows developers to run their Lambda logic outside of the AWS environment for faster, automated testing. The current stable version is 1.3.0. As of the provided information, it primarily supports CommonJS imports, and its release cadence appears to be infrequent, given the version number and the project's age (last commit several years ago). Its key differentiator is its focus on providing simple, predefined mocks for common AWS event types, reducing the boilerplate needed for basic Lambda unit tests, particularly for projects still relying on older callback-style Lambda handlers.","status":"maintenance","version":"1.3.0","language":"javascript","source_language":"en","source_url":"https://github.com/dwyl/aws-lambda-test-utils","tags":["javascript","lambda","testing","mock","context","event","aws"],"install":[{"cmd":"npm install aws-lambda-test-utils","lang":"bash","label":"npm"},{"cmd":"yarn add aws-lambda-test-utils","lang":"bash","label":"yarn"},{"cmd":"pnpm add aws-lambda-test-utils","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is a CommonJS module. Direct ESM 'import' will fail unless transpiled or using a CJS wrapper.","wrong":"import { mockContextCreator } from 'aws-lambda-test-utils';","symbol":"mockContextCreator","correct":"const { mockContextCreator } = require('aws-lambda-test-utils');"},{"note":"Provides methods like `createDynamoDBEvent`, `createSNSEvent`, `createS3Event`, and `createAPIGatewayEvent`.","wrong":"import { mockEventCreator } from 'aws-lambda-test-utils';","symbol":"mockEventCreator","correct":"const { mockEventCreator } = require('aws-lambda-test-utils');"},{"note":"The default export for the entire utility collection, containing both `mockContextCreator` and `mockEventCreator`.","wrong":"import * as utils from 'aws-lambda-test-utils';","symbol":"utils","correct":"const utils = require('aws-lambda-test-utils');"}],"quickstart":{"code":"const test = require('tape'); // A testing framework like 'tape' is recommended\nconst { mockContextCreator } = require('aws-lambda-test-utils');\n\n// Simulate your Lambda handler function\nconst myLambdaHandler = {\n  handler: (event, context) => {\n    setTimeout(() => {\n      if (event && event.key1) {\n        // Simulate success for callback-style Lambda\n        context.succeed(event.key1);\n      } else {\n        // Simulate failure\n        context.fail(new Error(\"Missing 'key1' in event.\"));\n      }\n    }, 10);\n  }\n};\n\nconst testEvent = { key1: 'expectedValue' };\nconst contextOptions = {\n  functionName: 'TestLambdaFunction',\n  functionVersion: '$LATEST',\n  invokedFunctionArn: 'arn:aws:lambda:us-east-1:123456789012:function:TestLambdaFunction'\n};\n\ntest('Lambda Handler Test Suite', function(t) {\n  t.test(\"Handler returns correct value for 'key1' using simple context\", function(st) {\n    function callback(error, result) {\n      st.error(error, \"No error expected\");\n      st.equals(result, testEvent.key1, \"Result should match event.key1\");\n      st.end();\n    };\n    const context = mockContextCreator({}, callback);\n    myLambdaHandler.handler(testEvent, context);\n  });\n\n  t.test(\"Handler returns correct value for 'key1' using custom context options\", function(st) {\n    function callback(error, result) {\n      st.error(error, \"No error expected\");\n      st.equals(result, testEvent.key1, \"Result should match event.key1\");\n      st.end();\n    };\n    const context = mockContextCreator(contextOptions, callback);\n    myLambdaHandler.handler(testEvent, context);\n  });\n\n  t.end();\n});","lang":"javascript","description":"Demonstrates how to use `mockContextCreator` to simulate an AWS Lambda execution context with both default and custom options, enabling local testing of a callback-style Lambda handler with a specific event payload. This setup is typical for unit testing Lambda functions outside of the AWS environment."},"warnings":[{"fix":"Use CommonJS `require()` syntax: `const { symbol } = require('aws-lambda-test-utils');` or configure your build system (e.g., Webpack, Rollup) to correctly transpile CJS modules for ESM usage.","message":"The package is primarily designed for CommonJS (CJS) environments, as indicated by its `require()` syntax usage in documentation. Attempting to use ESM `import` syntax directly will likely result in module resolution errors without proper transpilation or a CJS wrapper.","severity":"gotcha","affected_versions":"all"},{"fix":"Review the package's source code and current AWS Lambda documentation to ensure compatibility with your specific use case. For new projects or those using modern AWS features, consider more actively maintained alternatives or be prepared to extend/fork this package.","message":"The project repository has not been updated in over 4 years. While the package may remain functional for older Lambda runtimes and event structures, it may not reflect the latest AWS Lambda features, new event types, or modern Node.js runtime best practices. This could lead to incomplete or inaccurate mocks for newer AWS services or Lambda patterns.","severity":"gotcha","affected_versions":"all"},{"fix":"When testing `async/await` Lambda handlers, ensure your test setup correctly awaits the handler's returned promise and maps its resolution/rejection to your test expectations, potentially wrapping the `mockContextCreator` callback to resolve a promise.","message":"The `mockContextCreator`'s callback-based success/failure (`context.succeed`, `context.fail`) reflects an older Lambda handler pattern. Modern Lambda handlers commonly use `async/await` or return Promises. This package's mocks might not fully emulate the behavior of Promise-based handlers without manual adaptation in your test setup.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement to use CommonJS `require()`: `const { mockContextCreator } = require('aws-lambda-test-utils');`","cause":"Attempting to use ESM 'import' syntax for `aws-lambda-test-utils` in a CommonJS-only project or a file not configured as an ESM module.","error":"Error: Cannot use import statement outside a module"},{"fix":"Ensure the `mockContextCreator` is provided with a valid callback function as its second argument (`mockContextCreator(options, callback)`). Verify that your Lambda handler correctly calls `context.succeed()` or `context.fail()` when using the callback pattern.","cause":"The mock context's callback handler (`context.succeed`, `context.fail`) is not being invoked correctly within your Lambda handler, or your test is not providing a function to handle these calls.","error":"TypeError: context.succeed is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null}