Skip to content

Commit 1a30c91

Browse files
committed
fix bug with overriding matchers
1 parent b3f6062 commit 1a30c91

File tree

9 files changed

+278
-18
lines changed

9 files changed

+278
-18
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pactum-matchers",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "collection of json matchers for contract testing",
55
"main": "./src/index.js",
66
"types": "./src/index.d.ts",

src/compare.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ function compare(actual, expected, rules, path) {
1616

1717
function getCurrentPathRule(rules, path) {
1818
if (rules[path]) return rules[path];
19+
const genericPath = path.replace(/\[\d+\]/g, '[*]');
20+
if (rules[genericPath]) return rules[genericPath];
1921
let dotIndex = path.lastIndexOf('.');
2022
const allPropsPath = `${path.slice(0, dotIndex)}.*`;
2123
if (rules[allPropsPath]) return rules[allPropsPath];
22-
const genericPath = path.replace(/\[\d+\]/g, '[*]');
2324
dotIndex = genericPath.lastIndexOf('.');
2425
const allPropsGenericPath = `${genericPath.slice(0, dotIndex)}.*`;
2526
if (rules[allPropsGenericPath]) return rules[allPropsGenericPath];
@@ -168,7 +169,7 @@ function compareWithGt(actual, expected, rule, path) {
168169
if (type !== 'number') {
169170
throw `Json doesn't have type 'number' at '${path}' but found '${type}'`;
170171
} else if (!(actual > expected)) {
171-
throw `Json doesn't have 'greater' value at '${path}' but found '${actual}'`;
172+
throw `Json doesn't have 'greater' value than '${expected}' at '${path}' but found '${actual}'`;
172173
}
173174
}
174175

@@ -177,7 +178,7 @@ function compareWithGte(actual, expected, rule, path) {
177178
if (type !== 'number') {
178179
throw `Json doesn't have type 'number' at '${path}' but found '${type}'`;
179180
} else if (!(actual >= expected)) {
180-
throw `Json doesn't have 'greater or equal' value at '${path}' but found '${actual}'`;
181+
throw `Json doesn't have 'greater or equal' value than '${expected}' at '${path}' but found '${actual}'`;
181182
}
182183
}
183184

@@ -186,7 +187,7 @@ function compareWithLt(actual, expected, rule, path) {
186187
if (type !== 'number') {
187188
throw `Json doesn't have type 'number' at '${path}' but found '${type}'`;
188189
} else if (!(actual < expected)) {
189-
throw `Json doesn't have 'lesser' value at '${path}' but found '${actual}'`;
190+
throw `Json doesn't have 'lesser' value than '${expected}' at '${path}' but found '${actual}'`;
190191
}
191192
}
192193

@@ -195,7 +196,7 @@ function compareWithLte(actual, expected, rule, path) {
195196
if (type !== 'number') {
196197
throw `Json doesn't have type 'number' at '${path}' but found '${type}'`;
197198
} else if (!(actual <= expected)) {
198-
throw `Json doesn't have 'lesser or equal' value at '${path}' but found '${actual}'`;
199+
throw `Json doesn't have 'lesser or equal' value than '${expected}' at '${path}' but found '${actual}'`;
199200
}
200201
}
201202

test/compare.matchers.test.js

Lines changed: 182 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const suite = require('uvu').suite;
22
const assert = require('uvu/assert');
3-
const { like, eachLike, regex, oneOf, expression, utils } = require('../src/index');
3+
const { like, eachLike, regex, oneOf, expression, gt, any, utils } = require('../src/index');
44
const { setMatchingRules, getValue, compare } = utils;
55

66
const test = suite('Compare With Matchers');
@@ -135,6 +135,75 @@ test('like - prop object in array inside nested object', () => {
135135
assert.equal(message, `Json doesn't have type 'string' at '$.body.cmd[2]' but found 'number'`);
136136
});
137137

138+
test('like - nested matchers - pass', () => {
139+
const actual = {
140+
name: 'jon',
141+
age: 8,
142+
address: {
143+
line: 'flat',
144+
zip: null
145+
}
146+
};
147+
const value = like({
148+
name: 'snow',
149+
age: gt(5),
150+
address: like({
151+
line: 'flat',
152+
zip: any('123')
153+
})
154+
});
155+
const rules = setMatchingRules({}, value, '$.body');
156+
const expected = getValue(value);
157+
const { message } = compare(actual, expected, rules, '$.body');
158+
assert.equal(message, '');
159+
});
160+
161+
test('like - nested matchers - fail at root', () => {
162+
const actual = {
163+
name: 'jon',
164+
age: 8,
165+
address: {
166+
line: 'flat',
167+
zip: null
168+
}
169+
};
170+
const value = like({
171+
name: 'snow',
172+
age: gt(10),
173+
address: like({
174+
line: 'flat',
175+
zip: any('123')
176+
})
177+
});
178+
const rules = setMatchingRules({}, value, '$.body');
179+
const expected = getValue(value);
180+
const { message } = compare(actual, expected, rules, '$.body');
181+
assert.equal(message, `Json doesn't have 'greater' value than '10' at '$.body.age' but found '8'`);
182+
});
183+
184+
test('like - nested matchers - fail at nested', () => {
185+
const actual = {
186+
name: 'jon',
187+
age: 8,
188+
address: {
189+
line: 'flat',
190+
zip: 3
191+
}
192+
};
193+
const value = like({
194+
name: 'snow',
195+
age: gt(7),
196+
address: like({
197+
line: 'flat',
198+
zip: gt(7)
199+
})
200+
});
201+
const rules = setMatchingRules({}, value, '$.body');
202+
const expected = getValue(value);
203+
const { message } = compare(actual, expected, rules, '$.body');
204+
assert.equal(message, `Json doesn't have 'greater' value than '7' at '$.body.address.zip' but found '3'`);
205+
});
206+
138207
test('eachLike - root string', () => {
139208
const actual = ['null'];
140209
const value = eachLike('some string');
@@ -235,6 +304,118 @@ test('eachLike - extra prop in nested object', () => {
235304
assert.equal(message, `Json doesn't have property 'pin' at '$.body[1].address[1]'`);
236305
});
237306

307+
test('eachLike - nested matchers - pass', () => {
308+
const actual = [{
309+
name: 'jon',
310+
age: 8,
311+
address: {
312+
line: 'flat',
313+
zip: null
314+
}
315+
}];
316+
const value = eachLike({
317+
name: 'snow',
318+
age: gt(5),
319+
address: like({
320+
line: 'flat',
321+
zip: any('123')
322+
})
323+
});
324+
const rules = setMatchingRules({}, value, '$.body');
325+
const expected = getValue(value);
326+
const { message } = compare(actual, expected, rules, '$.body');
327+
assert.equal(message, '');
328+
});
329+
330+
test('eachLike - nested matchers - multiple values - pass', () => {
331+
const actual = [
332+
{
333+
name: 'sand',
334+
age: 7,
335+
address: {
336+
line: 'south',
337+
zip: '1456'
338+
}
339+
},
340+
{
341+
name: 'jon',
342+
age: 8,
343+
address: {
344+
line: 'flat',
345+
zip: null
346+
}
347+
}
348+
];
349+
const value = eachLike({
350+
name: 'snow',
351+
age: gt(5),
352+
address: like({
353+
line: 'flat',
354+
zip: any('123')
355+
})
356+
});
357+
const rules = setMatchingRules({}, value, '$.body');
358+
const expected = getValue(value);
359+
const { message } = compare(actual, expected, rules, '$.body');
360+
assert.equal(message, '');
361+
});
362+
363+
test('eachLike - nested matchers - fail at root', () => {
364+
const actual = [{
365+
name: 'jon',
366+
age: 8,
367+
address: {
368+
line: 'flat',
369+
zip: null
370+
}
371+
}];
372+
const value = eachLike({
373+
name: 'snow',
374+
age: gt(10),
375+
address: like({
376+
line: 'flat',
377+
zip: any('123')
378+
})
379+
});
380+
const rules = setMatchingRules({}, value, '$.body');
381+
const expected = getValue(value);
382+
const { message } = compare(actual, expected, rules, '$.body');
383+
assert.equal(message, `Json doesn't have 'greater' value than '10' at '$.body[0].age' but found '8'`);
384+
});
385+
386+
test('eachLike - nested matchers - fail at nested', () => {
387+
const actual = [
388+
{
389+
name: 'jon',
390+
age: 8,
391+
address: {
392+
line: 'flat',
393+
zip: 13
394+
}
395+
},
396+
{
397+
name: 'sand',
398+
age: 8,
399+
address: {
400+
line: 'south',
401+
zip: 3
402+
}
403+
}
404+
];
405+
const value = eachLike({
406+
name: 'snow',
407+
age: gt(7),
408+
address: like({
409+
line: 'flat',
410+
zip: gt(7)
411+
})
412+
});
413+
const rules = setMatchingRules({}, value, '$.body');
414+
const expected = getValue(value);
415+
const { message } = compare(actual, expected, rules, '$.body');
416+
assert.equal(message, `Json doesn't have 'greater' value than '7' at '$.body[1].address.zip' but found '3'`);
417+
});
418+
238419
test('regex - root string', () => {
239420
const actual = 'null';
240421
const value = regex('some string', /\w+/);

test/rules.gt.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ test('Gt - custom value - root number - comparison fails with lesser integer num
4040
const expected = getValue(value);
4141
const { equal, message } = compare(actual, expected, rules, '$.body');
4242
assert.strictEqual(equal, false);
43-
assert.strictEqual(message, `Json doesn't have 'greater' value at '$.body' but found '${actual}'`);
43+
assert.strictEqual(message, `Json doesn't have 'greater' value than '369' at '$.body' but found '${actual}'`);
4444
});
4545

4646
test('Gt - custom value - root number - comparison passes with greater float number', () => {
@@ -78,7 +78,7 @@ test('Gt - custom value - root number - comparison fails with lesser float numbe
7878
const expected = getValue(value);
7979
const { equal, message } = compare(actual, expected, rules, '$.body');
8080
assert.strictEqual(equal, false);
81-
assert.strictEqual(message, `Json doesn't have 'greater' value at '$.body' but found '${actual}'`);
81+
assert.strictEqual(message, `Json doesn't have 'greater' value than '369.08' at '$.body' but found '${actual}'`);
8282
});
8383

8484
test('Gt - custom value - root number - comparison fails with empty string', () => {

test/rules.gte.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ test('Gte - custom value - root number - comparison fails with lesser integer nu
5959
const expected = getValue(value);
6060
const { equal, message } = compare(actual, expected, rules, '$.body');
6161
assert.strictEqual(equal, false);
62-
assert.strictEqual(message, `Json doesn't have 'greater or equal' value at '$.body' but found '${actual}'`);
62+
assert.strictEqual(message, `Json doesn't have 'greater or equal' value than '369' at '$.body' but found '${actual}'`);
6363
});
6464

6565
test('Gte - custom value - root number - comparison passes with greater float number', () => {
@@ -116,7 +116,7 @@ test('Gte - custom value - root number - comparison fails with lesser float numb
116116
const expected = getValue(value);
117117
const { equal, message } = compare(actual, expected, rules, '$.body');
118118
assert.strictEqual(equal, false);
119-
assert.strictEqual(message, `Json doesn't have 'greater or equal' value at '$.body' but found '${actual}'`);
119+
assert.strictEqual(message, `Json doesn't have 'greater or equal' value than '369.08' at '$.body' but found '${actual}'`);
120120
});
121121

122122
test('Gte - custom value - root number - comparison fails with empty string', () => {

test/rules.lt.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ test('Lt - custom value - root number - comparison fails with greater integer nu
4040
const expected = getValue(value);
4141
const { equal, message } = compare(actual, expected, rules, '$.body');
4242
assert.strictEqual(equal, false);
43-
assert.strictEqual(message, `Json doesn't have 'lesser' value at '$.body' but found '${actual}'`);
43+
assert.strictEqual(message, `Json doesn't have 'lesser' value than '369' at '$.body' but found '${actual}'`);
4444
});
4545

4646
test('Lt - custom value - root number - comparison passes with lesser float number', () => {
@@ -78,7 +78,7 @@ test('Lt - custom value - root number - comparison fails with greater float numb
7878
const expected = getValue(value);
7979
const { equal, message } = compare(actual, expected, rules, '$.body');
8080
assert.strictEqual(equal, false);
81-
assert.strictEqual(message, `Json doesn't have 'lesser' value at '$.body' but found '${actual}'`);
81+
assert.strictEqual(message, `Json doesn't have 'lesser' value than '369.08' at '$.body' but found '${actual}'`);
8282
});
8383

8484
test('Lt - custom value - root number - comparison fails with empty string', () => {

test/rules.lte.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ test('Lte - custom value - root number - comparison fails with greater integer n
5959
const expected = getValue(value);
6060
const { equal, message } = compare(actual, expected, rules, '$.body');
6161
assert.strictEqual(equal, false);
62-
assert.strictEqual(message, `Json doesn't have 'lesser or equal' value at '$.body' but found '${actual}'`);
62+
assert.strictEqual(message, `Json doesn't have 'lesser or equal' value than '369' at '$.body' but found '${actual}'`);
6363
});
6464

6565
test('Lte - custom value - root number - comparison passes with lesser float number', () => {
@@ -116,7 +116,7 @@ test('Lte - custom value - root number - comparison fails with greater float num
116116
const expected = getValue(value);
117117
const { equal, message } = compare(actual, expected, rules, '$.body');
118118
assert.strictEqual(equal, false);
119-
assert.strictEqual(message, `Json doesn't have 'lesser or equal' value at '$.body' but found '${actual}'`);
119+
assert.strictEqual(message, `Json doesn't have 'lesser or equal' value than '369.08' at '$.body' but found '${actual}'`);
120120
});
121121

122122
test('Lte - custom value - root number - comparison fails with empty string', () => {

0 commit comments

Comments
 (0)