[**Documentation**](../../../README)

***

[Documentation](../../../README) / [zerotal](../../README) / [testing](../README) / TestResponse

# Class: TestResponse

Defined in: [packages/testing/src/TestResponse.ts:44](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L44)

## Constructors

### Constructor

> **new TestResponse**(`_res`, `_body`, `context?`): `TestResponse`

Defined in: [packages/testing/src/TestResponse.ts:53](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L53)

#### Parameters

##### \_res

`Response`

##### \_body

`string`

##### context?

[`TestResponseContext`](../interfaces/TestResponseContext) & `object` = `{}`

#### Returns

`TestResponse`

## Accessors

### status

#### Get Signature

> **get** **status**(): `number`

Defined in: [packages/testing/src/TestResponse.ts:92](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L92)

##### Returns

`number`

***

### ok

#### Get Signature

> **get** **ok**(): `boolean`

Defined in: [packages/testing/src/TestResponse.ts:95](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L95)

##### Returns

`boolean`

***

### headers

#### Get Signature

> **get** **headers**(): `Headers`

Defined in: [packages/testing/src/TestResponse.ts:98](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L98)

##### Returns

`Headers`

## Methods

### of()

> `static` **of**(`res`, `context?`): `Promise`\<`TestResponse`\>

Defined in: [packages/testing/src/TestResponse.ts:65](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L65)

Read a `Response`'s body (and session) and wrap it. The only async step in the class.

#### Parameters

##### res

`Response`

##### context?

[`TestResponseContext`](../interfaces/TestResponseContext) = `{}`

#### Returns

`Promise`\<`TestResponse`\>

***

### exception()

> **exception**(): `unknown`

Defined in: [packages/testing/src/TestResponse.ts:111](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L111)

