For AI agents: the complete documentation index is available at https://navios.tech/llms.txt, the full documentation bundle is available at https://navios.tech/llms-full.txt, and this page is available as Markdown at https://navios.tech/docs/server/essentials/testing.md.

Testing

A Navios application can be tested at three levels, from fastest to most realistic:

  1. The whole application on the in-memory adapter, with no server and no socket.
  2. The whole application on a real adapter, through app.inject().
  3. 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:

vitest.config.mts
import swc from 'unplugin-swc'
import { defineConfig } from 'vitest/config'

export default defineConfig({
  plugins: [
    swc.vite({
      jsc: {
        target: 'es2022',
        parser: { syntax: 'typescript', decorators: true },
        transform: { decoratorVersion: '2022-03' },
      },
    }),
  ],
})

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.

users.spec.mts
import { NaviosFactory } from '@navios/server'
import { createTestClient, defineInMemoryEnvironment } from '@navios/server/testing'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'

import type { NaviosApplication } from '@navios/server'

// AppModule, getUser and createUser come from the Quick start
import { AppModule } from './app.module.mjs'
import { createUser, getUser } from './users.mjs'

describe('users', () => {
  let app: NaviosApplication<any>

  beforeEach(async () => {
    app = await NaviosFactory.create(AppModule, {
      adapter: defineInMemoryEnvironment(),
      logger: false,
    })
  })

  afterEach(async () => {
    await app.close()
  })

  it('returns a user', async () => {
    const call = createTestClient(app)

    const found = await call(getUser, { urlParams: { userId: '1' } })

    expect(found.status).toBe(200)
    expect(found.body).toEqual({ id: '1', name: 'Ada', email: '[email protected]' })
  })

  it('rejects an invalid body', async () => {
    const call = createTestClient(app)

    const invalid = await call(createUser, { data: { name: 'Grace', email: 'nope' } })

    expect(invalid.status).toBe(400)
    expect(invalid.headers['content-type']).toBe('application/problem+json')
    expect(invalid.body).toMatchObject({ source: 'body' })
  })
})

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.signal never 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

const call = createTestClient(app, {
  headers: { 'x-api-key': 'secret' }, // sent with every call
  prefix: '/api', // the path prefix of the routes under test
})

const response = await call(listUsers, { params: { limit: 5 }, headers: { 'x-trace': '1' } })
  • Arguments are typed from the declaration: urlParams, params (query) and data (body), plus optional per-call headers.
  • The result is a TestResponse: status, headers, cookies (the values of set-cookie, one per cookie), text (the raw body) and body (the body parsed as JSON when the content type is JSON, otherwise undefined).
  • data is encoded as a client of the builder encodes it: as multipart/form-data for an endpoint with body: 'multipart', and as JSON otherwise.
  • The client builds the path from the declaration's url only. When the routes are mounted under a globalPrefix or module prefixes, pass the combined prefix as prefix.
Info

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.

const response = await app.inject({
  method: 'POST',
  url: '/users',
  headers: { 'x-api-key': 'secret' },
  body: { name: 'Grace', email: '[email protected]' },
})

expect(response.status).toBe(201)
expect(response.body).toMatchObject({ name: 'Grace' })

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:

const app = await NaviosFactory.create(AppModule, {
  adapter: defineFastifyEnvironment(),
  logger: false,
})
const base = await app.listen({ port: 0, host: '127.0.0.1' })

const response = await fetch(`${base}/users/1`)

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.

import { Injectable, provide } from '@navios/server'

@Injectable()
class EmptyUsers extends UsersService {
  override findById() {
    return null
  }
}

const app = await NaviosFactory.create(AppModule, {
  adapter: defineInMemoryEnvironment(),
  logger: false,
  providers: [provide(UsersService, EmptyUsers)],
})

const response = await createTestClient(app)(getUser, { urlParams: { userId: '1' } })
expect(response.status).toBe(404)

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:

// ApiKeyGuard comes from Guards and interceptors
const probe = API.declareEndpoint({
  method: 'GET',
  url: '/probe',
  responseSchema: z.object({ ok: z.boolean() }),
})

it('rejects a request without an API key', async () => {
  @Controller()
  class ProbeController {
    @Endpoint(probe)
    probe() {
      return { ok: true }
    }
  }

  @Module({ controllers: [ProbeController], guards: [ApiKeyGuard] })
  class ProbeModule {}

  const app = await NaviosFactory.create(ProbeModule, {
    adapter: defineInMemoryEnvironment(),
    logger: false,
  })
  const call = createTestClient(app)

  expect((await call(probe)).status).toBe(401)
  expect((await call(probe, { headers: { 'x-api-key': 'secret' } })).status).toBe(200)

  await app.close()
})

The same shape tests an interceptor, an error mapper or a request value: attach it to the probe module and assert on the response.