-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Framework update | chainable assertion
Chainable assertion response parameters as object and custom type
- Loading branch information
1 parent
fb28a00
commit 66ba599
Showing
9 changed files
with
114 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import supertest from 'supertest'; | ||
|
||
export const request = supertest(process.env.BASEURI || "https://reqres.in/api/"); | ||
export const request = supertest(process.env.BASEURI || "https://reqres.in/api"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
import { request } from "src/config/supertest"; | ||
import { endpoint } from "src/services/endpoints"; | ||
import { request } from "@Config/supertest"; | ||
import { logResponseToReport } from "@Helper/logger"; | ||
import { GetApiType, PostApiType } from "@Types/http"; | ||
import { Response } from "supertest"; | ||
|
||
export const postCall = async (service: endpoint, payload: object, headers?: object): Promise<Response> => { | ||
if (headers) return request.post(service).set(headers); | ||
return request.post(service).send(payload); | ||
export const httpPostCall = async (post: PostApiType): Promise<Response> => { | ||
let response = await request.post(post.service).set(post.headers ? post.headers : {}).send(post.payload); | ||
logResponseToReport(post.context, response); | ||
return response; | ||
} | ||
|
||
export const getCall = async (service: endpoint, payload?: object, headers?: object): Promise<Response> => { | ||
if (payload && headers) return request.get(service).set(headers).send(payload); | ||
else if (payload) return request.get(service).send(payload); | ||
else return request.get(service); | ||
export const httpGetCall = async (get: GetApiType): Promise<Response> => { | ||
let response = await request.get(get.service).set(get.headers ? get.headers : {}).send(get.payload ? get.payload : {}); | ||
logResponseToReport(get.context, response); | ||
return response; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { performance } from 'perf_hooks'; | ||
|
||
export const performanceTime = () => performance.now(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export enum endpoint { | ||
user = "users" | ||
user = "/users" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const RESPONSE = { | ||
TIME: 20000, | ||
CODE: 201, | ||
TYPE: "application/json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { endpoint } from "@Services/endpoints"; | ||
|
||
export type PostApiType = { | ||
service: endpoint, | ||
payload: object, | ||
headers?: object, | ||
context: Mocha.Context, | ||
} | ||
|
||
export type GetApiType = { | ||
service: endpoint, | ||
payload?: object, | ||
headers?: object, | ||
context: Mocha.Context | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { performanceTime } from '@Helper/utils'; | ||
import { expect, use } from 'chai'; | ||
import { Response } from 'supertest'; | ||
use(require('chai-json-schema')); | ||
|
||
export default class ResponseAssert { | ||
|
||
response: Response; | ||
|
||
constructor(response: Response) { | ||
this.response = response; | ||
} | ||
|
||
assertThat(response: Response) { | ||
this.response = response; | ||
return this; | ||
} | ||
|
||
timeIsLessThan(start: any, expectedTime: number) { | ||
expect(performanceTime() - start).to.be.lessThan(expectedTime); | ||
return this; | ||
} | ||
|
||
statusCodeIs(code: number) { | ||
expect(this.response.status).to.equal(code); | ||
return this; | ||
} | ||
|
||
typeIs(type: string) { | ||
expect(this.response.type).to.equal(type); | ||
return this; | ||
} | ||
|
||
schemaIs(schema: {}) { | ||
expect(this.response.body).to.be.jsonSchema(schema); | ||
return this; | ||
} | ||
|
||
toEqual(expected: string | number, actual: string | number) { | ||
expect(expected).equal(actual); | ||
return this; | ||
} | ||
|
||
isNotEmpty(value: string | number) { | ||
expect(value).is.not.empty; | ||
return this; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters