Skip to content

Commit

Permalink
Sleep after running a spec (#354)
Browse files Browse the repository at this point in the history
* sleep after running a spec

* fix

* fix

* component tests

* Review commented addressed
  • Loading branch information
Rajaneeshkumar authored May 17, 2024
1 parent ea9686d commit 2005889
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
},
Expand Down
6 changes: 6 additions & 0 deletions src/models/Spec.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }
6 changes: 6 additions & 0 deletions src/models/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/models/Tosser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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();
}
Expand Down
6 changes: 6 additions & 0 deletions test/component/bdd.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
14 changes: 14 additions & 0 deletions test/component/spec.handler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 2005889

Please sign in to comment.