@@ -88,19 +88,18 @@ expect(6).to.be.in.range(5, 6);
88
88
### Flags
89
89
90
90
The following words toggle a status flag for the current assertion:
91
- - ` deep ` - performs a deep comparison instead of simple equality (` === ` ). Required when trying to compare
92
- objects to an identical copy that is not the same reference. Used by ` equal() ` and ` include() ` .
93
91
- ` not ` - inverses the expected result of any assertion.
94
92
- ` once ` - requires that inclusion matches appear only once in the provided value. Used by ` include() ` .
95
93
- ` only ` - requires that only the provided elements appear in the provided value. Used by ` include() ` .
96
94
- ` part ` - allows a partial match when asserting inclusion. Used by ` include() ` .
95
+ - ` shallow ` - performs a comparison using strict equality (` === ` ). Code defaults to deep comparison. Used by ` equal() ` and ` include() ` .
97
96
98
97
``` js
99
98
const Code = require (' code' );
100
99
const expect = Code .expect ;
101
100
102
101
expect (10 ).to .not .be .above (20 );
103
- expect ([{ a : 1 } ]).to .deep .include ({ a : 1 } );
102
+ expect ([1 , 2 , 3 ]).to .shallow .include (3 );
104
103
expect ([1 , 1 , 2 ]).to .only .include ([1 , 2 ]);
105
104
expect ([1 , 2 ]).to .once .include ([1 , 2 ]);
106
105
expect ([1 , 2 , 3 ]).to .part .include ([1 , 4 ]);
@@ -256,7 +255,7 @@ expect({ a: '1' }).to.be.an.object();
256
255
257
256
#### Values
258
257
259
- Asserts that the reference value is equals to a predefined value.
258
+ Asserts that the reference value is equal to a predefined value.
260
259
261
260
##### ` true() `
262
261
@@ -308,8 +307,7 @@ Aliases: `includes()`, `contain()`, `contains()`
308
307
309
308
Asserts that the reference value (a string, array, or object) includes the provided values where:
310
309
- ` values ` - a single or array of values. If the reference value is a string, the values must be strings.
311
- If the reference value is an array, the values can be any array member (` deep ` is required to compare
312
- non-literal types). If the reference value is an object, the values can be key names, or a single object
310
+ If the reference value is an array, the values can be any array member. If the reference value is an object, the values can be key names, or a single object
313
311
with key-value pairs to match.
314
312
315
313
``` js
@@ -324,13 +322,13 @@ expect('abc').to.include(['a', 'c']);
324
322
expect (' abc' ).to .part .include ([' a' , ' d' ]);
325
323
326
324
expect ([1 , 2 , 3 ]).to .include (1 );
327
- expect ([{ a: 1 }]).to .deep . include ({ a: 1 });
325
+ expect ([{ a: 1 }]).to .include ({ a: 1 });
328
326
expect ([1 , 2 , 3 ]).to .include ([1 , 2 ]);
329
- expect ([{ a: 1 }]).to .deep . include ([{ a: 1 }]);
327
+ expect ([{ a: 1 }]).to .include ([{ a: 1 }]);
330
328
expect ([1 , 1 , 2 ]).to .only .include ([1 , 2 ]);
331
329
expect ([1 , 2 ]).to .once .include ([1 , 2 ]);
332
330
expect ([1 , 2 , 3 ]).to .part .include ([1 , 4 ]);
333
- expect ([[1 ], [2 ]]).to .deep . include ([[1 ]]);
331
+ expect ([[1 ], [2 ]]).to .include ([[1 ]]);
334
332
335
333
expect ({ a: 1 , b: 2 , c: 3 }).to .include (' a' );
336
334
expect ({ a: 1 , b: 2 , c: 3 }).to .include ([' a' , ' c' ]);
@@ -339,7 +337,7 @@ expect({ a: 1, b: 2, c: 3 }).to.include({ a: 1 });
339
337
expect ({ a: 1 , b: 2 , c: 3 }).to .include ({ a: 1 , c: 3 });
340
338
expect ({ a: 1 , b: 2 , c: 3 }).to .part .include ({ a: 1 , d: 4 });
341
339
expect ({ a: 1 , b: 2 , c: 3 }).to .only .include ({ a: 1 , b: 2 , c: 3 });
342
- expect ({ a: [1 ], b: [2 ], c: [3 ] }).to .deep . include ({ a: [1 ], c: [3 ] });
340
+ expect ({ a: [1 ], b: [2 ], c: [3 ] }).to .include ({ a: [1 ], c: [3 ] });
343
341
```
344
342
345
343
#### ` startWith(value) `
@@ -417,21 +415,19 @@ expect('abcd').to.have.length(4);
417
415
418
416
Aliases: ` equals() `
419
417
420
- Asserts that the reference value equals the provided value (` deep ` is required to compare non-literal
421
- types) where:
418
+ Asserts that the reference value equals the provided value where:
422
419
- ` value ` - the value to compare to.
423
- - ` options ` - optional object specifying comparison options. This is only used on
424
- deep comparisons, and is ignored otherwise.
420
+ - ` options ` - optional object specifying comparison options. This is ignored on ` shallow ` comparisons.
425
421
426
422
``` js
427
423
const Code = require (' code' );
428
424
const expect = Code .expect ;
429
425
430
426
expect (5 ).to .equal (5 );
431
- expect ({ a: 1 }).to .deep . equal ({ a: 1 });
427
+ expect ({ a: 1 }).to .equal ({ a: 1 });
432
428
```
433
429
434
- Deep comparisons are performed using
430
+ Deep comparisons (the default) are performed using
435
431
[ ` Hoek.deepEqual() ` ] ( https://github.com/hapijs/hoek/blob/master/API.md#deepequalb-a-options ) . The
436
432
optional ` options ` argument is passed directly to ` Hoek.deepEqual() ` . An example
437
433
deep comparison which ignores object prototypes is shown below.
@@ -440,7 +436,17 @@ deep comparison which ignores object prototypes is shown below.
440
436
const Code = require (' code' );
441
437
const expect = Code .expect ;
442
438
443
- expect (Object .create (null )).to .deep .equal ({}, { prototype: false });
439
+ expect (Object .create (null )).to .equal ({}, { prototype: false });
440
+ ```
441
+
442
+ Strict equality can be checked using the ` shallow ` modifier. This yields the same output as a ` === ` check.
443
+
444
+ ``` js
445
+ const Code = require (' code' );
446
+ const expect = Code .expect ;
447
+
448
+ expect (5 ).to .shallow .equal (5 );
449
+ expect ({ a: 1 }).to .shallow .equal ({ a: 1 }); // fails as they are not the same reference
444
450
```
445
451
446
452
#### ` above(value) `
@@ -680,18 +686,21 @@ const expect = Code.expect;
680
686
const foo = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ];
681
687
682
688
Code .settings .truncateMessages = false ;
683
- expect (foo).to .deep . equal ([]);
689
+ expect (foo).to .equal ([]);
684
690
```
685
691
686
692
#### ` comparePrototypes `
687
693
688
- A boolean value that, when ` false ` , ignores object prototypes when doing a deep comparison. Defaults to ` true ` .
694
+ A Boolean value that, when ` false ` , ignores object prototypes when doing a deep comparison. Defaults to ` false ` .
689
695
690
696
``` js
691
697
const Code = require (' code' );
692
698
const expect = Code .expect ;
693
699
const foo = Object .create (null );
694
700
695
- Code .setting .comparePrototypes = false ;
696
- expect (foo).to .deep .equal ({});
701
+ Code .settings .comparePrototypes = false ;
702
+ expect (foo).to .equal ({});
703
+
704
+ Code .settings .comparePrototypes = true ;
705
+ expect (foo).to .equal ({}); // fails
697
706
```
0 commit comments