Skip to content

Commit 93ab75d

Browse files
Copilotkobenguyent
andauthored
3.7.3 I.seeResponseContainsJson not working (#5081)
* Initial plan * Changes before error encountered Co-authored-by: kobenguyent <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: kobenguyent <[email protected]>
1 parent e3a195d commit 93ab75d

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

lib/helper/JSONResponse.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,25 @@ class JSONResponse extends Helper {
349349
for (const key in expected) {
350350
assert(key in actual, `Key "${key}" not found in ${JSON.stringify(actual)}`)
351351
if (typeof expected[key] === 'object' && expected[key] !== null) {
352-
this._assertContains(actual[key], expected[key])
352+
if (Array.isArray(expected[key])) {
353+
// Handle array comparison: each expected element should have a match in actual array
354+
assert(Array.isArray(actual[key]), `Expected array for key "${key}", but got ${typeof actual[key]}`)
355+
for (const expectedItem of expected[key]) {
356+
let found = false
357+
for (const actualItem of actual[key]) {
358+
try {
359+
this._assertContains(actualItem, expectedItem)
360+
found = true
361+
break
362+
} catch (err) {
363+
continue
364+
}
365+
}
366+
assert(found, `No matching element found in array for ${JSON.stringify(expectedItem)}`)
367+
}
368+
} else {
369+
this._assertContains(actual[key], expected[key])
370+
}
353371
} else {
354372
assert.deepStrictEqual(actual[key], expected[key], `Values for key "${key}" don't match`)
355373
}

test/helper/JSONResponse_test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe('JSONResponse', () => {
8282
I.seeResponseContainsJson({
8383
posts: [{ id: 1, author: 'davert' }],
8484
})
85-
expect(() => I.seeResponseContainsJson({ posts: [{ id: 2, author: 'boss' }] })).to.throw('expected { …(2) } to deeply match { Object (posts) }')
85+
expect(() => I.seeResponseContainsJson({ posts: [{ id: 2, author: 'boss' }] })).to.throw('No matching element found in array for {"id":2,"author":"boss"}')
8686
})
8787

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

142142
it('should check for json by callback', () => {
143143
restHelper.config.onResponse({ data })
144-
const fn = ({ expect, data }) => {
145-
expect(data).to.have.keys(['posts', 'user'])
144+
const fn = ({ assert, data }) => {
145+
assert('posts' in data)
146+
assert('user' in data)
146147
}
147148
I.seeResponseValidByCallback(fn)
148-
expect(fn.toString()).to.include('expect(data).to.have')
149+
expect(fn.toString()).to.include("assert('posts' in data)")
149150
})
150151

151152
it('should check for json by joi schema', () => {

0 commit comments

Comments
 (0)