Skip to content

[Feature]: Add new assertion 'toHaveJSON' for APIResponse's #35355

Open
@cpAdm

Description

@cpAdm

🚀 Feature Request

Add toHaveJSON (and similarly toHaveText) to existing APIResponseAssertion for checking the JSON/text result of an APIResponse

Example

Current:

const response = await page.request.get('example.com/api');
const json = await response.json();
expect(json).toBe(...)

New:

const response = await page.request.get('example.com/api');
await expect(response).toHaveJSON(...)

// Should also work with any other GenericAssertions
expect(response).toHaveJSON(expect.objectContaining({
  list: expect.arrayContaining([2, 3]),
  obj: expect.objectContaining({ prop: expect.stringContaining('Hello') }),
}));

Motivation

With many API tests, we first have to get the JSON from the response manually and then make assertions on it. This can be easily be combined. This potentially also makes for a better error message when the response is not valid JSON.

(would be willing to submit a PR)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions