Repository: pactumjs/pactum
Pull request #351 (2024-04-30 14:25:41)

fix: test cases for node 20


Patch:
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 6a7f924..81dcb1f 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -22,7 +22,7 @@ jobs:
     strategy:
       matrix:
         os: [windows-latest, ubuntu-latest, macos-latest]
-        node-version: [18.x]
+        node-version: [18.x, 20.x]
 
     steps:
     - uses: actions/checkout@v2
diff --git a/src/exports/expect.js b/src/exports/expect.js
index 5670283..165c7d9 100644
--- a/src/exports/expect.js
+++ b/src/exports/expect.js
@@ -11,7 +11,7 @@ class Have {
 
   status(code, message = '') {
     this.expect.statusCode = code;
-    this._expect.customMessage = message;
+    this.expect.customMessage = message;
     this._validate();
   }
 
diff --git a/src/models/expect.js b/src/models/expect.js
index c151674..5916884 100644
--- a/src/models/expect.js
+++ b/src/models/expect.js
@@ -345,7 +345,7 @@ class Expect {
         if (this.updateSnapshot) {
           log.warn(`Update snapshot is enabled for '${snapshot_name}'`);
           file.saveSnapshot(snapshot_name, response.json);
-        }    
+        }
         if (value) {
           const current_rules = jmv.getMatchingRules(value, '$.body');
           let errors = jmv.validate(actual,  jmv.getRawValue(value), current_rules, '$.body');
@@ -355,7 +355,7 @@ class Expect {
           Object.assign(all_rules, current_rules);
         }
       }
-      
+
       const expected = file.getSnapshotFile(snapshot_name, response.json);
       if (Object.keys(all_rules).length > 0) {
         const errors = jmv.validate(actual, expected, all_rules, '$.body', true);
@@ -420,9 +420,16 @@ class Expect {
       for (let i = 0; i < this.errors.length; i++) {
         const expected = this.errors[i];
         if (typeof expected === 'string') {
-          const actual = response.toString();
-          if (!actual.includes(expected)) {
-            this.fail(`Error - "${actual}" doesn't include - ${expected}`);
+          if (response.errors && Array.isArray(response.errors) && response.errors.length > 0) {
+            const actual = response.errors[0].toString();
+            if (!actual.includes(expected)) {
+              this.fail(`Error - "${actual}" doesn't include - ${expected}`);
+            }
+          } else {
+            const actual = response.toString();
+            if (!actual.includes(expected)) {
+              this.fail(`Error - "${actual}" doesn't include - ${expected}`);
+            }
           }
         }
         if (typeof expected === 'object') {


Test patch:
diff --git a/test/component/bdd.spec.js b/test/component/bdd.spec.js
index 098efc3..4bd5813 100644
--- a/test/component/bdd.spec.js
+++ b/test/component/bdd.spec.js
@@ -54,8 +54,8 @@ describe('BDD', () => {
   });
 
   it('should return a header', () => {
-    expect(response).to.have.header('connection', 'close');
-    expect(response).to.have.headerContains('connection', 'cl');
+    expect(response).to.have.header('connection', /\w+/);
+    expect(response).to.have.headerContains('connection', 'l');
   });
 
   it('should return cookies', () => {
diff --git a/test/component/expects.spec.js b/test/component/expects.spec.js
index 90cd097..b67bf9c 100644
--- a/test/component/expects.spec.js
+++ b/test/component/expects.spec.js
@@ -174,7 +174,7 @@ describe('Expects', () => {
     } catch (error) {
       err = error;
     }
-    expect(err.message).equals(`Header value 'value' did not match for header 'connection': 'close'`);
+    expect(err.message).includes(`Header value 'value' did not match for header 'connection':`);
   });
 
   it('header value not found - RegEx', async () => {
@@ -187,7 +187,7 @@ describe('Expects', () => {
     } catch (error) {
       err = error;
     }
-    expect(err.message).equals(`Header regex (/value/) did not match for header 'connection': 'close'`);
+    expect(err.message).includes(`Header regex (/value/) did not match for header 'connection':`);
   });
 
   it('header contains key not found', async () => {
@@ -213,7 +213,7 @@ describe('Expects', () => {
     } catch (error) {
       err = error;
     }
-    expect(err.message).equals(`Header value 'value' did not match for header 'connection': 'close'`);
+    expect(err.message).includes(`Header value 'value' did not match for header 'connection':`);
   });
 
   it('header contains value not found - RegEx', async () => {
@@ -226,7 +226,7 @@ describe('Expects', () => {
     } catch (error) {
       err = error;
     }
-    expect(err.message).equals(`Header regex (/value/) did not match for header 'connection': 'close'`);
+    expect(err.message).includes(`Header regex (/value/) did not match for header 'connection':`);
   });
 
   it('failed body', async () => {
diff --git a/test/component/interactions.spec.js b/test/component/interactions.spec.js
index 601b6d4..ec6b194 100644
--- a/test/component/interactions.spec.js
+++ b/test/component/interactions.spec.js
@@ -508,9 +508,7 @@ describe('Mock', () => {
       .put('http://localhost:9393/api/projects/1')
       .withBody("Hello")
       .expectStatus(404)
-      .expectBody('Interaction Not Found')
-      .expectHeader('connection', 'close')
-      .expectHeaderContains('connection', 'close');
+      .expectBody('Interaction Not Found');
   });
 
   it('PATCH - invalid interaction', async () => {
diff --git a/test/component/remote.spec.js b/test/component/remote.spec.js
index fa0be35..c02f832 100644
--- a/test/component/remote.spec.js
+++ b/test/component/remote.spec.js
@@ -123,7 +123,7 @@ describe('Remote- post single mock interaction', () => {
     await pactum.spec()
       .get(`http://localhost:9393/api/pactum/interactions?ids=${id}`)
       .expectStatus(200)
-      .expectJson([
+      .expectJsonLike([
         {
           "callCount": 1,
           "exercised": true,
@@ -134,8 +134,7 @@ describe('Remote- post single mock interaction', () => {
                 "path": "/api/projects/1",
                 "query": {},
                 "headers": {
-                  "host": "localhost:9393",
-                  "connection": "close"
+                  "host": "localhost:9393"
                 },
                 "body": ""
               },
diff --git a/test/component/response.spec.js b/test/component/response.spec.js
index 2dc3b79..dafbe16 100644
--- a/test/component/response.spec.js
+++ b/test/component/response.spec.js
@@ -60,14 +60,14 @@ describe('Response', () => {
   });
 
   it('with default expected response header', async () => {
-    response.setDefaultExpectHeaders('connection', 'close');
+    response.setDefaultExpectHeaders('content-type', 'application/json');
     await pactum.spec()
       .useInteraction('default get')
       .get('http://localhost:9393/default/get');
   });
 
   it('with default expected response header - failure', async () => {
-    response.setDefaultExpectHeaders('connection', 'open');
+    response.setDefaultExpectHeaders('content-type', 'application/xml');
     let err;
     try {
       await pactum.spec()
@@ -76,15 +76,15 @@ describe('Response', () => {
     } catch (error) {
       err = error;
     }
-    expect(err.message).equals(`Header value 'open' did not match for header 'connection': 'close'`);
+    expect(err.message).equals(`Header value 'application/xml' did not match for header 'content-type': 'application/json'`);
   });
 
   it('with default expected response header - override value', async () => {
-    response.setDefaultExpectHeaders('connection', 'open');
+    response.setDefaultExpectHeaders('content-type', 'application/xml');
     await pactum.spec()
       .useInteraction('default get')
       .get('http://localhost:9393/default/get')
-      .expectHeader('connection', 'close');
+      .expectHeader('content-type', 'application/json');
   });
 
   it('with default expected response handler', async () => {
diff --git a/test/component/returns.spec.js b/test/component/returns.spec.js
index 829f80b..7a0275b 100644
--- a/test/component/returns.spec.js
+++ b/test/component/returns.spec.js
@@ -141,8 +141,8 @@ describe('Returns', () => {
       })
       .get('http://localhost:9393/api/users')
       .expectStatus(200)
-      .returns('res.headers.connection');
-    expect(response).equals('close');
+      .returns('res.headers');
+    expect(response['content-type']).equals('application/json');
   });
 
   it('return request headers', async () => {