diff --git a/lib/helper/JSONResponse.js b/lib/helper/JSONResponse.js index b45859dd9..908d4d0bf 100644 --- a/lib/helper/JSONResponse.js +++ b/lib/helper/JSONResponse.js @@ -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`) } diff --git a/test/helper/JSONResponse_test.js b/test/helper/JSONResponse_test.js index 146bbd643..6fc42fca5 100644 --- a/test/helper/JSONResponse_test.js +++ b/test/helper/JSONResponse_test.js @@ -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', () => { @@ -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', () => {