Skip to content

Commit

Permalink
feat: add assertBodyNotContains method
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-lefebvre committed Mar 20, 2023
1 parent d54700f commit 80ad638
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,15 @@ export class ApiResponse extends Macroable {
this.assert!.containsSubset(this.body(), expectedBody)
}

/**
* Assert response body not to match the subset from the
* expected body
*/
public assertBodyNotContains(expectedBody: any) {
this.ensureHasAssert()
this.assert!.notContainsSubset(this.body(), expectedBody)
}

/**
* Assert response to contain a given cookie and optionally
* has the expected value
Expand Down
18 changes: 18 additions & 0 deletions tests/response/assertions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ test.group('Response | assertions', (group) => {
response.assertBodyContains([{ message: 'hello world' }, { message: 'hi world' }])
})

test('assert response body not subset', async ({ assert }) => {
assert.plan(1)

httpServer.onRequest((_, res) => {
res.statusCode = 200
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify([{ message: 'hello world', time: new Date() }]))
})

const request = new ApiRequest(
{ baseUrl: httpServer.baseUrl, method: 'GET', endpoint: '/' },
assert
)

const response = await request
response.assertBodyNotContains([{ message: 'hi world' }])
})

test('assert response body when response is not json', async ({ assert }) => {
httpServer.onRequest((_, res) => {
res.statusCode = 401
Expand Down

0 comments on commit 80ad638

Please sign in to comment.