Skip to content

Commit

Permalink
feat: message param for expectStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilc7 committed Apr 28, 2024
1 parent 1e42a64 commit 61339b5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/models/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,9 @@ class Spec {
return this;
}

expectStatus(statusCode) {
expectStatus(statusCode, message = '') {
this._expect.statusCode = statusCode;
this._expect.customMessage = message;
return this;
}

Expand Down
3 changes: 2 additions & 1 deletion src/models/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/component/expects.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 61339b5

Please sign in to comment.