Registry / testing / serverless-jest-plugin

serverless-jest-plugin

JSON →
library0.4.0jsnpmunverified

serverless-jest-plugin integrates the Jest testing framework directly into the Serverless Framework workflow, enabling test-driven development for AWS Lambda functions and other Serverless components. The current stable version is 0.4.0, which dropped support for Node.js 8.10 and added compatibility for Node.js 14.x and Serverless Framework v3.x. Its release cadence appears to be infrequent, with significant gaps between major updates, focusing on compatibility with new Node.js and Serverless Framework versions. The plugin differentiates itself by providing `sls` commands to create functions with accompanying test templates, create tests for existing handlers, and run tests directly via `sls invoke test`. This streamlines the development process for Serverless applications by embedding testing commands within the familiar Serverless CLI.

npm install serverless-jest-plugin
INSTALL
IMPORT
SIG · SERVERLESS-JEST-PL
S
serverless-jest-plugin
testingjavascriptv0.4.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

serverless-jest-plugin
plugins: - serverless-jest-plugin
import 'serverless-jest-plugin';
This package is a Serverless Framework plugin and is loaded via the `plugins` section in `serverless.yml`, not through direct JavaScript/TypeScript imports. It extends the Serverless CLI with new commands.
custom.jest
custom: jest: collectCoverage: true
jest: collectCoverage: true
Jest configuration options are passed through the `custom.jest` property in `serverless.yml`, following the Serverless Framework's convention for plugin-specific custom configurations.
sls create function
sls create function -f myFunction --handler functions/myFunction/index.handler
serverless create function -f myFunction
The plugin extends the Serverless CLI with `sls create function` and `sls create test` commands. Ensure the plugin is correctly listed in `serverless.yml` for these commands to be available.

This quickstart demonstrates how to install and configure the plugin, create a new Serverless function along with its test file, and execute tests using the provided `sls invoke test` command.

npm install --save-dev serverless-jest-plugin jest serverless # serverless.yml # ... other config ... plugins: - serverless-jest-plugin custom: jest: collectCoverage: true testEnvironment: 'node' # Create a new Serverless service if you don't have one # sls create --template aws-nodejs --path my-service # cd my-service # Create a new function and its associated test file sls create function -f myFunc --handler functions/myFunc/index.handler --path tests # Example of generated file structure and content: # functions/myFunc/index.js // 'use strict'; // module.exports.handler = async (event) => { // return { // statusCode: 200, // body: JSON.stringify({ // message: 'Hello from myFunc!', // input: event, // }, null, 2), // }; // }; # tests/myFunc.test.js // 'use strict'; // const jestPlugin = require('serverless-jest-plugin'); // const lambda = jestPlugin.getlambda({ // handler: 'functions/myFunc/index.handler' // }); // describe('myFunc', () => { // beforeAll((done) => { done(); }); // it('should return 200 with a greeting message', async () => { // const event = {}; // Mock your event object // const response = await lambda.handler(event); // expect(response.statusCode).toBe(200); // expect(JSON.parse(response.body).message).toEqual('Hello from myFunc!'); // }); // }); # Run all tests using the plugin's command sls invoke test # Run tests for a specific function sls invoke test -f myFunc
sls --version
Debug
Known issues
breakingVersion 0.4.0 of serverless-jest-plugin dropped support for Node.js 8.10. Users on older Node.js runtimes must upgrade to Node.js 14.x or newer.
fix
Upgrade your Node.js environment to version 14.x or later. Ensure your `serverless.yml` specifies a compatible runtime for your Lambda functions.
affects: >=0.4.0
breakingVersion 0.4.0 added explicit support for Serverless Framework v3.x. While previous versions required Serverless v1.0+, ensuring compatibility across major versions of the Serverless Framework itself is critical.
fix
Ensure your Serverless Framework CLI version is compatible with the plugin version. For `serverless-jest-plugin` v0.4.0, use Serverless Framework v3.x. If experiencing issues, align your plugin and framework versions based on release notes.
affects: >=0.4.0
gotchaJest configuration options, such as `collectCoverage`, must be placed under the `custom.jest` property in your `serverless.yml`. Placing them directly under `jest` or another custom key will prevent them from being picked up by the plugin.
fix
Review your `serverless.yml` and ensure all Jest-specific configurations (e.g., `collectCoverage`, `testEnvironment`) are nested directly under `custom.jest`.
affects: >=0.1.0
Errors
Common errors & fixes
Plugin "serverless-jest-plugin" not found. Make sure it's installed and listed in your serverless.yml
The plugin is either not installed as a dev dependency or not correctly added to the `plugins` array in `serverless.yml`.
fix
Run `npm install --save-dev serverless-jest-plugin` and then add `- serverless-jest-plugin` to the `plugins` section in your `serverless.yml` file.
TypeError: Cannot read properties of undefined (reading 'handler')
This often occurs when the generated test file or the test runner cannot correctly resolve the handler path provided during `sls create function` or `sls invoke test`.
fix
Verify the `--handler` argument used with `sls create function` is correct. Inspect the generated test file (e.g., `test/myFunc.test.js`) and ensure the `jestPlugin.getlambda({ handler: '...' })` path matches your function's handler export.
Serverless command 'create' does not support the option 'function'
The Serverless Framework CLI itself does not natively have a `create function` command. This command is added by the `serverless-jest-plugin` when loaded.
fix
Ensure `serverless-jest-plugin` is correctly installed via `npm install --save-dev serverless-jest-plugin` and listed in the `plugins` array of your `serverless.yml` file. Restart your terminal if necessary to pick up changes.
Upgrade
Version history
0.4.0latest on npm
Audit
Dependencies
serverlessrequiredCore dependency for Serverless Framework integration.
jestrequiredThe underlying testing framework used by the plugin.
Agent activity
14 hits · last 30 days
node
12
Resources