Lesson 20 / 32
Local Development and Testing
Structure handlers so business logic is unit-testable, test adapters with fake events, and know what emulators cannot prove.
By Rahul Agarwal · 15 years in web development
Thin handlers make testing easy
Serverless code is easy to test if you keep the handler thin. Put business logic in plain functions that take ordinary inputs and return ordinary outputs; the handler only parses the event, calls that logic and formats the response. Most tests then become fast unit tests that need no cloud at all. For the adapter layer, keep sample events (saved JSON copies of real API Gateway, SQS or S3 events) and call the handler directly. Local emulators help with the wiring: sam local invoke runs a Lambda function in a Docker container with a sample event, and wrangler dev runs a Worker locally on the same open-source runtime that powers production. Emulators never perfectly reproduce IAM permissions, quotas or the behaviour of managed services, so add a small set of integration tests against a real deployed stage, ideally a short-lived environment per branch, and run them in CI.
Testing logic without the cloud
Three checks hit the pure function directly; one goes through the handler with a fake event. All of it runs in milliseconds on a laptop.
// Keep business logic in a plain function; the handler is a thin adapter.
import assert from 'node:assert/strict';
export function quoteShipping({ weightKg, pincode }) {
if (weightKg <= 0) throw new RangeError('weight must be positive');
const metro = ['110', '400', '560', '600'].includes(pincode.slice(0, 3));
return Math.round((metro ? 40 : 60) + weightKg * 25);
}
export const handler = async (event) => {
const input = JSON.parse(event.body);
return { statusCode: 200, body: JSON.stringify({ rupees: quoteShipping(input) }) };
};
// Tests: the pure function needs no cloud, no mocks.
assert.equal(quoteShipping({ weightKg: 2, pincode: '560034' }), 90);
assert.equal(quoteShipping({ weightKg: 2, pincode: '302001' }), 110);
assert.throws(() => quoteShipping({ weightKg: 0, pincode: '560034' }), RangeError);
// One thin test through the adapter, with a fake event.
const res = await handler({ body: '{"weightKg":1,"pincode":"400001"}' });
assert.deepEqual(JSON.parse(res.body), { rupees: 65 });
console.log('all 4 checks passed');
Output:
all 4 checks passed
Emulating the platform locally
Use emulators to check event wiring and configuration, not to replace unit tests.
# Generate a sample SQS event and invoke one function locally (needs Docker)
sam local generate-event sqs receive-message > events/sqs.json
sam local invoke OrderFunction --event events/sqs.json
# Run a Cloudflare Worker locally with live reload
npx wrangler devWhat only a real deployment reveals
Missing IAM permissions, wrong environment variables, VPC networking problems and service quotas usually appear only in the cloud. A smoke test that calls the deployed endpoint after each deploy catches them before users do.
Quick check: Why separate business logic from the handler?
- Handlers are not allowed to contain more than ten lines
- The logic can be unit-tested with plain inputs, without events, mocks or cloud access
- It removes the need for integration tests
- It makes the deployment package smaller
Answer
The logic can be unit-tested with plain inputs, without events, mocks or cloud access — Pure functions are the easiest code to test. Integration tests are still needed for permissions and wiring.