@@ -168,63 +168,77 @@ export function expectResultCheck(
168
168
actual : Document ,
169
169
expected : Document | number | string | boolean ,
170
170
entities : EntitiesMap ,
171
- path : string [ ] = [ ]
171
+ path : string [ ] = [ ] ,
172
+ depth = 0
172
173
) : boolean {
173
- const result = resultCheck ( actual , expected , entities , path ) ;
174
- if ( result [ 0 ] === false ) {
175
- const path = result [ 1 ] . join ( '' ) ;
174
+ const ok = resultCheck ( actual , expected , entities , path , depth ) ;
175
+ if ( ok === false ) {
176
+ const pathString = path . join ( '' ) ;
176
177
const expectedJSON = JSON . stringify ( expected , undefined , 2 ) ;
177
178
const actualJSON = JSON . stringify ( actual , undefined , 2 ) ;
178
- expect . fail ( `Unable to match ${ expectedJSON } to ${ actualJSON } at ${ path } ` ) ;
179
+ expect . fail ( `Unable to match ${ expectedJSON } to ${ actualJSON } at ${ pathString } ` ) ;
179
180
}
180
- return result [ 0 ] ;
181
+ return ok ;
181
182
}
182
183
183
184
export function resultCheck (
184
185
actual : Document ,
185
186
expected : Document | number | string | boolean ,
186
187
entities : EntitiesMap ,
187
- path : string [ ]
188
- ) : [ ok : boolean , path : string [ ] ] {
188
+ path : string [ ] ,
189
+ depth = 0
190
+ ) : boolean {
189
191
if ( typeof expected === 'object' && expected !== null ) {
190
192
// Expected is an object
191
193
// either its a special operator or just an object to check equality against
192
194
193
195
if ( isSpecialOperator ( expected ) ) {
194
196
// Special operation check is a base condition
195
197
// specialCheck may recurse depending upon the check ($$unsetOrMatches)
196
- return [ specialCheck ( actual , expected , entities , path ) , path ] ;
198
+ return specialCheck ( actual , expected , entities , path , depth ) ;
197
199
} else {
198
200
// Just a plain object, however this object can contain special operations
199
201
// So we need to recurse over each key,value
200
202
let ok = true ;
201
203
const expectedEntries = Object . entries ( expected ) ;
204
+
205
+ if ( depth > 1 && Object . keys ( actual ) . length !== Object . keys ( expected ) . length ) {
206
+ throw new Error ( `[${ Object . keys ( actual ) } ] length !== [${ Object . keys ( expected ) } ]` ) ;
207
+ }
208
+
202
209
for ( const [ key , value ] of expectedEntries ) {
203
210
path . push ( Array . isArray ( expected ) ? `[${ key } ]` : `.${ key } ` ) ; // record what key we're at
204
- ok &&= expectResultCheck ( actual [ key ] , value , entities , path ) ;
211
+ depth += 1 ;
212
+ ok &&= expectResultCheck ( actual [ key ] , value , entities , path , depth ) ;
213
+ depth -= 1 ;
205
214
path . pop ( ) ; // if the recursion was successful we can drop the tested key
206
215
}
207
- return [ ok , path ] ;
216
+ return ok ;
208
217
}
209
218
} else {
210
219
// Here's our recursion base case
211
220
// expected is: number | string | boolean | null
212
- return [ isDeepStrictEqual ( actual , expected ) , path ] ;
221
+ return isDeepStrictEqual ( actual , expected ) ;
213
222
}
214
223
}
215
224
216
225
export function specialCheck (
217
226
actual : Document ,
218
227
expected : SpecialOperator ,
219
228
entities : EntitiesMap ,
220
- path : string [ ] = [ ]
229
+ path : string [ ] = [ ] ,
230
+ depth = 0
221
231
) : boolean {
222
232
let ok = false ;
223
233
if ( isUnsetOrMatchesOperator ( expected ) ) {
224
234
// $$unsetOrMatches
225
235
ok = true ; // start with true assumption
226
236
if ( actual === null || actual === undefined ) ok = true ;
227
- else ok &&= expectResultCheck ( actual , expected . $$unsetOrMatches , entities , path ) ;
237
+ else {
238
+ depth += 1 ;
239
+ ok &&= expectResultCheck ( actual , expected . $$unsetOrMatches , entities , path , depth ) ;
240
+ depth -= 1 ;
241
+ }
228
242
} else if ( isMatchesEntityOperator ( expected ) ) {
229
243
// $$matchesEntity
230
244
const entity = entities . get ( expected . $$matchesEntity ) ;
0 commit comments