How to Run, Organize, and Write Tests
This document describes the expected style for new and updated tests.
Prefer the smallest harness that exercises the behavior under test without hiding important framework interactions.
Unit Tests
Tests use Mocha and can be run with:
npm test
Use runServerless when the behavior depends on a fully initialized framework instance, such as command execution, configuration resolution, plugin loading, lifecycle behavior, or final compiled CloudFormation output.
The runServerless helper supports fixture and configExt options for running against copied service fixtures with optional configuration extensions.
Because runServerless tests are relatively expensive, keep the number of runs minimal. Prefer one representative service setup that covers the relevant cases, as in the ALB health check tests.
When creating a new test, it is an established practice to name the top-level describe after the path to the file, as shown in AWS Kafka tests.
Existing Test Examples
Example of test files fully backed by runServerless:
When adding tests to a file that still has older direct setup, prefer adding a separate describe block for the new coverage instead of expanding the older setup style.
Prefer small, focused test refactors that preserve behavior while moving toward the style documented here.
Test Style Decision Rule
Use runServerless when the behavior under test depends on a fully initialized framework instance: command execution, configuration resolution, plugin loading, lifecycle behavior, or final compiled CloudFormation output.
Prefer a small fake or direct collaborator when the subject is a pure helper, formatter, path/fs utility, deterministic template builder, or narrow SDK wrapper.
Direct new Serverless(...) is allowed only when constructor or class wiring is the subject, or when a smaller fake would hide the behavior being asserted.
Do not wire fixture-engine wrappers directly in new tests. Use runServerless({ fixture }) for command/lifecycle tests or setupProgrammaticFixture(...) for mutable copied services.
For new AWS lifecycle tests, use awsSdkV3StubMap. Shared data factories for common deploy stubs live in test/utils/aws-stub-maps.js; tests should still assert the relevant awsSdkV3Stub.sends entries.
Package Integration Tests
Package integration tests live under test/integration-package and run in CI with:
npm run integration-test-run-package