Best JavaScript code snippet using istanbul
object-utils.js
Source:object-utils.js
...70 * @method addDerivedInfo71 * @static72 * @param {Object} coverage the coverage object73 */74 function addDerivedInfo(coverage) {75 Object.keys(coverage).forEach(function (k) {76 addDerivedInfoForFile(coverage[k]);77 });78 }79 /**80 * removes line coverage information from all file coverage objects81 * @method removeDerivedInfo82 * @static83 * @param {Object} coverage the coverage object84 */85 function removeDerivedInfo(coverage) {86 Object.keys(coverage).forEach(function (k) {87 delete coverage[k].l;88 });89 }90 function percent(covered, total) {91 var tmp;92 if (total > 0) {93 tmp = 1000 * 100 * covered / total + 5;94 return Math.floor(tmp / 10) / 100;95 } else {96 return 100.00;97 }98 }99 function computeSimpleTotals(fileCoverage, property, mapProperty) {100 var stats = fileCoverage[property],101 map = mapProperty ? fileCoverage[mapProperty] : null,102 ret = { total: 0, covered: 0, skipped: 0 };103 Object.keys(stats).forEach(function (key) {104 var covered = !!stats[key],105 skipped = map && map[key].skip;106 ret.total += 1;107 if (covered || skipped) {108 ret.covered += 1;109 }110 if (!covered && skipped) {111 ret.skipped += 1;112 }113 });114 ret.pct = percent(ret.covered, ret.total);115 return ret;116 }117 function computeBranchTotals(fileCoverage) {118 var stats = fileCoverage.b,119 branchMap = fileCoverage.branchMap,120 ret = { total: 0, covered: 0, skipped: 0 };121 Object.keys(stats).forEach(function (key) {122 var branches = stats[key],123 map = branchMap[key],124 covered,125 skipped,126 i;127 for (i = 0; i < branches.length; i += 1) {128 covered = branches[i] > 0;129 skipped = map.locations && map.locations[i] && map.locations[i].skip;130 if (covered || skipped) {131 ret.covered += 1;132 }133 if (!covered && skipped) {134 ret.skipped += 1;135 }136 }137 ret.total += branches.length;138 });139 ret.pct = percent(ret.covered, ret.total);140 return ret;141 }142 /**143 * returns a blank summary metrics object. A metrics object has the following144 * format.145 *146 * {147 * lines: lineMetrics,148 * statements: statementMetrics,149 * functions: functionMetrics,150 * branches: branchMetrics151 * linesCovered: lineCoveredCount152 * }153 *154 * Each individual metric object looks as follows:155 *156 * {157 * total: n,158 * covered: m,159 * pct: percent160 * }161 *162 * @method blankSummary163 * @static164 * @return {Object} a blank metrics object165 */166 function blankSummary() {167 return {168 lines: {169 total: 0,170 covered: 0,171 skipped: 0,172 pct: 'Unknown'173 },174 statements: {175 total: 0,176 covered: 0,177 skipped: 0,178 pct: 'Unknown'179 },180 functions: {181 total: 0,182 covered: 0,183 skipped: 0,184 pct: 'Unknown'185 },186 branches: {187 total: 0,188 covered: 0,189 skipped: 0,190 pct: 'Unknown'191 },192 linesCovered: {}193 };194 }195 /**196 * returns the summary metrics given the coverage object for a single file. See `blankSummary()`197 * to understand the format of the returned object.198 *199 * @method summarizeFileCoverage200 * @static201 * @param {Object} fileCoverage the coverage object for a single file.202 * @return {Object} the summary metrics for the file203 */204 function summarizeFileCoverage(fileCoverage) {205 var ret = blankSummary();206 addDerivedInfoForFile(fileCoverage);207 ret.lines = computeSimpleTotals(fileCoverage, 'l');208 ret.functions = computeSimpleTotals(fileCoverage, 'f', 'fnMap');209 ret.statements = computeSimpleTotals(fileCoverage, 's', 'statementMap');210 ret.branches = computeBranchTotals(fileCoverage);211 ret.linesCovered = fileCoverage.l;212 return ret;213 }214 /**215 * merges two instances of file coverage objects *for the same file*216 * such that the execution counts are correct.217 *218 * @method mergeFileCoverage219 * @static220 * @param {Object} first the first file coverage object for a given file221 * @param {Object} second the second file coverage object for the same file222 * @return {Object} an object that is a result of merging the two. Note that223 * the input objects are not changed in any way.224 */225 function mergeFileCoverage(first, second) {226 var ret = JSON.parse(JSON.stringify(first)),227 i;228 delete ret.l; //remove derived info229 Object.keys(second.s).forEach(function (k) {230 ret.s[k] += second.s[k];231 });232 Object.keys(second.f).forEach(function (k) {233 ret.f[k] += second.f[k];234 });235 Object.keys(second.b).forEach(function (k) {236 var retArray = ret.b[k],237 secondArray = second.b[k];238 for (i = 0; i < retArray.length; i += 1) {239 retArray[i] += secondArray[i];240 }241 });242 return ret;243 }244 /**245 * merges multiple summary metrics objects by summing up the `totals` and246 * `covered` fields and recomputing the percentages. This function is generic247 * and can accept any number of arguments.248 *249 * @method mergeSummaryObjects250 * @static251 * @param {Object} summary... multiple summary metrics objects252 * @return {Object} the merged summary metrics253 */254 function mergeSummaryObjects() {255 var ret = blankSummary(),256 args = Array.prototype.slice.call(arguments),257 keys = ['lines', 'statements', 'branches', 'functions'],258 increment = function (obj) {259 if (obj) {260 keys.forEach(function (key) {261 ret[key].total += obj[key].total;262 ret[key].covered += obj[key].covered;263 ret[key].skipped += obj[key].skipped;264 });265 // keep track of all lines we have coverage for.266 Object.keys(obj.linesCovered).forEach(function (key) {267 if (!ret.linesCovered[key]) {268 ret.linesCovered[key] = obj.linesCovered[key];269 } else {270 ret.linesCovered[key] += obj.linesCovered[key];271 }272 });273 }274 };275 args.forEach(function (arg) {276 increment(arg);277 });278 keys.forEach(function (key) {279 ret[key].pct = percent(ret[key].covered, ret[key].total);280 });281 return ret;282 }283 /**284 * returns the coverage summary for a single coverage object. This is285 * wrapper over `summarizeFileCoverage` and `mergeSummaryObjects` for286 * the common case of a single coverage object287 * @method summarizeCoverage288 * @static289 * @param {Object} coverage the coverage object290 * @return {Object} summary coverage metrics across all files in the coverage object291 */292 function summarizeCoverage(coverage) {293 var fileSummary = [];294 Object.keys(coverage).forEach(function (key) {295 fileSummary.push(summarizeFileCoverage(coverage[key]));296 });297 return mergeSummaryObjects.apply(null, fileSummary);298 }299 /**300 * makes the coverage object generated by this library yuitest_coverage compatible.301 * Note that this transformation is lossy since the returned object will not have302 * statement and branch coverage.303 *304 * @method toYUICoverage305 * @static306 * @param {Object} coverage The `istanbul` coverage object307 * @return {Object} a coverage object in `yuitest_coverage` format.308 */309 function toYUICoverage(coverage) {310 var ret = {};311 addDerivedInfo(coverage);312 Object.keys(coverage).forEach(function (k) {313 var fileCoverage = coverage[k],314 lines = fileCoverage.l,315 functions = fileCoverage.f,316 fnMap = fileCoverage.fnMap,317 o;318 o = ret[k] = {319 lines: {},320 calledLines: 0,321 coveredLines: 0,322 functions: {},323 calledFunctions: 0,324 coveredFunctions: 0325 };...
istanbul-utils.js
Source:istanbul-utils.js
...68 * @method addDerivedInfo69 * @static70 * @param {Object} coverage the coverage object71 */72 function addDerivedInfo(coverage) {73 Object.keys(coverage).forEach(function (k) {74 addDerivedInfoForFile(coverage[k]);75 });76 }77 /**78 * removes line coverage information from all file coverage objects79 * @method removeDerivedInfo80 * @static81 * @param {Object} coverage the coverage object82 */83 function removeDerivedInfo(coverage) {84 Object.keys(coverage).forEach(function (k) {85 delete coverage[k].l;86 });87 }88 function percent(covered, total) {89 var tmp;90 if (total > 0) {91 tmp = 1000 * 100 * covered / total + 5;92 return Math.floor(tmp / 10) / 100;93 } else {94 return 100.00;95 }96 }97 function computeSimpleTotals(fileCoverage, property) {98 var stats = fileCoverage[property],99 ret = { total: 0, covered: 0 };100 Object.keys(stats).forEach(function (key) {101 ret.total += 1;102 if (stats[key]) {103 ret.covered += 1;104 }105 });106 ret.pct = percent(ret.covered, ret.total);107 return ret;108 }109 function computeBranchTotals(fileCoverage) {110 var stats = fileCoverage.b,111 ret = { total: 0, covered: 0 };112 Object.keys(stats).forEach(function (key) {113 var branches = stats[key],114 covered = branches.filter(function (num) { return num > 0; });115 ret.total += branches.length;116 ret.covered += covered.length;117 });118 ret.pct = percent(ret.covered, ret.total);119 return ret;120 }121 /**122 * returns a blank summary metrics object. A metrics object has the following123 * format.124 *125 * {126 * lines: lineMetrics,127 * statements: statementMetrics,128 * functions: functionMetrics,129 * branches: branchMetrics130 * }131 *132 * Each individual metric object looks as follows:133 *134 * {135 * total: n,136 * covered: m,137 * pct: percent138 * }139 *140 * @method blankSummary141 * @static142 * @return {Object} a blank metrics object143 */144 function blankSummary() {145 return {146 lines: {147 total: 0,148 covered: 0,149 pct: 'Unknown'150 },151 statements: {152 total: 0,153 covered: 0,154 pct: 'Unknown'155 },156 functions: {157 total: 0,158 covered: 0,159 pct: 'Unknown'160 },161 branches: {162 total: 0,163 covered: 0,164 pct: 'Unknown'165 }166 };167 }168 /**169 * returns the summary metrics given the coverage object for a single file. See `blankSummary()`170 * to understand the format of the returned object.171 *172 * @method summarizeFileCoverage173 * @static174 * @param {Object} fileCoverage the coverage object for a single file.175 * @return {Object} the summary metrics for the file176 */177 function summarizeFileCoverage(fileCoverage) {178 var ret = blankSummary();179 addDerivedInfoForFile(fileCoverage);180 ret.lines = computeSimpleTotals(fileCoverage, 'l');181 ret.functions = computeSimpleTotals(fileCoverage, 'f');182 ret.statements = computeSimpleTotals(fileCoverage, 's');183 ret.branches = computeBranchTotals(fileCoverage);184 return ret;185 }186 /**187 * merges two instances of file coverage objects *for the same file*188 * such that the execution counts are correct.189 *190 * @method mergeFileCoverage191 * @static192 * @param {Object} first the first file coverage object for a given file193 * @param {Object} second the second file coverage object for the same file194 * @return {Object} an object that is a result of merging the two. Note that195 * the input objects are not changed in any way.196 */197 function mergeFileCoverage(first, second) {198 var ret = JSON.parse(JSON.stringify(first)),199 i;200 delete ret.l; //remove derived info201 Object.keys(second.s).forEach(function (k) {202 ret.s[k] += second.s[k];203 });204 Object.keys(second.f).forEach(function (k) {205 ret.f[k] += second.f[k];206 });207 Object.keys(second.b).forEach(function (k) {208 var retArray = ret.b[k],209 secondArray = second.b[k];210 for (i = 0; i < retArray.length; i += 1) {211 retArray[i] += secondArray[i];212 }213 });214 return ret;215 }216 /**217 * merges multiple summary metrics objects by summing up the `totals` and218 * `covered` fields and recomputing the percentages. This function is generic219 * and can accept any number of arguments.220 *221 * @method mergeSummaryObjects222 * @static223 * @param {Object} summary... multiple summary metrics objects224 * @return {Object} the merged summary metrics225 */226 function mergeSummaryObjects() {227 var ret = blankSummary(),228 args = Array.prototype.slice.call(arguments),229 keys = ['lines', 'statements', 'branches', 'functions'],230 increment = function (obj) {231 if (obj) {232 keys.forEach(function (key) {233 ret[key].total += obj[key].total;234 ret[key].covered += obj[key].covered;235 });236 }237 };238 args.forEach(function (arg) {239 increment(arg);240 });241 keys.forEach(function (key) {242 ret[key].pct = percent(ret[key].covered, ret[key].total);243 });244 return ret;245 }246 /**247 * returns the coverage summary for a single coverage object. This is248 * wrapper over `summarizeFileCoverage` and `mergeSummaryObjects` for249 * the common case of a single coverage object250 * @method summarizeCoverage251 * @static252 * @param {Object} coverage the coverage object253 * @return {Object} summary coverage metrics across all files in the coverage object254 */255 function summarizeCoverage(coverage) {256 var fileSummary = [];257 Object.keys(coverage).forEach(function (key) {258 fileSummary.push(summarizeFileCoverage(coverage[key]));259 });260 return mergeSummaryObjects.apply(null, fileSummary);261 }262 /**263 * makes the coverage object generated by this library yuitest_coverage compatible.264 * Note that this transformation is lossy since the returned object will not have265 * statement and branch coverage.266 *267 * @method toYUICoverage268 * @static269 * @param {Object} coverage The `istanbul` coverage object270 * @return {Object} a coverage object in `yuitest_coverage` format.271 */272 function toYUICoverage(coverage) {273 var ret = {};274 addDerivedInfo(coverage);275 Object.keys(coverage).forEach(function (k) {276 var fileCoverage = coverage[k],277 lines = fileCoverage.l,278 functions = fileCoverage.f,279 fnMap = fileCoverage.fnMap,280 o;281 o = ret[k] = {282 lines: {},283 calledLines: 0,284 coveredLines: 0,285 functions: {},286 calledFunctions: 0,287 coveredFunctions: 0288 };...
day2.js
Source:day2.js
...45 );46 console.log(message, validPasswords.length);47};48validatePasswordPart1(49 addDerivedInfo(parseInput(part1TestInputFail)),50 `Test failure conditions for part 1 expect 0 to be valid, actual number valid is`51);52validatePasswordPart1(53 addDerivedInfo(parseInput(part1TestInputPass)),54 `Test success conditions for part 1 expect 2 to be valid, actual number valid is`55);56validatePasswordPart1(57 addDerivedInfo(parseInput(inputString)),58 "Part 1, the number of valid passwords is"59);60validatePasswordPart2(61 addDerivedInfo(parseInput(part2TestInputFail)),62 `Test failure conditions for part 2 expect 0 to be valid, actual number valid is`63);64validatePasswordPart2(65 addDerivedInfo(parseInput(part2TestInputPass)),66 `Test success conditions for part 2 expect 2 to be valid, actual number valid is`67);68validatePasswordPart2(69 addDerivedInfo(parseInput(inputString)),70 "Part 2, the number of valid passwords is"...
Using AI Code Generation
1var istanbul = require('istanbul');2var collector = new istanbul.Collector();3var reporter = new istanbul.Reporter();4var sync = false;5collector.add(__coverage__);6collector.addDerivedInfo();7reporter.add('text');8reporter.addAll(['lcov', 'json']);9reporter.write(collector, sync, function () {10 console.log('All reports generated');11});
Using AI Code Generation
1var istanbul = require('istanbul');2var collector = new istanbul.Collector();3var reporter = new istanbul.Reporter();4var sync = false;5collector.add({6 'path/to/file1.js': {7 s: { 1: 1, 2: 1 },8 b: { 1: [1, 0] },9 f: { 1: 1, 2: 1 },10 fnMap: {11 1: { name: 'bar', line: 1, loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 9 } } },12 2: { name: '(anonymous_1)', line: 1, loc: { start: { line: 1, column: 14 }, end: { line: 1, column: 15 } } }13 },14 statementMap: {15 1: { start: { line: 1, column: 0 }, end: { line: 1, column: 9 } },16 2: { start: { line: 1, column: 14 }, end: { line: 1, column: 15 } }17 },18 branchMap: {19 1: { line: 1, type: 'binary-expr', locations: [{ start: { line: 1, column: 0 }, end: { line: 1, column: 9 } }, { start: { line: 1, column: 14 }, end: { line: 1, column: 15 } }] }20 }21 }22});23collector.add({24 'path/to/file2.js': {25 s: { 1: 1, 2: 1 },26 b: { 1: [1, 0] },27 f: { 1: 1, 2: 1 },28 fnMap: {29 1: { name: 'bar', line: 1, loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 9 } } },30 2: { name: '(anonymous_1)', line: 1,
Using AI Code Generation
1var istanbul = require('istanbul');2var collector = new istanbul.Collector();3collector.add(__coverage__);4var reporter = new istanbul.Reporter();5var sync = false;6reporter.addAll([ 'lcov', 'text-summary' ]);7reporter.write(collector, sync, function () { console.log('done'); });
Using AI Code Generation
1var istanbul = require('istanbul');2var collector = new istanbul.Collector();3collector.add(__coverage__);4var reporter = new istanbul.Reporter();5reporter.addAll(['lcov', 'json', 'text', 'clover']);6reporter.write(collector, true, function () {7 console.log('All reports generated');8});
Using AI Code Generation
1var istanbul = require('istanbul');2var instrumenter = new istanbul.Instrumenter();3var fs = require('fs');4var code = fs.readFileSync('test.js', 'utf8');5var instrumentedCode = instrumenter.instrumentSync(code, 'test.js');6fs.writeFileSync('test_instrumented.js', instrumentedCode, 'utf8');7var collector = new istanbul.Collector();8var reporter = new istanbul.Reporter();9var sync = false;10var tree = istanbul.utils.createTree();11var map = istanbul.utils.createCoverageMap();12var coverage = JSON.parse(fs.readFileSync('coverage.json', 'utf8'));13var derivedInfo = istanbul.utils.mergeFileCoverage(coverage);14map.addFileCoverage(derivedInfo);15tree.merge(map);16reporter.addAll(['text', 'lcov', 'json', 'html']);17reporter.write(tree, sync, function () { console.log('done'); });18 throw ex;19 at Array.forEach (native)20 at annotateBranches (C:\Users\abc\Documents\GitHub\test-istanbul\node_modules\istanbul\lib\report\html.js:256:27)21 at HtmlReport.Report.mix.writeDetailPage (C:\Users\abc\Documents\GitHub\test-istanbul\node_modules\istanbul\lib\report\html.js:427:9)22 at SyncFileWriter.extend.writeFile (C:\Users\abc\Documents\GitHub\test-istanbul\node_modules\istanbul\lib\util\file-writer.js:57:9)23 at FileWriter.extend.writeFile (C:\Users\abc\Documents\GitHub\test-istanbul\node_modules\istanbul\lib\util\file
Using AI Code Generation
1var istanbulMiddleware = require('istanbul-middleware');2var addDerivedInfo = istanbulMiddleware.addDerivedInfo;3var coverage = require('./coverage.json');4var derivedCoverage = addDerivedInfo(coverage);5console.log(derivedCoverage);6#### addDerivedInfo(coverage, options)
Using AI Code Generation
1const instrumenter = require('istanbul-lib-instrument').createInstrumenter();2const fs = require('fs');3const code = fs.readFileSync('./test.js', 'utf-8');4const ast = instrumenter.parse(code);5instrumenter.addDerivedInfo(ast, code);6console.log(JSON.stringify(ast, null, 2));
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!