File tree Expand file tree Collapse file tree 2 files changed +24
-5
lines changed Expand file tree Collapse file tree 2 files changed +24
-5
lines changed Original file line number Diff line number Diff line change @@ -349,7 +349,25 @@ class JSONResponse extends Helper {
349
349
for ( const key in expected ) {
350
350
assert ( key in actual , `Key "${ key } " not found in ${ JSON . stringify ( actual ) } ` )
351
351
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
+ }
353
371
} else {
354
372
assert . deepStrictEqual ( actual [ key ] , expected [ key ] , `Values for key "${ key } " don't match` )
355
373
}
Original file line number Diff line number Diff line change @@ -82,7 +82,7 @@ describe('JSONResponse', () => {
82
82
I . seeResponseContainsJson ( {
83
83
posts : [ { id : 1 , author : 'davert' } ] ,
84
84
} )
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" }' )
86
86
} )
87
87
88
88
it ( 'should check for json inclusion - returned Array' , ( ) => {
@@ -141,11 +141,12 @@ describe('JSONResponse', () => {
141
141
142
142
it ( 'should check for json by callback' , ( ) => {
143
143
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 )
146
147
}
147
148
I . seeResponseValidByCallback ( fn )
148
- expect ( fn . toString ( ) ) . to . include ( 'expect( data).to.have' )
149
+ expect ( fn . toString ( ) ) . to . include ( "assert('posts' in data)" )
149
150
} )
150
151
151
152
it ( 'should check for json by joi schema' , ( ) => {
You can’t perform that action at this time.
0 commit comments