test command runs tests for services defined in a workerd configuration. Tests are defined by exporting a test function in your worker code.
Usage
Arguments
Path to the configuration file. Can be:
- A Cap’n Proto text file (
.capnp) - A binary Cap’n Proto file (with
--binaryflag)
Specifies which tests to run. Has one of the following formats:
<service-pattern>: Match services by glob pattern<service-pattern>:<entrypoint-pattern>: Match services and entrypoints<const-name>:<service-pattern>:<entrypoint-pattern>: Specify config constant, services, and entrypoints
*:* (all services, all entrypoints).Examples:api-*: All services starting with “api-”*:test*: All test entrypoints in all servicesmain:unit*: Unit tests in the “main” service
Options
Configuration parsing
Add a directory to the list of directories searched for non-relative imports (imports starting with
/).Can be specified multiple times.Indicates that the configuration file is an encoded binary Cap’n Proto message rather than text format.
Service overrides
Override the directory named
<name> to point to <path> instead of the path specified in the config file.Override the external service named
<name> to connect to the address <addr> instead of the address specified in the config file.Test behavior
Disable INFO-level logging. By default, INFO logging is enabled for tests to show uncaught exceptions.
Enable predictable mode. Makes workerd behave more deterministically by using pre-set values instead of random data or timestamps.Useful for snapshot testing and reproducible test runs.
Enable all autogates. Useful for testing code paths that are guarded by autogates.
Set the compatibility date for all workers. When specified, workers must NOT specify compatibilityDate in the config.Special values:
0000-00-00: Oldest behavior9999-12-31: Newest behavior
Development features
Watch configuration files and automatically rerun tests when changes are detected.Tests will run once immediately, then again whenever changes are detected.
Enable the Chrome DevTools inspector protocol on the specified address.
Python support
Use the specified path as a disk cache for Python packages.
Use the specified path as a disk cache for Pyodide bundles.
Load a Python snapshot from the specified directory.
Set the Python snapshot directory.
Advanced options
Permit the use of experimental features.
Enable Perfetto tracing output to the specified file.
Writing tests
ES modules syntax
Export atest function from your worker:
Multiple tests
Export multiple entrypoints with test functions:Test parameters
Test controller (currently unused, reserved for future use).
Environment object containing bindings, same as in fetch handlers.
Execution context, same as in fetch handlers.
Loopback networking
The test command enables special “loopback:” network addresses for testing network communication within the same process:Examples
Run all tests
Run tests for specific service
Run specific test entrypoint
Run tests with watch mode
Run tests with predictable mode
Run with specific compatibility date
Run tests with filter and options
Test output
The test command provides clear output:Exit codes
- 0: All tests passed
- 1: One or more tests failed or configuration error
- Other: System-specific error codes
Assertions
workerd does not include a built-in assertion library. You can:- Throw errors directly
- Use conditional throws
- Import an assertion library in your worker code