The exception the request raised, when the suite called
[TestApp.withoutExceptionHandling](TestApp#withoutexceptionhandling). `undefined` otherwise, and when
the request completed without throwing.

#### Returns

`unknown`

#### Example

```ts
const res = await app.withoutExceptionHandling().get('/boom');
expect(res.exception()).toBeInstanceOf(PaymentDeclinedError);
```

***

### assertStatus()

> **assertStatus**(`expected`): `this`

Defined in: [packages/testing/src/TestResponse.ts:118](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L118)

Assert the HTTP status code. Chainable.

#### Parameters

##### expected

`number`

#### Returns

`this`

***

### assertOk()

> **assertOk**(): `this`

Defined in: [packages/testing/src/TestResponse.ts:128](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L128)

Assert HTTP 200 OK.

#### Returns

`this`

***

### assertCreated()

> **assertCreated**(): `this`

Defined in: [packages/testing/src/TestResponse.ts:133](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L133)

Assert HTTP 201 Created.

#### Returns

`this`

***

### assertNoContent()

> **assertNoContent**(): `this`

Defined in: [packages/testing/src/TestResponse.ts:138](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L138)

Assert HTTP 204 No Content.

#### Returns

`this`

***

### assertMovedPermanently()

> **assertMovedPermanently**(): `this`

Defined in: [packages/testing/src/TestResponse.ts:143](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L143)

Assert HTTP 301 Moved Permanently.

#### Returns

`this`

***

### assertUnauthorized()

> **assertUnauthorized**(): `this`

Defined in: [packages/testing/src/TestResponse.ts:148](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L148)

Assert HTTP 401 Unauthorized.

#### Returns

`this`

***

### assertForbidden()

> **assertForbidden**(): `this`

Defined in: [packages/testing/src/TestResponse.ts:153](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L153)

Assert HTTP 403 Forbidden.

#### Returns

`this`

***

### assertNotFound()

> **assertNotFound**(): `this`

Defined in: [packages/testing/src/TestResponse.ts:158](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L158)

Assert HTTP 404 Not Found.

#### Returns

`this`

***

### assertUnprocessable()

> **assertUnprocessable**(): `this`

Defined in: [packages/testing/src/TestResponse.ts:163](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L163)

Assert HTTP 422 Unprocessable Entity.

#### Returns

`this`

***

### assertServerError()

> **assertServerError**(): `this`

Defined in: [packages/testing/src/TestResponse.ts:168](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L168)

Assert HTTP 500 Internal Server Error.

#### Returns

`this`

***

### assertSuccessful()

> **assertSuccessful**(): `this`

Defined in: [packages/testing/src/TestResponse.ts:173](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L173)

Assert the status is in the 2xx range.

#### Returns

`this`

***

### assertRedirect()

> **assertRedirect**(`url`): `this`

Defined in: [packages/testing/src/TestResponse.ts:183](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L183)

Assert the response is a redirect to `url`.

#### Parameters

##### url

`string`

#### Returns

`this`

***

### assertHeader()

> **assertHeader**(`name`, `value?`): `this`

Defined in: [packages/testing/src/TestResponse.ts:207](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L207)

Assert a response header is present and optionally matches a value.

#### Parameters

##### name

`string`

##### value?

`string`

#### Returns

`this`

#### Example

```ts
res.assertHeader('Content-Type');
res.assertHeader('Content-Type', 'application/json');
```

***

### assertHeaderMissing()

> **assertHeaderMissing**(`name`): `this`

Defined in: [packages/testing/src/TestResponse.ts:225](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L225)

Assert a response header is absent.

#### Parameters

##### name

`string`

#### Returns

`this`

***

### assertJson()

> **assertJson**(`expected`): `this`

Defined in: [packages/testing/src/TestResponse.ts:240](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L240)

Parse the body as JSON and assert that every key in `expected` matches.
Extra keys in the actual response are allowed.

#### Parameters

##### expected

`Record`\<`string`, `unknown`\>

#### Returns

`this`

***

### assertJsonPath()

> **assertJsonPath**(`path`, `expected`): `this`

Defined in: [packages/testing/src/TestResponse.ts:264](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L264)

Assert a value at a dot-notation path in the JSON body.

#### Parameters

##### path

`string`

##### expected

`unknown`

#### Returns

`this`

#### Example

```ts
res.assertJsonPath('user.name', 'Alice');
res.assertJsonPath('data.0.id', 1);
```

***

### assertJsonCount()

> **assertJsonCount**(`count`, `key?`): `this`

Defined in: [packages/testing/src/TestResponse.ts:286](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L286)

Assert the JSON body (or a key within it) is an array of the given length.

#### Parameters

##### count

`number`

##### key?

`string`

#### Returns

`this`

#### Example

```ts
res.assertJsonCount(3);          // top-level array
res.assertJsonCount(3, 'data');  // array at body.data
```

***

### assertSee()

> **assertSee**(`needle`): `this`

Defined in: [packages/testing/src/TestResponse.ts:316](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L316)

Assert the response body contains the given string.

#### Parameters

##### needle

`string`

#### Returns

`this`

#### Example

```ts
res.assertSee('Welcome, Alice');
```

***

### assertDontSee()

> **assertDontSee**(`needle`): `this`

Defined in: [packages/testing/src/TestResponse.ts:329](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L329)

Assert the response body does NOT contain the given string.

#### Parameters

##### needle

`string`

#### Returns

`this`

#### Example

```ts
res.assertDontSee('Error');
```

***

### assertBodyContains()

> **assertBodyContains**(`needle`): `this`

Defined in: [packages/testing/src/TestResponse.ts:339](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L339)

Assert the response body contains the given string. Alias of assertSee.

#### Parameters

##### needle

`string`

#### Returns

`this`

***

### assertSeeText()

> **assertSeeText**(`needle`): `this`

Defined in: [packages/testing/src/TestResponse.ts:352](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L352)

Assert the body contains `needle` once tags are stripped — the text a user
would actually read. Use it when markup sits between the words you expect,
which is what makes a plain [assertSee](#assertsee) on rendered HTML brittle.

#### Parameters

##### needle

`string`

#### Returns

`this`

#### Example

```ts
// Passes against `<strong>Welcome</strong>, <em>Alice</em>`
res.assertSeeText('Welcome, Alice');
```

***

### assertDontSeeText()

> **assertDontSeeText**(`needle`): `this`

Defined in: [packages/testing/src/TestResponse.ts:361](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L361)

Assert the tag-stripped body does NOT contain `needle`.

#### Parameters

##### needle

`string`

#### Returns

`this`

***

### json()

> **json**\<`T`\>(): `T`

Defined in: [packages/testing/src/TestResponse.ts:374](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L374)

Parse and return the full JSON body.

#### Type Parameters

##### T

`T` = `unknown`

#### Returns

`T`

***

### text()

> **text**(): `string`

Defined in: [packages/testing/src/TestResponse.ts:379](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L379)

Return the full response body as text.

#### Returns

`string`

***

### validationErrors()

> **validationErrors**(): `Record`\<`string`, `string`[]\> \| `null`

Defined in: [packages/testing/src/TestResponse.ts:394](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L394)

The validation errors this response carries, from whichever place the
response put them: a JSON API gets `422 { errors: {...} }`, while a form
submit gets a redirect with the errors flashed to the session. Returns
`null` when the response carries none.

#### Returns

`Record`\<`string`, `string`[]\> \| `null`

#### Example

```ts
const errors = res.validationErrors();
```

***

### assertInvalid()

> **assertInvalid**(`fields?`): `this`

Defined in: [packages/testing/src/TestResponse.ts:421](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L421)

Assert the request failed validation, optionally naming the fields.

Covers both shapes a failed validation takes: the `422` JSON body an API
client receives, and the errors a form submit flashes to the session before
redirecting back. You assert the same way for either.

#### Parameters

##### fields?

`string` \| `string`[] \| `Record`\<`string`, `string`\>

#### Returns

`this`

#### Example

```ts
res.assertInvalid();                      // failed on something
res.assertInvalid('email');               // failed on email
res.assertInvalid(['email', 'password']); // failed on both
res.assertInvalid({ email: 'required' }); // and the message contains "required"
```

***

### assertValid()

> **assertValid**(`fields?`): `this`

Defined in: [packages/testing/src/TestResponse.ts:470](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L470)

Assert the response carries no validation errors — for the named fields
when given, or for any field at all when called bare.

#### Parameters

##### fields?

`string` \| `string`[]

#### Returns

`this`

#### Example

```ts
res.assertValid();        // nothing failed
res.assertValid('email'); // email in particular did not fail
```

***

### assertCookie()

> **assertCookie**(`name`, `value?`): `this`

Defined in: [packages/testing/src/TestResponse.ts:510](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L510)

Assert that the response sets a cookie with the given name.
Optionally assert its value.

#### Parameters

##### name

`string`

##### value?

`string`

#### Returns

`this`

#### Example

```ts
res.assertCookie('zerotal_session');
res.assertCookie('theme', 'dark');
```

***

### assertCookieMissing()

> **assertCookieMissing**(`name`): `this`

Defined in: [packages/testing/src/TestResponse.ts:535](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L535)

Assert that the response does NOT set a cookie with the given name.

#### Parameters

##### name

`string`

#### Returns

`this`

#### Example

```ts
res.assertCookieMissing('remember_me');
```

***

### session()

> **session**(): `Record`\<`string`, `unknown`\> \| `null`

Defined in: [packages/testing/src/TestResponse.ts:557](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L557)

The session the response carries, decoded through the app's own session
driver. `null` when the response set no session cookie.

#### Returns

`Record`\<`string`, `unknown`\> \| `null`

#### Throws

When the response was not produced by a [TestApp](TestApp) with a
resolvable `session.driver` — there is nothing to decode with.

#### Example

```ts
expect(res.session()?.['cart_id']).toBe(7);
```

***

### assertSessionHas()

> **assertSessionHas**(`key`, `value?`): `this`

Defined in: [packages/testing/src/TestResponse.ts:569](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L569)

Assert that the session contains the given key (optionally matching value).

#### Parameters

##### key

`string`

##### value?

`unknown`

#### Returns

`this`

#### Example

```ts
res.assertSessionHas('status', 'saved');
res.assertSessionHas('user_id');
```

***

### assertSessionMissing()

> **assertSessionMissing**(`key`): `this`

Defined in: [packages/testing/src/TestResponse.ts:607](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L607)

Assert that the session does NOT contain the given key.

A session that cannot be decoded throws rather than passing: "I could not
read the session" is not evidence that the key is absent, and treating it
as such is an assertion that can never fail.

#### Parameters

##### key

`string`

#### Returns

`this`

#### Example

```ts
res.assertSessionMissing('errors');
```

***

### assertSessionHasErrors()

> **assertSessionHasErrors**(`fields?`): `this`

Defined in: [packages/testing/src/TestResponse.ts:628](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L628)

Assert the session carries flashed validation errors, optionally for the
named fields. The form-submit counterpart of [assertInvalid](#assertinvalid).

#### Parameters

##### fields?

`string` \| `string`[]

#### Returns

`this`

#### Example

```ts
res.assertSessionHasErrors(['email']);
```

***

### assertSessionHasNoErrors()

> **assertSessionHasNoErrors**(): `this`

Defined in: [packages/testing/src/TestResponse.ts:635](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L635)

Assert the session carries no flashed validation errors.

#### Returns

`this`

***

### assertAuthenticated()

> **assertAuthenticated**(): `this`

Defined in: [packages/testing/src/TestResponse.ts:649](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L649)

Assert the response leaves someone signed in — the session carries the
`user_id` that AuthSessionMiddleware hydrates `ctx.user` from.

#### Returns

`this`

#### Example

```ts
const res = await app.followingRedirects().post('/login', creds);
res.assertAuthenticated();
```

***

### assertAuthenticatedAs()

> **assertAuthenticatedAs**(`user`): `this`

Defined in: [packages/testing/src/TestResponse.ts:674](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L674)

Assert the response leaves the given user signed in.

Accepts the user or a bare id, and compares loosely across the number/string
divide — a session round-trips through JSON, so an integer key can come back
either way depending on the driver.

#### Parameters

##### user

`string` \| `number` \| \{ `id`: `string` \| `number`; \}

#### Returns

`this`

#### Example

```ts
res.assertAuthenticatedAs(user);
res.assertAuthenticatedAs(42);
```

***

### assertGuest()

> **assertGuest**(): `this`

Defined in: [packages/testing/src/TestResponse.ts:690](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L690)

Assert the response leaves nobody signed in.

#### Returns

`this`

***

### inertia()

> **inertia**(): [`InertiaPage`](../interfaces/InertiaPage) \| `null`

Defined in: [packages/testing/src/TestResponse.ts:712](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L712)

The Inertia page object this response carries, from either shape the
protocol uses: the JSON body of an `X-Inertia` visit, or the
`<script data-page>` payload embedded in a full page load. `null` when the
response is not an Inertia response.

#### Returns

[`InertiaPage`](../interfaces/InertiaPage) \| `null`

***

### assertInertia()

> **assertInertia**(`component?`, `props?`): `this`

Defined in: [packages/testing/src/TestResponse.ts:740](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L740)

Assert the response rendered an Inertia page — optionally the named
component, optionally carrying the given props.

Props are matched partially, so you assert the ones the test is about and
ignore the shared props riding along with every page.

#### Parameters

##### component?

`string`

##### props?

`Record`\<`string`, `unknown`\>

#### Returns

`this`

#### Example

```ts
res.assertInertia('Posts/Index');
res.assertInertia('Posts/Show', { post: { id: 1, title: 'Hello' } });
```

***

### assertInertiaProp()

> **assertInertiaProp**(`key`, `value?`): `this`

Defined in: [packages/testing/src/TestResponse.ts:774](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/TestResponse.ts#L774)

Assert the Inertia page carries the named prop, optionally matching a value.

#### Parameters

##### key

`string`

##### value?

`unknown`

#### Returns

`this`
