Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions testing/tests/introduction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
---

<Warning>
Bruno uses the [Chai library <strong><sup>↗</sup></strong>](https://www.chaijs.com/) for assertions. All Chai expect syntax works in Bruno tests.

Check warning on line 7 in testing/tests/introduction.mdx

View check run for this annotation

Mintlify / Mintlify Validation (bruno-a6972042) - vale-spellcheck

testing/tests/introduction.mdx#L7

Did you really mean 'Chai'?

Check warning on line 7 in testing/tests/introduction.mdx

View check run for this annotation

Mintlify / Mintlify Validation (bruno-a6972042) - vale-spellcheck

testing/tests/introduction.mdx#L7

Did you really mean 'Chai'?
</Warning>

Write JavaScript test scripts to validate API responses, handle complex logic, and automate testing workflows.

![bru test script](/images/screenshots/get-started/bruno-basics/create_test/test-script.webp)

Check warning on line 12 in testing/tests/introduction.mdx

View check run for this annotation

Mintlify / Mintlify Validation (bruno-a6972042) - vale-spellcheck

testing/tests/introduction.mdx#L12

Did you really mean 'bru'?

## Basic Test Structure

Expand Down Expand Up @@ -185,7 +185,7 @@
});
```

## Common Chai Assertions

Check warning on line 188 in testing/tests/introduction.mdx

View check run for this annotation

Mintlify / Mintlify Validation (bruno-a6972042) - vale-spellcheck

testing/tests/introduction.mdx#L188

Did you really mean 'Chai'?

```javascript
// Equality
Expand Down Expand Up @@ -219,6 +219,86 @@
expect(arr).to.be.empty;
```

## Bruno-specific assertions

In addition to the standard Chai assertions, Bruno registers custom assertions for working with JSON response bodies.

Check warning on line 224 in testing/tests/introduction.mdx

View check run for this annotation

Mintlify / Mintlify Validation (bruno-a6972042) - vale-spellcheck

testing/tests/introduction.mdx#L224

Did you really mean 'Chai'?

### `jsonBody`

Use `jsonBody` to validate the response body against a value or to check for the existence of a nested property. This assertion mirrors Postman's `pm.response.to.have.jsonBody(...)` API and is convenient for collections imported from Postman.

```javascript
test("body is valid JSON", function () {
// Passes when the body is a JSON object or array
expect(res.getBody()).to.have.jsonBody();
});

test("body deep equals expected object", function () {
expect(res.getBody()).to.have.jsonBody({
id: 1,
name: "Alice"
});
});

test("body has a nested property", function () {
// Supports dot notation, numeric brackets, and quoted bracket keys
expect(res.getBody()).to.have.jsonBody("user.profile.email");
expect(res.getBody()).to.have.jsonBody("items[0].id");
expect(res.getBody()).to.have.jsonBody('data["a.b"].name');
});

test("nested property equals value", function () {
expect(res.getBody()).to.have.jsonBody("user.profile.name", "Alice");
});

test("body should not have property", function () {
expect(res.getBody()).to.not.have.jsonBody("error");
});
```

| Call | Behavior |
|------|----------|
| `jsonBody()` | Asserts the body is a JSON object or array |
| `jsonBody(object)` | Deep equality against the supplied object |
| `jsonBody("path")` | Asserts the nested property exists |
| `jsonBody("path", value)` | Asserts the nested property equals the supplied value |

All variants support negation via `.not` (for example, `to.not.have.jsonBody("key")`).

### `jsonSchema`

Use `jsonSchema` to validate a response body against a [JSON Schema](https://json-schema.org/). Bruno uses [Ajv](https://ajv.js.org/) under the hood, so any JSON Schema draft supported by Ajv works here. An optional second argument forwards options to the Ajv instance.

Check warning on line 270 in testing/tests/introduction.mdx

View check run for this annotation

Mintlify / Mintlify Validation (bruno-a6972042) - vale-spellcheck

testing/tests/introduction.mdx#L270

Did you really mean 'Ajv'?

Check warning on line 270 in testing/tests/introduction.mdx

View check run for this annotation

Mintlify / Mintlify Validation (bruno-a6972042) - vale-spellcheck

testing/tests/introduction.mdx#L270

Did you really mean 'Ajv'?

```javascript
test("body matches schema", function () {
const schema = {
type: "object",
required: ["id", "name", "email"],
properties: {
id: { type: "number" },
name: { type: "string" },
email: { type: "string", format: "email" }
}
};

expect(res.getBody()).to.have.jsonSchema(schema);
});

test("body matches schema with Ajv options", function () {
const schema = {
type: "object",
properties: {
createdAt: { type: "string", format: "date-time" }
}
};

// Pass Ajv options as the second argument
expect(res.getBody()).to.have.jsonSchema(schema, { strict: false });
});
```

When the body fails validation, the assertion error includes the Ajv validation errors so you can quickly see which fields are missing or have the wrong type.

Check warning on line 300 in testing/tests/introduction.mdx

View check run for this annotation

Mintlify / Mintlify Validation (bruno-a6972042) - vale-spellcheck

testing/tests/introduction.mdx#L300

Did you really mean 'Ajv'?

## Next Steps

For more advanced scripting capabilities, see:
Expand Down
Loading