Install
@forst/cli first. Add a postinstall script that runs forst generate ..
Run generate once before you import @forst/gen.
Details are on the Installation page.Replace one function
This is the usual choice for a unit test. Supply the result you need, then run the code that depends on it.$auth.VerifyPassword still comes from @forst/gen/auth. The replacement only applies inside the callback passed to withForstTestScope.
The generated handler type matches the real function. TypeScript reports an error if your replacement accepts the wrong input or returns the wrong shape.
What the scope guarantees
The scope contains each replacement and cleans it up for you.- The original client behavior is restored after the callback finishes.
- Restoration also happens when the callback throws.
- Nested scopes use the closest replacement.
- Concurrent tests keep their replacements separate.
- A call with no replacement fails with
InvokeRejectedand names the package and function.
afterEach cleanup hook.
Choose how much to replace
Start with the smallest replacement that describes your test.Replace several package functions
Place related replacements under the same package name. Any function you call inside the scope must have a replacement.Replace the connection
Use a connection replacement when the request itself matters. It is also useful for failures that happen before Forst returns a business result.InvokeTimedOut tag that it would see in production.
Test a namespaced client
If your application usescreateForstClient, create a test client with the same package and function shape.
Effect mode
An Effect Layer supplies replacement services to the code under test. You can replace one service, a set of package functions, or the connection. See Effect mode.Call a real Forst server
Mocks replace the server. When a test should exercise compiled Forst code over HTTP, start or attach to an invoke server from the same testing module. Install the optional peer once:startForstTestServer lazy-loads @forst/cli/invoke, starts or attaches to the invoke server, and points the default client at it. Package handles keep working.
If a server is already running (CI, Docker, or a globalSetup), set FORST_BASE_URL or FORST_SKIP_SPAWN=1. The same call attaches instead of spawning. @forst/runtime is the other direction (Forst calling JS) and is not used here.
In Effect mode use ForstTestServerLayer or makeForstTestServer instead. See Effect mode.
Related
Call Forst over HTTP
Production calls, failures, and options.
Effect mode
Layer.mock and ForstTestLayer.Generate a TypeScript client
@forst/gen imports and layout.CLI reference
testingSubpath and other generate fields.