Repository: pactumjs/pactum
Pull request #350 (2024-04-28 20:28:44)

feat: message param for expectStatus
closes https://github.com/pactumjs/pactum/issues/308

akhil7000: > Changes are looking good. Please update
> 
> * typings file
>   https://github.com/pactumjs/pactum/blob/master/src/models/Spec.d.ts#L253
> * bdd expectations
>   https://github.com/pactumjs/pactum/blob/master/src/exports/expect.js#L12
>   https://github.com/pactumjs/pactum/blob/master/src/exports/expect.d.ts#L2

This is done, please review.

Patch:
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 6b2e444..6a7f924 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -5,29 +5,29 @@ on:
     paths-ignore:
      - 'docs/**'
      - 'README.md'
-    branches: 
+    branches:
       - master
   push:
     paths-ignore:
      - 'docs/**'
      - 'README.md'
-    branches: 
+    branches:
       - master
 
 jobs:
   build:
-    
+
     runs-on: ${{ matrix.os }}
 
     strategy:
       matrix:
         os: [windows-latest, ubuntu-latest, macos-latest]
-        node-version: [12.x, 14.x, 16.x, 18.x]
+        node-version: [18.x]
 
     steps:
     - uses: actions/checkout@v2
     - name: Use Node.js ${{ matrix.node-version }}
-      uses: actions/setup-node@v1
+      uses: actions/setup-node@v4
       with:
         node-version: ${{ matrix.node-version }}
     - run: npm install
diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml
index 1d92ee1..772d017 100644
--- a/.github/workflows/coverage.yml
+++ b/.github/workflows/coverage.yml
@@ -5,7 +5,7 @@ on:
     paths-ignore:
      - 'docs/**'
      - 'README.md'
-    branches: 
+    branches:
       - master
 
 jobs:
@@ -15,9 +15,9 @@ jobs:
 
     steps:
     - uses: actions/checkout@v2
-    - uses: actions/setup-node@v1
+    - uses: actions/setup-node@v4
       with:
-        node-version: '12'
+        node-version: '18.x'
     - run: npm install
     - name: Code Coverage
       uses: paambaati/codeclimate-action@v2.4.0
diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml
index 7c5cd16..87bfae2 100644
--- a/.github/workflows/publish.yml
+++ b/.github/workflows/publish.yml
@@ -8,9 +8,9 @@ jobs:
     steps:
       - uses: actions/checkout@v2
       # Setup .npmrc file to publish to npm
-      - uses: actions/setup-node@v2
+      - uses: actions/setup-node@v4
         with:
-          node-version: '12.x'
+          node-version: '18.x'
           registry-url: 'https://registry.npmjs.org'
       - run: npm install
       - run: npm test
diff --git a/src/exports/expect.d.ts b/src/exports/expect.d.ts
index f23ab62..92665ee 100644
--- a/src/exports/expect.d.ts
+++ b/src/exports/expect.d.ts
@@ -1,5 +1,5 @@
 export interface Have {
-  status(code: number): void;
+  status(code: number, message?: string): void;
   header(key: string, value: any): void;
   headerContains(key: string, value: any): void;
   cookiesLike(key: any, value?: any): void;
diff --git a/src/exports/expect.js b/src/exports/expect.js
index f6f8e58..5670283 100644
--- a/src/exports/expect.js
+++ b/src/exports/expect.js
@@ -9,8 +9,9 @@ class Have {
     this.spec = spec;
   }
 
-  status(code) {
+  status(code, message = '') {
     this.expect.statusCode = code;
+    this._expect.customMessage = message;
     this._validate();
   }
 
diff --git a/src/models/Spec.d.ts b/src/models/Spec.d.ts
index b3d9e27..f4ab9a9 100644
--- a/src/models/Spec.d.ts
+++ b/src/models/Spec.d.ts
@@ -250,7 +250,7 @@ declare class Spec {
    * expects a status code on the response
    * @see https://pactumjs.github.io/api/assertions/expectStatus.html
    */
-  expectStatus(code: number): Spec;
+  expectStatus(code: number, message?: string): Spec;
 
   /**
    * expects a header on the response
diff --git a/src/models/Spec.js b/src/models/Spec.js
index c3e308e..192497a 100644
--- a/src/models/Spec.js
+++ b/src/models/Spec.js
@@ -345,8 +345,9 @@ class Spec {
     return this;
   }
 
-  expectStatus(statusCode) {
+  expectStatus(statusCode, message = '') {
     this._expect.statusCode = statusCode;
+    this._expect.customMessage = message;
     return this;
   }
 
diff --git a/src/models/expect.js b/src/models/expect.js
index 70fb6af..c151674 100644
--- a/src/models/expect.js
+++ b/src/models/expect.js
@@ -102,7 +102,8 @@ class Expect {
   _validateStatus(response) {
     this.statusCode = processor.processData(this.statusCode);
     if (this.statusCode !== null) {
-      assert.strictEqual(response.statusCode, this.statusCode, `HTTP status ${response.statusCode} !== ${this.statusCode}`);
+      const message = this.customMessage ? `${this.customMessage}\n ` : '';
+      assert.strictEqual(response.statusCode, this.statusCode, `${message}HTTP status ${response.statusCode} !== ${this.statusCode}`);
     }
   }
 


Test patch:
diff --git a/test/component/expects.spec.js b/test/component/expects.spec.js
index f9c029c..90cd097 100644
--- a/test/component/expects.spec.js
+++ b/test/component/expects.spec.js
@@ -137,6 +137,20 @@ describe('Expects', () => {
     expect(err.message).equals('HTTP status 404 !== 200');
   });
 
+  it('failed status code with custom message', async () => {
+    const customMessage = 'An error occurred in the API call because it did not return the statusCode 200'
+    let err;
+    try {
+      await pactum.spec()
+        .get('http://localhost:9393/api/users/1')
+        .expectStatus(200, customMessage)
+        .useLogLevel('ERROR');
+    } catch (error) {
+      err = error;
+    }
+    expect(err.message).equals(`${customMessage}\n HTTP status 404 !== 200`);
+  });
+
   it('header key not found', async () => {
     let err;
     try {