Repository: pactumjs/pactum
Pull request #354 (2024-05-17 10:28:19)

Sleep after running a spec
Enhancement for the below issue:
https://github.com/pactumjs/pactum/issues/349

Hi @ASaiAnudeep ,
Could you pls review this PR

Rajaneeshkumar: @ASaiAnudeep Addressed review comments


Patch:
diff --git a/package-lock.json b/package-lock.json
index e96dab3..cdd6280 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
 {
   "name": "pactum",
-  "version": "3.6.8",
+  "version": "3.6.9",
   "lockfileVersion": 2,
   "requires": true,
   "packages": {
     "": {
       "name": "pactum",
-      "version": "3.6.8",
+      "version": "3.6.9",
       "license": "MIT",
       "dependencies": {
         "@exodus/schemasafe": "^1.3.0",
diff --git a/package.json b/package.json
index a1e57c8..b38fb3e 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "pactum",
-  "version": "3.6.8",
+  "version": "3.6.9",
   "description": "REST API Testing Tool for all levels in a Test Pyramid",
   "main": "./src/index.js",
   "types": "./src/index.d.ts",
@@ -11,6 +11,7 @@
     "test": "npm run test:unit && npm run test:component",
     "test:unit": "mocha --timeout 10000 ./test/unit/",
     "test:component": "mocha --timeout 10000 ./test/component/",
+    "test:component:tag": "mocha --timeout 10000 ./test/component/ --grep '@tag'",
     "coverage": "nyc --reporter=lcov --reporter=text npm run test",
     "lint": "eslint src/**/*.js"
   },
diff --git a/src/models/Spec.d.ts b/src/models/Spec.d.ts
index ff7c1b4..4bc4967 100644
--- a/src/models/Spec.d.ts
+++ b/src/models/Spec.d.ts
@@ -456,6 +456,12 @@ declare class Spec {
    * @see https://pactumjs.github.io/api/requests/end.html
    */
   end(): Spec;
+
+  /** 
+   * sleep after spec execution
+   * @see https://pactumjs.github.io/api/utils/sleep.html
+   */
+  sleep(ms: number): Spec;
 }
 
 declare namespace Spec { }
diff --git a/src/models/Spec.js b/src/models/Spec.js
index 8b25463..1a7da3b 100644
--- a/src/models/Spec.js
+++ b/src/models/Spec.js
@@ -39,11 +39,17 @@ class Spec {
     this._save = null;
     this._data_maps = [];
     this._specHandlerData = data;
+    this._sleep = '';
     hr.spec(name, data, this);
     this._opts = opts || {};
     this._opts.handler_name = name;
     this._expect.setDefaultResponseExpectations();
   }
+  
+  sleep(ms) {
+    this._sleep = ms;
+    return this;
+  }
 
   name(value) {
     this._name = value;
diff --git a/src/models/Tosser.js b/src/models/Tosser.js
index d5fca2c..5cca5ca 100644
--- a/src/models/Tosser.js
+++ b/src/models/Tosser.js
@@ -19,6 +19,7 @@ class Tosser {
     this.request = spec._request;
     this.state = spec._state;
     this.expect = spec._expect;
+    this.sleep = spec._sleep;
     this.interactions = spec.interactions;
     this.previousLogLevel = spec.previousLogLevel;
     this.response = {};
@@ -50,6 +51,7 @@ class Tosser {
       }
       return th.getOutput(this.spec, this.spec._returns);
     } finally {
+      this.sleep > 0 && await helper.sleep(this.sleep);
       await this.removeInteractionsFromServer();
       this.setPreviousLogLevel();
     }


Test patch:
diff --git a/test/component/bdd.spec.js b/test/component/bdd.spec.js
index 4bd5813..95b7307 100644
--- a/test/component/bdd.spec.js
+++ b/test/component/bdd.spec.js
@@ -178,6 +178,12 @@ describe('BDD', () => {
     ce(err).not.undefined;
   });
 
+  it('Should sleep after spec', async () => {
+    const spec = pactum.spec();
+    spec.useInteraction('default get');
+    await spec.get('http://localhost:9393/default/get').expectStatus(200).sleep(10).toss();
+  });
+
 });
 
 describe('BDD - AutoReportRunner Disabled', () => {
diff --git a/test/component/spec.handler.spec.js b/test/component/spec.handler.spec.js
index 4c940f5..bc34d00 100644
--- a/test/component/spec.handler.spec.js
+++ b/test/component/spec.handler.spec.js
@@ -82,4 +82,18 @@ describe('Spec Handler', () => {
       });
   });
 
+  it('sleep in spec handler', async () => {
+    await pactum
+      .spec('get users')
+      .expectStatus(200)
+      .expectJson([
+        {
+          id: 1
+        },
+        {
+          id: 2
+        }
+      ])
+      .sleep(10);
+  });
 });
\ No newline at end of file