Skip to content
Merged
Show file tree
Hide file tree
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
20 changes: 19 additions & 1 deletion lib/helper/JSONResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,25 @@ class JSONResponse extends Helper {
for (const key in expected) {
assert(key in actual, `Key "${key}" not found in ${JSON.stringify(actual)}`)
if (typeof expected[key] === 'object' && expected[key] !== null) {
this._assertContains(actual[key], expected[key])
if (Array.isArray(expected[key])) {
// Handle array comparison: each expected element should have a match in actual array
assert(Array.isArray(actual[key]), `Expected array for key "${key}", but got ${typeof actual[key]}`)
for (const expectedItem of expected[key]) {
let found = false
for (const actualItem of actual[key]) {
try {
this._assertContains(actualItem, expectedItem)
found = true
break
} catch (err) {
continue
}
}
assert(found, `No matching element found in array for ${JSON.stringify(expectedItem)}`)
}
} else {
this._assertContains(actual[key], expected[key])
}
} else {
assert.deepStrictEqual(actual[key], expected[key], `Values for key "${key}" don't match`)
}
Expand Down
9 changes: 5 additions & 4 deletions test/helper/JSONResponse_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('JSONResponse', () => {
I.seeResponseContainsJson({
posts: [{ id: 1, author: 'davert' }],
})
expect(() => I.seeResponseContainsJson({ posts: [{ id: 2, author: 'boss' }] })).to.throw('expected { …(2) } to deeply match { Object (posts) }')
expect(() => I.seeResponseContainsJson({ posts: [{ id: 2, author: 'boss' }] })).to.throw('No matching element found in array for {"id":2,"author":"boss"}')
})

it('should check for json inclusion - returned Array', () => {
Expand Down Expand Up @@ -141,11 +141,12 @@ describe('JSONResponse', () => {

it('should check for json by callback', () => {
restHelper.config.onResponse({ data })
const fn = ({ expect, data }) => {
expect(data).to.have.keys(['posts', 'user'])
const fn = ({ assert, data }) => {
assert('posts' in data)
assert('user' in data)
}
I.seeResponseValidByCallback(fn)
expect(fn.toString()).to.include('expect(data).to.have')
expect(fn.toString()).to.include("assert('posts' in data)")
})

it('should check for json by joi schema', () => {
Expand Down
Loading