Skip to main content

Testing

@opra/testing provides an in-process HTTP client and a fluent assertion API for testing OPRA services end-to-end — without binding a port, starting a server, or writing manual expect chains.

npm install --save-dev @opra/testing

How it works

OpraTestClient sends requests directly to your application's request handler, bypassing the network layer entirely. You get the full OPRA request pipeline — validation, encoding, error handling — at the speed of an in-memory function call.

import { OpraTestClient } from '@opra/testing';

// Create the client against your adapter (no server needed)
const testClient = new OpraTestClient(adapter);

Making requests & asserting

Every response carries a .expect property with chainable assertions:

const res = await testClient
.get('/customers')
.param({ limit: 10, filter: 'givenName = "Jane"' })
.getResponse();

res.expect
.toSuccess(200)
.toReturnCollection()
.toReturnItems(1)
.toMatch({ givenName: 'Jane' });
// Assert a single resource
const res = await testClient.get('/customers/1').getResponse();
res.expect
.toSuccess(200)
.toMatch({ id: 1, givenName: 'Alice' });
// Assert an error response
const res = await testClient.get('/customers/999').getResponse();
res.expect
.toFail(404)
.toHaveErrorCode('NOT_FOUND');

Available matchers

MatcherWhat it checks
.toSuccess(status?)Response is 2xx (optionally exact status)
.toFail(status?)Response is 4xx/5xx (optionally exact status)
.toReturnCollection()Response is a list/collection result
.toReturnItems(n)Collection contains exactly n items
.toMatch(partial)Payload matches the given partial object
.toHaveErrorCode(code)Error response contains the given code
.toBeSortedBy(field, dir?)Collection is sorted by field ascending/descending

OpraTestClient · ApiExpect & Matchers