Skip to content

Commit

Permalink
Framework update | chainable assertion
Browse files Browse the repository at this point in the history
Chainable assertion
response parameters as object and custom type
  • Loading branch information
sadabnepal committed May 21, 2022
1 parent fb28a00 commit 66ba599
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/config/supertest.ts
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");
20 changes: 11 additions & 9 deletions src/helper/apicall.ts
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;
}
3 changes: 3 additions & 0 deletions src/helper/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { performance } from 'perf_hooks';

export const performanceTime = () => performance.now();
2 changes: 1 addition & 1 deletion src/services/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export enum endpoint {
user = "users"
user = "/users"
}
5 changes: 5 additions & 0 deletions src/static/Constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const RESPONSE = {
TIME: 20000,
CODE: 201,
TYPE: "application/json"
}
38 changes: 20 additions & 18 deletions src/test/Users.spec.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
import { postCall } from '@Helper/apicall';
import { logResponseToReport } from '@Helper/logger';
import { httpPostCall } from '@Helper/apicall';
import { performanceTime } from '@Helper/utils';
import users from "@Resources/testdata.json";
import userschema from "@Schema/user.json";
import { endpoint } from '@Services/endpoints';
import { RESPONSE } from '@Static/Constants';
import { UserPayloadType, UserResponseType } from '@Types/user';
import { expect, use } from 'chai';
import { performance } from 'perf_hooks';
use(require('chai-json-schema'));
import ResponseAssert from "@Validation/ResponseAssert";

describe('Test ReqRes APIs', () => {

users.forEach((user: UserPayloadType) => {
it(`should validate create user ${user.name}`, async function () {
const startTime = performance.now();
const response = await postCall(endpoint.user, user);
const startTime = performanceTime()
const response = await httpPostCall({
service: endpoint.user,
payload: user,
context: this
});

expect(performance.now() - startTime).to.be.lessThan(2000);
expect(response.statusCode).to.equal(201);
expect(response.type).to.equal("application/json");
const { name, job, id, createdAt }: UserResponseType = response.body;

logResponseToReport(this, response);
const responseBody: UserResponseType = response.body;
expect(responseBody).to.be.jsonSchema(userschema)
new ResponseAssert(response)
.timeIsLessThan(startTime, RESPONSE.TIME)
.statusCodeIs(RESPONSE.CODE)
.typeIs(RESPONSE.TYPE)
.schemaIs(userschema)
.toEqual(name, user.name)
.toEqual(job, user.job)
.isNotEmpty(id)
.isNotEmpty(createdAt);

const { name, job, id, createdAt } = responseBody;
expect(name).equal(user.name);
expect(job).equal(user.job);
expect(id).is.not.empty;
expect(createdAt).is.not.empty;
})
})

Expand Down
15 changes: 15 additions & 0 deletions src/types/http.d.ts
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
}
49 changes: 49 additions & 0 deletions src/validations/ResponseAssert.ts
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;
}

}
9 changes: 9 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"esModuleInterop": true,
"resolveJsonModule": true,
"paths": {
"@Config/*": [
"./src/config/*"
],
"@Resources/*": [
"./src/resources/*"
],
Expand All @@ -18,6 +21,12 @@
],
"@Schema/*": [
"./src/schema/*"
],
"@Validation/*": [
"./src/validations/*"
],
"@Static/*": [
"./src/static/*"
]
}
}
Expand Down

0 comments on commit 66ba599

Please sign in to comment.