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 29, 2024
1 parent 1e42a64 commit a6ec1f0
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/exports/expect.d.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/exports/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ class Have {
this.spec = spec;
}

status(code) {
status(code, message = '') {
this.expect.statusCode = code;
this._expect.customMessage = message;
this._validate();
}

Expand Down
2 changes: 1 addition & 1 deletion src/models/Spec.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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 a6ec1f0

Please sign in to comment.