Skip to content

Commit

Permalink
add axios test and update util name
Browse files Browse the repository at this point in the history
  • Loading branch information
sadabnepal committed Jun 27, 2023
1 parent 559db5b commit 891ddea
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 25 deletions.
3 changes: 2 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ npm test [run all tests]
- GraphQL API Testing
- Request and Response log to report
- Multi environment support using dotenv
- Supertest, Mocha, TypesScript with mochawesome report
- Type support for better code intellisense
- Supertest and Axios api test boilerplate codes

## Folder structure:

Expand Down
83 changes: 75 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "export ENV=test&&mocha --timeout 30000 --reporter mochawesome --reporter-options reportDir=report --reporter-options charts=true --reporter-options code=false -r ts-node/register test/specs/*.ts"
"test": "export ENV=test&&mocha --timeout 30000 --reporter mochawesome --reporter-options reportDir=report --reporter-options charts=true --reporter-options code=false -r ts-node/register test/specs/**.ts",
"super:test": "export ENV=test&&mocha --timeout 30000 --reporter mochawesome --reporter-options reportDir=report --reporter-options charts=true --reporter-options code=false -r ts-node/register test/specs/supertest.spec.ts",
"axios:test": "export ENV=test&&mocha --timeout 30000 --reporter mochawesome --reporter-options reportDir=report --reporter-options charts=true --reporter-options code=false -r ts-node/register test/specs/axios.spec.ts"
},
"keywords": [],
"author": "",
Expand All @@ -15,18 +17,17 @@
"@types/chalk": "^2.2.0",
"@types/mocha": "^10.0.1",
"@types/mochawesome": "^6.2.1",
"@types/prettyjson": "^0.0.30",
"@types/supertest": "^2.0.12",
"axios": "^1.4.0",
"chai": "^4.3.7",
"chalk": "^4.1.2",
"dotenv": "^16.3.1",
"mocha": "^10.2.0",
"mochawesome": "^7.1.3",
"prettyjson": "^1.2.5",
"supertest": "^6.3.3",
"ts-node": "^10.9.1",
"typescript": "^5.1.3"
},
"dependencies": {
"@types/prettyjson": "^0.0.30",
"prettyjson": "^1.2.5"
}
}
}
19 changes: 18 additions & 1 deletion test/helper/apiUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import axios from 'axios';
import supertest from 'supertest';
import { logRequest, logResponse } from './logger';

export const graphQlAPI = async (url: string, schema: any, logs?: { logRequest?: boolean, logResponse?: boolean, mochaContext?: any }) => {
export const callGraphQlAPIUsingSuperTest = async (url: string, schema: any, logs?: { logRequest?: boolean, logResponse?: boolean, mochaContext?: Mocha.Context }) => {
const request = supertest(url);
const gqlData = { query: schema };

Expand All @@ -14,4 +15,20 @@ export const graphQlAPI = async (url: string, schema: any, logs?: { logRequest?:

if (logs?.logResponse) logResponse(response.statusCode, response.body, logs?.mochaContext)
return response;
}

export const callGraphQlAPIUsingAxios = async (url: string, schema: any, logs?: { logRequest?: boolean, logResponse?: boolean, mochaContext?: Mocha.Context }) => {
if (logs?.logRequest) logRequest(url, schema, logs?.mochaContext);

let config = {
method: 'post',
maxBodyLength: Infinity,
url: url,
headers: { 'Content-Type': 'application/json' },
data: { query: schema }
};
const response = await axios.request(config);

if (logs?.logResponse) logResponse(response.status, response.data, logs?.mochaContext)
return response;
}
51 changes: 51 additions & 0 deletions test/specs/axios.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { faker } from '@faker-js/faker';
import { expect } from 'chai';
import { URL } from '../env/manager';
import { callGraphQlAPIUsingAxios } from '../helper/apiUtils';
import { registerCustomerAccount } from '../payload/mutation';
import { getProductByName, getProducts } from '../payload/queries';
import { CustomerDetailsType } from '../types/customer';

describe('test graphql api using axios', () => {

describe('query data', function () {
it('should fetch all products', async function () {
const response = await callGraphQlAPIUsingAxios(URL, getProducts, { logRequest: true, logResponse: true, mochaContext: this });

expect(response.status).equal(200);
expect(response.data.data.products.items).to.have.length.greaterThan(0);
});

it('should fetch products by name', async function () {
let productName = "Laptop";
const response = await callGraphQlAPIUsingAxios(URL, getProductByName(productName), { logRequest: true, logResponse: true, mochaContext: this });

expect(response.status).equal(200)
expect(response.data.data.products.items).to.have.length(1)
});
})

describe('mutate data', function () {

it('should register user using mutation', async function () {
const firstName = faker.person.firstName();
const lastName = faker.person.lastName();

const customerData: CustomerDetailsType = {
emailAddress: faker.internet.email({ firstName: firstName, lastName: lastName }),
title: faker.person.prefix(),
firstName: firstName,
lastName: lastName,
phoneNumber: faker.phone.number(),
password: faker.internet.password()
}

const response = await callGraphQlAPIUsingAxios(URL, registerCustomerAccount(customerData), { logRequest: true, logResponse: true, mochaContext: this });

expect(response.status).equal(200);
expect(response.data.data.registerCustomerAccount.success).equal(true)
});

});

});
14 changes: 5 additions & 9 deletions test/specs/graphql.spec.ts → test/specs/supertest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ import { faker } from '@faker-js/faker';
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { URL } from '../env/manager';
import { graphQlAPI } from '../helper/apiUtils';
import { callGraphQlAPIUsingSuperTest } from '../helper/apiUtils';
import { registerCustomerAccount } from '../payload/mutation';
import { getProductByName, getProducts } from '../payload/queries';
import { CustomerDetailsType } from '../types/customer';

describe('test vendure shop api', function () {
describe('test graphql api using supertest', function () {

describe('query data', function () {

it('should fetch all products', async function () {
const response = await graphQlAPI(URL, getProducts, { logRequest: true, logResponse: true, mochaContext: this });
const response = await callGraphQlAPIUsingSuperTest(URL, getProducts, { logRequest: true, logResponse: true, mochaContext: this });

expect(response.statusCode).equal(200);
expect(response.body.data.products.items).to.have.length.greaterThan(0);
});

it('should fetch product using query parameter', async function () {
const productName = "Laptop";
const response = await graphQlAPI(URL, getProductByName(productName), { logRequest: true, logResponse: true, mochaContext: this });
const response = await callGraphQlAPIUsingSuperTest(URL, getProductByName(productName), { logRequest: true, logResponse: true, mochaContext: this });

expect(response.statusCode).equal(200);
expect(response.body.data.products.items).to.have.length(1);
Expand All @@ -45,16 +45,12 @@ describe('test vendure shop api', function () {
password: faker.internet.password()
}

const response = await graphQlAPI(URL, registerCustomerAccount(customerData), { logRequest: true, logResponse: true, mochaContext: this });
const response = await callGraphQlAPIUsingSuperTest(URL, registerCustomerAccount(customerData), { logRequest: true, logResponse: true, mochaContext: this });

expect(response.statusCode).equal(200);
expect(response.body.data.registerCustomerAccount.success).equal(true)
});

it.skip('should update customer', async function () {
//TODO: add code to update customer
});

});


Expand Down

0 comments on commit 891ddea

Please sign in to comment.