Skip to content

Commit 24d7fa3

Browse files
committed
assert interface: add deepNestedInclude and notDeepNestedInclude
1 parent 7063b94 commit 24d7fa3

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

lib/chai/interface/assert.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ module.exports = function (chai, util) {
10131013
};
10141014

10151015
/**
1016-
* ### .nestedInclude
1016+
* ### .nestedInclude(targetObj, nestedObject, [msg])
10171017
*
10181018
* Asserts that 'targetObject' includes 'nestedObject'.
10191019
* Enables the use of dot- and bracket-notation for referencing nested properties.
@@ -1035,7 +1035,7 @@ module.exports = function (chai, util) {
10351035
};
10361036

10371037
/**
1038-
* ### .notNestedInclude
1038+
* ### .notNestedInclude(targetObj, nestedObj, [msg])
10391039
*
10401040
* Asserts that 'targetObject' does not include 'nestedObject'.
10411041
* Enables the use of dot- and bracket-notation for referencing nested properties.
@@ -1057,6 +1057,50 @@ module.exports = function (chai, util) {
10571057
new Assertion(exp, msg, assert.notNestedInclude, true).not.nested.include(inc);
10581058
};
10591059

1060+
/**
1061+
* ### .deepNestedInclude(targetObject, nestedObject, [msg])
1062+
*
1063+
* Asserts that 'targetObj' includes 'nestedObject' while checking for deep equality.
1064+
* Enables the user of dot- and bracket-notation for referencing nested properties.
1065+
* '[]' and '.' in property names can be escaped using double backslashes.
1066+
*
1067+
* assert.deepNestedInclude({a: {b: [{x: 1}]}}, {'a.b[0]': {x: 1}})
1068+
* assert.deepNestedInclude({'.a': {'[b]': {x: 1}}}, {'\\.a.\\[b\\]': {x: 1}});
1069+
*
1070+
* @name deepNestedInclude
1071+
* @param {Object} targetObject
1072+
* @param {Object} nestedObject
1073+
* @param {String} message
1074+
* @namespace Assert
1075+
* @api public
1076+
*/
1077+
1078+
assert.deepNestedInclude = function(exp, inc, msg) {
1079+
new Assertion(exp, msg, assert.deepNestedInclude, true).deep.nested.include(inc);
1080+
};
1081+
1082+
/**
1083+
* ### .notDeepNestedInclude(targetObject, nestedObject, [msg])
1084+
*
1085+
* Asserts that 'targetObj' does not include 'nestedObject' while checking for deep equality.
1086+
* Enables the user of dot- and bracket-notation for referencing nested properties.
1087+
* '[]' and '.' in property names can be escaped using double backslashes.
1088+
*
1089+
* assert.notDeepNestedInclude({a: {b: [{x: 1}]}}, {'a.b[0]': {y: 1}})
1090+
* assert.notDeepNestedInclude({'.a': {'[b]': {x: 1}}}, {'\\.a.\\[b\\]': {y: 2}});
1091+
*
1092+
* @name notDeepNestedInclude
1093+
* @param {Object} targetObject
1094+
* @param {Object} nestedObject
1095+
* @param {String} message
1096+
* @namespace Assert
1097+
* @api public
1098+
*/
1099+
1100+
assert.notDeepNestedInclude = function(exp, inc, msg) {
1101+
new Assertion(exp, msg, assert.notDeepNestedInclude, true).not.deep.nested.include(inc);
1102+
};
1103+
10601104
/**
10611105
* ### .match(value, regexp, [message])
10621106
*

test/assert.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -763,15 +763,15 @@ describe('assert', function () {
763763
}, "blah: expected { a: { b: [ 'x', 'y' ] } } to have nested property 'a.b[1]' of 'x', but got 'y'");
764764

765765
err(function () {
766-
assert.nestedInclude({a: {b: ['x', 'y']}}, 'blah', {'a.b[1]': 'x'});
766+
assert.nestedInclude({a: {b: ['x', 'y']}}, {'a.b[1]': 'x'}, 'blah');
767767
}, "blah: expected { a: { b: [ 'x', 'y' ] } } to have nested property 'a.b[1]' of 'x', but got 'y'");
768768

769769
err(function () {
770770
assert.nestedInclude({a: {b: ['x', 'y']}}, {'a.c': 'y'});
771771
}, "expected { a: { b: [ 'x', 'y' ] } } to have nested property 'a.c'");
772772

773773
err(function () {
774-
assert.nestedInclude({a: {b: ['x', 'y']}}, {'a.b[1]': 'y'});
774+
assert.notNestedInclude({a: {b: ['x', 'y']}}, {'a.b[1]': 'y'});
775775
}, "expected { a: { b: [ 'x', 'y' ] } } to not have nested property 'a.b[1]' of 'y'");
776776
});
777777

@@ -788,7 +788,7 @@ describe('assert', function () {
788788
}, "blah: expected { a: { b: [ [Object] ] } } to have deep nested property 'a.b[0]' of { y: 2 }, but got { x: 1 }");
789789

790790
err(function () {
791-
assert.deepNestedInclude({a: {b: [{x: 1}]}}, 'blah', {'a.b[0]': {y: 2}});
791+
assert.deepNestedInclude({a: {b: [{x: 1}]}}, {'a.b[0]': {y: 2}}, 'blah');
792792
}, "blah: expected { a: { b: [ [Object] ] } } to have deep nested property 'a.b[0]' of { y: 2 }, but got { x: 1 }");
793793

794794
err(function () {

0 commit comments

Comments
 (0)