Skip to content
Vegha Docs

Assertions & Tests

Tests let you assert that a response is correct — the right status, the right headers, the right body — and see pass/fail results after each run.

Each request has a Tests tab. Scripts you write there run after the response arrives, alongside any post-response script, and have access to the same bru, req, and res objects.

Wrap each assertion in a named test() block. Inside it, use expect() to make the assertion:

test("status is 200", () => {
expect(res.status).to.equal(200);
});
test("response is fast enough", () => {
expect(res.responseTime).to.be.below(500);
});

Each test() block reports independently, so one failing assertion does not hide the others.

GoalExample
Status codeexpect(res.status).to.equal(201)
Header presentexpect(res.headers["content-type"]).to.include("json")
JSON body fieldexpect(res.body.user.id).to.equal(42)
Field existsexpect(res.body.token).to.exist
Array lengthexpect(res.body.items).to.have.lengthOf(3)
Response timeexpect(res.responseTime).to.be.below(800)

A typical Tests tab combines several checks:

test("created order", () => {
expect(res.status).to.equal(201);
expect(res.body.order.id).to.exist;
expect(res.body.order.status).to.equal("pending");
});

After a request runs, the Test Results view lists every test() block with a pass or fail marker. Failed assertions show what was expected versus what was received, so you can diagnose the difference quickly.

You can run an entire collection in one pass. Every request’s tests execute in order, and the Test Results view aggregates pass/fail counts across all of them — useful for regression-checking an API after a change.

See the Scripting API reference for the full test and expect API.