Skip to content

Instantly share code, notes, and snippets.

@balindersingh
Last active December 29, 2019 20:46
Show Gist options
  • Save balindersingh/bc7db2bbe2bb5152061efdae2cb3dd9f to your computer and use it in GitHub Desktop.
Save balindersingh/bc7db2bbe2bb5152061efdae2cb3dd9f to your computer and use it in GitHub Desktop.
Sample module for test coverage article
var _MyModule = {};
// public function
_MyModule.Add = function (a, b) {
// your code here
return parseFloat(a) + parseFloat(b);
};
_MyModule.Subtract = function (a, b) {
// your code here
return parseFloat(a) - parseFloat(b);
};
module.exports = _MyModule;
var assert = require('assert');
var myModule = require('./MyModule.js');
describe('MyModule Library functions tests', function(){
var additionArray = [{a:23,b:34,addition:57,subtraction:-11},{a:5,b:1,addition:6,subtraction:4},{a:31,b:10,addition:41,subtraction:21}];
it('Testing Add function from MyModule', function(){
additionArray.forEach(function(arrayItem){
var resultOfAddition = myModule.Add(arrayItem.a,arrayItem.b);
assert.equal(resultOfAddition, arrayItem.addition, 'result of addition should be '+arrayItem.addition);
});
});
it('Testing Subtract function from MyModule', function(){
additionArray.forEach(function(arrayItem){
var resultOfAddition = myModule.Subtract(arrayItem.a,arrayItem.b);
assert.equal(resultOfAddition, arrayItem.subtraction, 'result of subtraction should be '+arrayItem.subtraction);
});
});
});
{
"name": "mymodule",
"version": "1.0.0",
"description": "testing code coverage",
"main": "MyModule.js",
"scripts": {
"test": "mocha \"./**_test.js\"",
"test:coverage": "npm run cover:unit && npm run cover:report",
"test:coveragehtml": "npm run cover:unit && npm run cover:reporthtml",
"cover:unit": "nyc --silent npm run test",
"cover:report": "nyc report --reporter=lcov --reporter=text --report-dir='./jscoverage'",
"cover:reporthtml": "nyc report --reporter=html --report-dir='./jscoverage'"
},
"nyc": {
"exclude": ["**/*_test.js"]
},
"author": "Balinder Singh",
"license": "ISC",
"devDependencies": {
"mocha": "^6.2.2",
"nyc": "^15.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment