Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: message param for expectStatus #350

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading