From 36d5aa60f1a9d5a7adea3464a9e54d5098d9c323 Mon Sep 17 00:00:00 2001 From: a4elru Date: Thu, 13 Jul 2023 17:25:00 +0300 Subject: [PATCH 1/2] fix eachLike in strict mode --- package.json | 2 +- src/compare.js | 6 ++++-- test/compare.eachLike.test.js | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 1c91423..a8ccf44 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pactum-matchers", - "version": "1.1.5", + "version": "1.1.6", "description": "collection of json matchers for contract testing", "main": "./src/index.js", "types": "./src/index.d.ts", diff --git a/src/compare.js b/src/compare.js index 08a2e9a..f1ff2e0 100644 --- a/src/compare.js +++ b/src/compare.js @@ -141,8 +141,10 @@ function compareWithRuleType(actual, expected, rules, regex_rules, path, rule) { if (actual.length < rule.min) { throw `Json doesn't have 'array' with min length of '${rule.min}' at '${path}' but found 'array' with length '${actual.length}'`; } - for (let i = 0; i < actual.length; i++) { - _compare(actual[i], expected[0], rules, regex_rules, `${path}[${i}]`); + if (typeof expected[0] !== 'undefined') { + for (let i = 0; i < actual.length; i++) { + _compare(actual[i], expected[0], rules, regex_rules, `${path}[${i}]`); + } } } else { arrayCompare(actual, expected, rules, regex_rules, path); diff --git a/test/compare.eachLike.test.js b/test/compare.eachLike.test.js index 73068bb..1068815 100644 --- a/test/compare.eachLike.test.js +++ b/test/compare.eachLike.test.js @@ -40,6 +40,20 @@ test('root object', () => { assert.equal(message, ''); }); +test('root object - empty array in strict mode', () => { + const actual = []; + const value = eachLike({ + name: 'snow', + age: 18 + }, { + min: 0 + }); + const rules = setMatchingRules({}, value, '$.body'); + const expected = getValue(value); + const { message } = compare(actual, expected, rules, '$.body', true); + assert.equal(message, ''); +}); + test('root object - one object fails', () => { const actual = [ { From d7870bb5790af819eb1c4a853ea8e0c9dbb05de0 Mon Sep 17 00:00:00 2001 From: a4elru Date: Tue, 1 Aug 2023 09:01:09 +0300 Subject: [PATCH 2/2] fix previous commit --- src/compare.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compare.js b/src/compare.js index f1ff2e0..b96473d 100644 --- a/src/compare.js +++ b/src/compare.js @@ -141,7 +141,7 @@ function compareWithRuleType(actual, expected, rules, regex_rules, path, rule) { if (actual.length < rule.min) { throw `Json doesn't have 'array' with min length of '${rule.min}' at '${path}' but found 'array' with length '${actual.length}'`; } - if (typeof expected[0] !== 'undefined') { + if (rule.min > 0 || typeof expected[0] !== 'undefined') { for (let i = 0; i < actual.length; i++) { _compare(actual[i], expected[0], rules, regex_rules, `${path}[${i}]`); }