Repository: pactumjs/pactum
Pull request #45 (2021-03-05 03:06:32)

added expectJsonMatchStrict assertions
- expectJsonMatchStrict
- expectJsonMatchStrictAt


Patch:
diff --git a/src/models/Spec.d.ts b/src/models/Spec.d.ts
index 63a67c4..4572a6d 100644
--- a/src/models/Spec.d.ts
+++ b/src/models/Spec.d.ts
@@ -474,6 +474,16 @@ declare class Spec {
    */
   expectJsonMatchAt(path: string, value: object): Spec;
 
+  /**
+   * expects the json to strictly match with value
+   */
+  expectJsonMatchStrict(value: object): Spec;
+
+  /**
+   * expects the json at path to strictly match with value
+   */
+  expectJsonMatchStrictAt(value: object): Spec;
+
   /**
    * expects the json to match with stored snapshots
    * @example
diff --git a/src/models/Spec.js b/src/models/Spec.js
index 36f309a..0bba29a 100644
--- a/src/models/Spec.js
+++ b/src/models/Spec.js
@@ -329,6 +329,16 @@ class Spec {
     return this;
   }
 
+  expectJsonMatchStrict(value) {
+    this._expect.jsonMatchStrict.push(value);
+    return this;
+  }
+
+  expectJsonMatchStrictAt(path, value) {
+    this._expect.jsonMatchStrictQuery.push({ path, value });
+    return this;
+  }
+
   expectJsonSnapshot(matchers) {
     this._expect.jsonSnapshot.push(matchers);
     return this;
diff --git a/src/models/expect.js b/src/models/expect.js
index e0b5177..cf658a8 100644
--- a/src/models/expect.js
+++ b/src/models/expect.js
@@ -25,6 +25,8 @@ class Expect {
     this.jsonSchemaQuery = [];
     this.jsonMatch = [];
     this.jsonMatchQuery = [];
+    this.jsonMatchStrict = [];
+    this.jsonMatchStrictQuery = [];
     this.jsonSnapshot = [];
     this.headers = [];
     this.headerContains = [];
@@ -46,6 +48,8 @@ class Expect {
     this._validateJsonSchemaQuery(response);
     this._validateJsonMatch(response);
     this._validateJsonMatchQuery(response);
+    this._validateJsonMatchStrict(response);
+    this._validateJsonMatchStrictQuery(response);
     this._validateJsonSnapshot(response);
     this._validateResponseTime(response);
     // for asynchronous expectations
@@ -270,6 +274,33 @@ class Expect {
     }
   }
 
+  _validateJsonMatchStrict(response) {
+    this.jsonMatchStrict = processor.processData(this.jsonMatchStrict);
+    for (let i = 0; i < this.jsonMatchStrict.length; i++) {
+      const data = this.jsonMatchStrict[i];
+      const rules = jmv.getMatchingRules(data, '$.body');
+      const value = jmv.getRawValue(data);
+      const errors = jmv.validate(response.json, value, rules, '$.body', true);
+      if (errors) {
+        this.fail(errors.replace('$.body', '$'));
+      }
+    }
+  }
+
+  _validateJsonMatchStrictQuery(response) {
+    this.jsonMatchStrictQuery = processor.processData(this.jsonMatchStrictQuery);
+    for (let i = 0; i < this.jsonMatchStrictQuery.length; i++) {
+      const jQ = this.jsonMatchStrictQuery[i];
+      const actualValue = jqy(jQ.path, { data: response.json }).value;
+      const rules = jmv.getMatchingRules(jQ.value, jQ.path);
+      const expectedValue = jmv.getRawValue(jQ.value);
+      const errors = jmv.validate(actualValue, expectedValue, rules, jQ.path, true);
+      if (errors) {
+        this.fail(errors);
+      }
+    }
+  }
+
   _validateJsonSnapshot(response) {
     if (this.jsonSnapshot.length > 0) {
       if (!this.name) {


Test patch:
diff --git a/test/component/expects.spec.js b/test/component/expects.spec.js
index ebc65e8..84eff6f 100644
--- a/test/component/expects.spec.js
+++ b/test/component/expects.spec.js
@@ -620,6 +620,56 @@ describe('Expects', () => {
     expect(err).not.undefined;
   });
 
+  it('json strict match at', async () => {
+    await pactum.spec()
+      .useInteraction('get people')
+      .get('http://localhost:9393/api/people')
+      .expectStatus(200)
+      .expectJsonMatchStrictAt('people[*].name', eachLike('Matt'));
+  });
+
+  it('json match strict at - fails', async () => {
+    let err;
+    try {
+      await pactum.spec()
+        .useInteraction('get people')
+        .get('http://localhost:9393/api/people')
+        .expectStatus(200)
+        .expectJsonMatchStrictAt('people[*].name', eachLike(12));
+    } catch (error) {
+      err = error;
+    }
+    expect(err).not.undefined;
+  });
+
+  it('json strict match', async () => {
+    await pactum.spec()
+      .useInteraction('get people')
+      .get('http://localhost:9393/api/people')
+      .expectStatus(200)
+      .expectJsonMatchStrict({
+        people: [
+          { name: 'Matt', country: 'NZ' },
+          { name: 'Pete', country: 'AU' },
+          { name: 'Mike', country: 'NZ' }
+        ]
+      });
+  });
+
+  it('json match strict - fails', async () => {
+    let err;
+    try {
+      await pactum.spec()
+        .useInteraction('get people')
+        .get('http://localhost:9393/api/people')
+        .expectStatus(200)
+        .expectJsonMatchStrict({});
+    } catch (error) {
+      err = error;
+    }
+    expect(err).not.undefined;
+  });
+
   it('json snapshot - deep equal', async () => {
     await pactum.spec()
       .useInteraction('get people')