Testing
A Navios application can be tested at three levels, from fastest to most realistic:
- The whole application on the in-memory adapter, with no server and no socket.
- The whole application on a real adapter, through
app.inject(). - The application listening on an ephemeral port, called with
fetch.
Start with the first. It runs the same compiled routes as a real adapter, so guards, validation, error mapping and request state behave the same. A service that does not involve HTTP is tested with a DI container alone; see Testing in the DI docs.
The examples use Vitest. Nothing here depends on it.
Vitest does not compile standard decorators by itself. Add a transform that does, for example unplugin-swc, which needs @swc/core; bun test needs none:
The in-memory adapter
defineInMemoryEnvironment() from @navios/server/testing is an adapter without a server. createTestClient(app) returns a function that calls a declaration against the application in process.
Create a new application for each test and close it afterwards. close() disposes the container, which keeps tests independent.
NaviosApplication<any> is an application on any adapter. NaviosApplication alone is one on the adapter of the Register declaration, which the in-memory one is not.
How the in-memory adapter differs from a real one:
app.listen()throws; there is no server.ctx.signalnever aborts.- There is no limit on the size of a body, and no CORS.
Everything else is as over HTTP. A body is encoded and decoded, so a Date in data reaches the schema as text and a FormData as its parts, and paths are matched by the same rules. The adapter runs the part of the conformance suite that needs no connection.
The test client
- Arguments are typed from the declaration:
urlParams,params(query) anddata(body), plus optional per-callheaders. - The result is a
TestResponse:status,headers,cookies(the values ofset-cookie, one per cookie),text(the raw body) andbody(the body parsed as JSON when the content type is JSON, otherwiseundefined). datais encoded as a client of the builder encodes it: asmultipart/form-datafor an endpoint withbody: 'multipart', and as JSON otherwise.- The client builds the path from the declaration's
urlonly. When the routes are mounted under aglobalPrefixor module prefixes, pass the combined prefix asprefix.
The test client is a thin typed wrapper over app.inject(), not the @navios/builder client. It does not validate the response against the declaration, does not type body, and does not throw on error statuses. Assert on status and body yourself.
Injecting requests
app.inject(request) handles one request in process on whatever adapter the application uses, and initialises the application if needed. Use it for a request no declaration describes, or to exercise a real adapter without a socket.
url is a path with an optional query string. body is sent as JSON unless it is a string, bytes, a FormData, URLSearchParams, Blob or stream, which are sent as fetch sends them: for a string that holds JSON, set the content-type header. remoteAddress is what ctx.ip is for the request. On Fastify the request goes through Fastify's own inject(), so Fastify's parsing, hooks and plugins take part. On every adapter it finds the route that it would find over HTTP.
On every adapter the promise rejects when the response is a stream that fails after its first chunk, where a client over HTTP would see the connection close, and once the application is closed. An error before that is an ordinary error response.
Over real HTTP
To include the network layer, listen on port 0 and use fetch:
listen resolves to the base URL with the port that was assigned. On Bun the options are { port: 0, hostname: '127.0.0.1' }.
Replacing dependencies
Pass providers to the application. They win over everything declared in modules.
A token that holds a value is replaced with provide(Token, { value }). To inspect a service after a request, resolve it from the application: await app.get(UsersService) returns the instance the controller used.
A TestContainer from @navios/di/testing can also back an application: pass it as the container option of NaviosFactory.create.
For state that a guard normally establishes, send what the guard reads (a header, a cookie) or replace the service the guard depends on. That exercises the guard instead of bypassing it.
Testing a guard alone
A guard takes a RequestContext, which only the pipeline builds. The practical way to test one in isolation is a small module on the in-memory adapter with a single probe route:
The same shape tests an interceptor, an error mapper or a request value: attach it to the probe module and assert on the response.