From 4d5dabdf67406dc6e93effc30d6606f8e4946a30 Mon Sep 17 00:00:00 2001 From: Sergej Shafarenka Date: Sun, 27 Aug 2017 10:37:54 +0200 Subject: [PATCH 1/2] Adding compare function to be used in expected object template --- lib/chai-subset.js | 3 +++ test/unit/chai-subset.spec.js | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/chai-subset.js b/lib/chai-subset.js index d2dce91..4ca3ba0 100644 --- a/lib/chai-subset.js +++ b/lib/chai-subset.js @@ -71,6 +71,9 @@ if (typeof(eo) === 'object' && eo !== null && ao !== null) { return compare(eo, ao); } + if (typeof(eo) === 'function') { + return eo(ao); + } return ao === eo; }); } diff --git a/test/unit/chai-subset.spec.js b/test/unit/chai-subset.spec.js index 3fc4e28..042067d 100644 --- a/test/unit/chai-subset.spec.js +++ b/test/unit/chai-subset.spec.js @@ -126,6 +126,20 @@ describe('circular objects', function() { }); }); +describe('object with compare function', function() { + it('should pass when function returns true', function () { + expect({a: 5}).to.containSubset({a: a => a}); + }); + + it('should fail when function returns false', function () { + expect({a: 5}).to.not.containSubset({a: a => !a}); + }); + + it('should pass for function with no arguments', function () { + expect({a: 5}).to.containSubset({a: () => true}); + }); +}); + describe('comparison of non objects', function () { it('should fail if actual subset is null', function () { expect(null).to.not.containSubset({a: 1}); From cf6b56d423d8841717e399bf94701b26e73aa504 Mon Sep 17 00:00:00 2001 From: Sergej Shafarenka Date: Thu, 31 Aug 2017 18:24:51 +0200 Subject: [PATCH 2/2] Adding compare function example to README.md --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 80ad1b1..2e50904 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,14 @@ expect(obj).to.containSubset({ } } }); -//or with 'not' + +// or using a compare function +expect(obj).containSubset({ + a: (expectedValue) => expectedValue, + c: (expectedValue) => expectedValue === 'd' +}) + +// or with 'not' expect(obj).to.not.containSubset({ g: 'whatever' });