Every new Lambda project I start used to begin the same way: install a test framework, add a transpiler, write a config file, and then update all of it every few weeks. For a handler that loads a record, does a bit of math and writes it back, that is a lot of machinery. With Node.js 24 I stopped doing that for most backend code.
In my full article on testing Lambda handlers with the Node.js test runner I build a small return API for a fictional bike rental shop and test it with nothing but what ships with Node. Here is the condensed version of why that works and where it gets tricky.
The setup is smaller than you think
The node:test module has been stable since Node.js 20. What makes it interesting for TypeScript projects in 2026 is type stripping: Node removes type annotations while loading a .ts file and runs the rest as plain JavaScript. So node --test "test/**/*.test.ts" just works, with no build step in between.
Two things follow from that:
-
Type checking becomes a separate job. Node strips types, it does not check them. I run
tsc --noEmitas its own CI step before the tests, otherwise a type error slips through. -
Only erasable syntax is allowed. Enums and constructor parameter properties produce runtime code, so Node refuses them. The tsconfig option
erasableSyntaxOnlyflags them while you type.
Also keep the .ts extension in your imports. Missing extensions are the most common reason for ERR_MODULE_NOT_FOUND.
Design for testability, then mock less
The decision that makes everything else easy is not a testing feature at all. The handler is created by a factory that receives a small repository interface. The real DynamoDB implementation is only wired up in the entry point, which contains no logic.
In the test I then pass two mock.fn functions instead of the repository. They record calls and arguments, stay type-safe with the Mock<...> type, and can change behavior per test with mockImplementationOnce. Time is frozen with mock.timers, so a fee calculation based on new Date() returns the same result on every run. A simplified test looks like this:
import { it, mock } from 'node:test';
import assert from 'node:assert/strict';
it('charges three started hours', async () => {
mock.timers.enable({ apis: ['Date'], now: new Date('2026-05-02T11:15:00Z') });
const repo = {
laden: mock.fn(async () => ({ ausleiheId: 'a-42', radId: 'rad-7', startAm: '2026-05-02T09:00:00Z' })),
abschliessen: mock.fn(async () => {}),
};
const res = await erstelleRueckgabeHandler(repo)(event('a-42'));
assert.equal(res.statusCode, 200);
assert.equal(repo.abschliessen.mock.callCount(), 1);
mock.timers.reset();
});
There is also mock.module for replacing whole modules such as the AWS SDK, but it sits behind an experimental flag. I would not build Lambda tests on it. Passing dependencies as parameters avoids the need and makes the code clearer anyway.
The coverage trap
Coverage is built in via --experimental-test-coverage, and a threshold like --test-coverage-lines=90 turns the pipeline red when it drops. The catch: Node only reports files that were actually loaded during the tests. A module without any test does not show up with 0 percent, it is simply absent. Your threshold can be green while a whole file is untested. I keep entry points free of logic and check in reviews that new files appear in the report.
Two smaller things worth knowing: quote your glob patterns so Node expands them instead of your shell (otherwise subfolders may be skipped silently), and you can attach several reporters at once to get console output and a JUnit file for CI in a single run.
When I still pick Vitest
For Lambda backends, node --test is now my default. Not mainly because a run finishes in under a second, but because fewer packages in devDependencies means fewer updates and a smaller supply chain. For frontend components with a DOM or browser mode, or a large codebase built around vi.mock, Vitest remains the better fit, and switching a happy team just on principle is not worth it.
My rule for new projects: start with the built-in runner and switch only when you miss a concrete feature.
What the full article covers
- The complete bike rental example: fee logic, DynamoDB repository with a conditional update, and the handler factory
-
package.jsonscripts and a tsconfig for type-check-only setups - Snapshot tests with
t.assert.snapshotand coverage thresholds in practice - A ready-to-use GitHub Actions workflow with JUnit output, plus a FAQ
Read it here: Node.js test runner: test Lambda handlers without Jest
๐ This is the condensed version. The full article on sascha-ms.de goes deeper โ with all code samples, diagrams and a short knowledge check.
๐ Next step: Curious what your own Lambda functions cost? Try my Lambda cost calculator โ it runs right in your browser.
๐ฌ Stay updated: New articles on serverless, AI and leading tech teams via RSS, browser push or the weekly newsletter (in German).
Originally published on sascha-ms.de. More articles on serverless, AI in everyday development and leading tech teams: sascha-ms.de/en
Top comments (0)