Skip to main content

The rq.expect object provides assertion methods for writing tests in Requestly. It is built on top of the popular Chai.js assertion library, giving you access to powerful and expressive assertions for validating API responses.
Using Chai.js: Requestly uses the Chai.js BDD assertion library. For the complete list of assertions and advanced features, refer to the Chai.js official documentation.

Basic Usage

The rq.expect function is used within test functions created by rq.test to assert expected conditions:
rq.test("Status code is 200", function() {
    rq.expect(rq.response.code).to.equal(200);
});

Common Assertions

Below are some commonly used Chai.js assertions. For a complete reference, visit the Chai.js BDD API documentation.

Equality

rq.expect(value).to.equal(expected);        // Strict equality (===)
rq.expect(obj).to.eql(expected);            // Deep equality
rq.expect(value).to.deep.equal(expected);   // Deep equality (alias)

Type Checking

rq.expect(value).to.be.a("string");
rq.expect(value).to.be.an("array");
rq.expect(value).to.be.a("number");
rq.expect(value).to.be.a("boolean");

Properties

rq.expect(obj).to.have.property("key");
rq.expect(obj).to.have.property("key", value);

Strings

rq.expect(str).to.include("substring");
rq.expect(str).to.match(/regex/);

Numbers

rq.expect(num).to.be.above(value);
rq.expect(num).to.be.below(value);
rq.expect(num).to.be.within(min, max);

Arrays & Length

rq.expect(arr).to.have.lengthOf(value);
rq.expect(arr).to.include(item);
rq.expect(arr).to.be.empty;

Booleans & Existence

rq.expect(value).to.be.true;
rq.expect(value).to.be.false;
rq.expect(value).to.exist;
rq.expect(value).to.be.null;
rq.expect(value).to.be.undefined;

Negation with .not

Negate any assertion using .not:
rq.expect(value).to.not.equal("deleted");
rq.expect(arr).to.not.be.empty;

Chaining Assertions

Chain multiple assertions for better readability:
rq.expect(data.id).to.be.a("number").and.to.be.above(0);
rq.expect(data.name).to.be.a("string").that.is.not.empty;

Example: Validate Response Structure

rq.test("Response has correct structure", function() {
    const data = rq.response.json();
    
    rq.expect(data).to.be.an("object");
    rq.expect(data).to.have.property("status");
    rq.expect(data).to.have.property("data");
    rq.expect(data.data).to.be.an("array").and.not.be.empty;
});

More Assertions

For the complete list of assertions including:
  • Advanced object and array matchers
  • Custom assertions with .satisfy()
  • Key checking with .have.keys()
  • And many more…
Visit the Chai.js BDD API Documentation