diff --git a/src/exports/expect.d.ts b/src/exports/expect.d.ts index f23ab62..92665ee 100644 --- a/src/exports/expect.d.ts +++ b/src/exports/expect.d.ts @@ -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; diff --git a/src/exports/expect.js b/src/exports/expect.js index f6f8e58..5670283 100644 --- a/src/exports/expect.js +++ b/src/exports/expect.js @@ -9,8 +9,9 @@ class Have { this.spec = spec; } - status(code) { + status(code, message = '') { this.expect.statusCode = code; + this._expect.customMessage = message; this._validate(); } diff --git a/src/models/Spec.d.ts b/src/models/Spec.d.ts index b3d9e27..f4ab9a9 100644 --- a/src/models/Spec.d.ts +++ b/src/models/Spec.d.ts @@ -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 diff --git a/src/models/Spec.js b/src/models/Spec.js index c3e308e..192497a 100644 --- a/src/models/Spec.js +++ b/src/models/Spec.js @@ -345,8 +345,9 @@ class Spec { return this; } - expectStatus(statusCode) { + expectStatus(statusCode, message = '') { this._expect.statusCode = statusCode; + this._expect.customMessage = message; return this; } diff --git a/src/models/expect.js b/src/models/expect.js index 70fb6af..c151674 100644 --- a/src/models/expect.js +++ b/src/models/expect.js @@ -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}`); } } diff --git a/test/component/expects.spec.js b/test/component/expects.spec.js index f9c029c..90cd097 100644 --- a/test/component/expects.spec.js +++ b/test/component/expects.spec.js @@ -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 {