npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

serverless-testing-plugin

v1.0.1

Published

Serverless Framework plugin that exposes function metadata and env loading for tests

Readme

serverless-testing-plugin

Serverless Framework plugin that exposes function metadata and environment loading for tests. Use it with @sls-testing/core and @sls-testing/jest to test Lambda handlers with the same configuration they receive in production.

Install

npm install serverless-testing-plugin @sls-testing/core @sls-testing/jest --save-dev

Configuration

Add the plugin to your serverless.yml:

plugins:
  - serverless-testing-plugin

custom:
  serverlessTesting:
    envFile: .env.test     # default
    autoLoadEnv: true      # default

API

getFunction(name)

Returns metadata for a function defined in serverless.yml.

import { ServerlessTestingPlugin } from 'serverless-testing-plugin'

const plugin = new ServerlessTestingPlugin(serverless, options)
const fn = plugin.getFunction('processOrder')

The returned object contains:

| Field | Type | Description | |-------|------|-------------| | handler | string | Handler path (e.g. src/handlers/order.handler) | | name | string | Resolved function name (e.g. my-service-dev-processOrder) | | memorySize | number | Memory allocation in MB | | timeout | number | Timeout in seconds | | runtime | string | Runtime (e.g. nodejs20.x) | | environment | Record<string, string> | Environment variables from serverless.yml | | events | unknown[] | Event source triggers |

Throws a descriptive error if the function name is not found, listing available functions.

getAllFunctions()

Returns metadata for all functions in the service.

const all = plugin.getAllFunctions()
// { processOrder: { handler, name, ... }, getOrder: { handler, name, ... } }

sls test

Loads .env.test, generates sls-testing.config.json with resolved function metadata, then runs your test suite.

sls test

Usage in Tests

The plugin bridges serverless.yml and your test setup. Instead of hardcoding function names, memory limits, and environment variables in tests, read them from the same source of truth your Lambda functions use.

Step 1: Create the plugin instance

Point the plugin at your serverless.yml configuration. In a test file, this is typically a mock matching your real config:

import { ServerlessTestingPlugin } from 'serverless-testing-plugin'
import { buildApiGatewayV1Event, buildLambdaContext } from '@sls-testing/core'
import '@sls-testing/jest'

const serverless = {
  service: {
    service: 'order-service',
    functions: {
      processOrder: {
        handler: 'src/handlers/process-order.handler',
        memorySize: 512,
        timeout: 30,
        runtime: 'nodejs20.x',
        environment: {
          ORDERS_TABLE: 'orders-dev',
          NOTIFICATION_TOPIC: 'arn:aws:sns:us-east-1:123456789012:notifications',
        },
        events: [{ http: { path: '/orders', method: 'post' } }],
      },
    },
    provider: { stage: 'dev', region: 'us-east-1' },
  },
  config: { servicePath: __dirname },
}

const plugin = new ServerlessTestingPlugin(serverless as any, {})

Step 2: Load function metadata and environment

Use getFunction() to read the function's configuration and set environment variables before tests run:

const fnConfig = plugin.getFunction('processOrder')

beforeAll(() => {
  // Load the same env vars the function receives in production
  if (fnConfig.environment) {
    for (const [key, value] of Object.entries(fnConfig.environment)) {
      process.env[key] = value
    }
  }
})

afterAll(() => {
  delete process.env.ORDERS_TABLE
  delete process.env.NOTIFICATION_TOPIC
})

Step 3: Build context from real function settings

Configure buildLambdaContext() with the function's actual name, memory limit, and timeout:

const context = buildLambdaContext({
  functionName: fnConfig.name,
  memoryLimitInMB: String(fnConfig.memorySize),
  remainingTimeOverride: (fnConfig.timeout ?? 30) * 1000,
})

Step 4: Build events and assert with Jest matchers

import { handler } from './process-order'

it('creates an order', async () => {
  const event = buildApiGatewayV1Event({
    httpMethod: 'POST',
    path: '/orders',
    body: JSON.stringify({ product: 'Widget', amount: 29.99 }),
  })

  const result = await handler(event)

  expect(result).toHaveStatusCode(201)
  expect(result).toBeSuccessfulApiResponse()
  expect(result).toMatchLambdaResponse({
    body: { product: 'Widget', status: 'pending' },
  })
})

Complete example

See examples/serverless-plugin/ for a full working project with:

  • serverless.yml defining two functions (processOrder and getOrder)
  • .env.test with test environment variables
  • Test files showing the complete plugin workflow
  • 7 tests demonstrating metadata loading, context building, and assertion

License

MIT