Skip to content

Instantly share code, notes, and snippets.

@niteshpsit1
Last active December 30, 2016 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niteshpsit1/14079bd67408b1c4a301cfcd22c29034 to your computer and use it in GitHub Desktop.
Save niteshpsit1/14079bd67408b1c4a301cfcd22c29034 to your computer and use it in GitHub Desktop.
var fs = require('fs')
var path = require('path')
/**
* function To generate the descption for test
* @param { String } filename name of api for which we need to generate test cases
* @returns { String } template for test cases
**/
function getTestTemplate(endpoint, httpMethod = 'GET') {
endpoint = JSON.stringify(endpoint)
return `var chai = require('chai');
var expect = chai.expect;
var assert = chai.assert;
var request = require('superagent');
describe('${endpoint} function', function() {
let response;
let body;
before((done)=>{
request
${httpMethods[httpMethod]}(${endpoint})
.end(function(err, res){
response = res;
body = res.body.result;
done();
})
})
${getTestDesc('isEqual', 200, 'expect(response.statusCode).to.equal(200);')}`
}
/**
* http method
**/
let httpMethods = {
get: '.get',
GET: '.get',
post: '.post',
POST: '.post',
put: '.put',
PUT: '.put',
delete: '.detele',
DELETE: '.detele',
all: '.all',
ALL: '.all'
}
/**
* function generate the defferent - 2 cases for api test and returns template
* @param { String } type of cases to compare
* @param { String } word for compare
* @returns { String } template for case
**/
function getCaseTemp(type, word) {
return `
it('${testDes(type, word)}', () => {`
}
/**
* @param { String } path is the path of file to write the contents
* @param { String } content is the mail contents to write in the file
**/
function writeToFile(path, content) {
path = __dirname + '/' + path
fs.writeFile(path, content, 'utf-8');
}
/**
* @param { String } type test case to be done
* @param { String } word for display in descption
* @returns { String } case template
**/
function testDes(type, word = '') {
let message = {
isNotEmpty: 'should not be empty',
isEmpty: 'should be empty',
haveAnyKeys: 'should have any keys ' + word,
haveAllKeys: 'should have all keys ' + word,
typeOf: 'should be typeOf ' + word,
haveKey: 'should have key ' + word,
arrayInclude: 'array should include ' + word,
stringContain: 'String should contain ' + word,
truthy: 'should be truthy value',
notTruthy: 'should not be truthy value',
isTrue: 'should be true',
isNotTrue: 'should not be true',
isFalse: 'should be false',
isNotFalse: 'should not be false',
isNull: 'should be null',
isNotNull: 'should not be null',
isUndefined: 'should be undefined',
isNotUndefined: 'should not be undefined',
isEqual: 'should be equal to ' + word,
isNotEqual: 'should not be equal',
isDeepEqual: 'should be deep equal',
instanceof: 'should be instanceof ',
isExist: 'should not be null and undefined',
Object: 'should be an Object',
Array: 'should be an Array',
String: 'should be an String',
Number: 'should be a Number'
}
return message[type]
}
/**
* An Object to just return the expect value or function to use in compare
**/
let expectType = {
isNotEmpty: '.not.to.be.empty',
isEmpty: '.to.be.empty',
haveAnyKeys: '.to.have.any.keys',
haveAllKeys: '.to.have.all.keys',
haveKey: '.to.include.keys',
typeOf: '.to.be.an',
arrayInclude: '.to.include',
stringContain: '.to.contain',
truthy: '.to.be.ok',
notTruthy: '.to.not.be.ok',
isTrue: '.to.be.true',
isNotTrue: '.to.not.be.true',
isFalse: '.to.be.false',
isNotFalse: '.to.not.be.false',
isNull: '.to.be.null',
isNotNull: '.to.not.be.null',
isUndefined: '.to.be.undefined',
isNotUndefined: '.to.not.be.undefined',
isEqual: '.to.equal',
isNotEqual: '.to.not.equal',
isDeepEqual: '.to.deep.equal',
instanceof: '.to.be.an.instanceof',
isExist: '.to.exist'
}
/**
* assert libraty
**/
let assertLib = {
Object: 'assert.isObject(response, "result should is an object");',
Array: 'assert.isArray(response, "result should is an array");',
String: 'assert.isString(response, "result should is an string");',
Number: 'assert.isNumber(response, "result should is a number");'
}
/**
* @param { Object/Array/String/Number/Null/Undefined } value that we get to compare from api
* @param { String } type case to compare
* @param { String } compareTo value with which we compare our response
* @param { String} Simple whole template of test case for api
**/
function getTestDesc(type = 'isNotEmpty', compareTo, test = '') {
compareTo = compareTo ? JSON.stringify(compareTo) : compareTo
let descption = `${getCaseTemp(type, compareTo)}`
test = test ? test : ( assertLib[type] !== undefined ? assertLib[type] : (compareTo ? `expect(body)${expectType[type]}(${compareTo});` : `expect(body)${expectType[type]};`) )
return `
${descption}
${test}
})`
}
/**
*
**/
function genTestCases(endpoint, httpMethod, expectedRes, fileNameToWriteTC) {
let testContent = getTestTemplate(endpoint, httpMethod)
/**
* common testcases for all apis irrespective of api's response
*/
testContent += getTestDesc('isNotNull')
testContent += getTestDesc('isNotUndefined')
/**
* check response is Array or String
**/
let resType = (expectedRes instanceof Array) === true ? 'Array' : 'Object';
testContent += getTestDesc(resType)
if (resType === 'Object') {
let objectKeys = Object.keys(expectedRes)
testContent += getTestDesc('haveAllKeys', objectKeys)
}
else if (resType === 'Array') {
}
testContent += `
});`
writeToFile(fileNameToWriteTC, testContent);
}
let expectedRes = {
"_id": "581c626745fa80374a000001",
"name": "Cat",
"cost": 40
}
genTestCases('http://localhost:8080/pets/petis', 'get', expectedRes, 'test.js')
console.log("getCaseTemp('isEqual', 200)",getCaseTemp('haveAllKeys', ['200']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment