Best JavaScript code snippet using istanbul
test-statement-with-hints.js
Source:test-statement-with-hints.js
...14 cb();15 },16 "should cover line and one branch": function (test) {17 verifier.verify(test, [ 10 ], 10, { lines: { 2: 1, 3: 1 }, branches: { 1: [1, 0 ]}, functions: {}, statements: { 1: 1, 2: 1 } });18 var cov = verifier.getFileCoverage();19 test.equal(true, cov.statementMap[1].skip);20 test.equal(true, cov.branchMap[1].locations[0].skip);21 test.equal(true, cov.branchMap[1].locations[1].skip);22 test.done();23 }24 },25 "with a function declaration": {26 setUp: function (cb) {27 code = [28 '/* istanbul ignore next */',29 'function foo(x) { return x; }',30 'output = args[0];'31 ];32 verifier = helper.verifier(__filename, code);33 cb();34 },35 "should not cover function call": function (test) {36 verifier.verify(test, [ 10 ], 10, { lines: { 2: 1, 3: 1 }, branches: {}, functions: { 1: 0 }, statements: { 1: 1, 2: 0, 3: 1 } });37 var cov = verifier.getFileCoverage();38 test.equal(true, cov.statementMap[1].skip);39 test.equal(true, cov.statementMap[2].skip);40 test.equal(true, cov.fnMap[1].skip);41 test.done();42 }43 },44 "with a function expression": {45 setUp: function (cb) {46 code = [47 '/* istanbul ignore next */',48 '(function () { output = args[0]; })();'49 ];50 verifier = helper.verifier(__filename, code);51 cb();52 },53 "should cover function call": function (test) {54 verifier.verify(test, [ 10 ], 10, { lines: { 2: 1 }, branches: {}, functions: { 1: 1 }, statements: { 1: 1, 2: 1 } });55 var cov = verifier.getFileCoverage();56 test.equal(true, cov.statementMap[1].skip);57 test.equal(true, cov.statementMap[2].skip);58 test.equal(true, cov.fnMap[1].skip);59 test.done();60 }61 },62 "with a disabled switch statement": {63 setUp: function (cb) {64 code = [65 '/* istanbul ignore next */',66 'switch (args[0]) {',67 'case "1": output = 2; break;',68 'default: output = 1;',69 '}'70 ];71 verifier = helper.verifier(__filename, code);72 cb();73 },74 "should ignore all branches": function (test) {75 verifier.verify(test, [ "1" ], 2, { lines: { 2: 1, 3: 1, 4: 1 }, branches: { 1: [ 1, 0 ]},76 functions: {}, statements: { 1: 1, 2: 1, 3: 1, 4: 0 } });77 var cov = verifier.getFileCoverage();78 test.equal(true, cov.statementMap[1].skip);79 test.equal(true, cov.statementMap[2].skip);80 test.equal(true, cov.statementMap[3].skip);81 test.equal(true, cov.statementMap[4].skip);82 test.equal(true, cov.branchMap[1].locations[0].skip);83 test.equal(true, cov.branchMap[1].locations[1].skip);84 test.done();85 }86 },87 "with a disabled case statement": {88 setUp: function (cb) {89 code = [90 'switch (args[0]) {',91 '/* istanbul ignore next */',92 'case "1": output = 2; break;',93 'default: output = 1;',94 '}'95 ];96 verifier = helper.verifier(__filename, code);97 cb();98 },99 "should ignore specific case": function (test) {100 verifier.verify(test, [ "2" ], 1, { lines: { 1: 1, 3: 1, 4: 1 }, branches: { 1: [ 0, 1 ]},101 functions: {}, statements: { 1: 1, 2: 0, 3: 0, 4: 1 } });102 var cov = verifier.getFileCoverage();103 test.equal(true, cov.branchMap[1].locations[0].skip);104 test.equal(true, cov.statementMap[2].skip);105 test.equal(true, cov.statementMap[3].skip);106 test.done();107 }108 },109 "with disabled conditional statement": {110 setUp: function (cb) {111 code = [112 '/* istanbul ignore next */',113 'output = args[0] === 1 ? 1: 0;'114 ];115 verifier = helper.verifier(__filename, code);116 cb();117 },118 "should ignore conditions": function (test) {119 verifier.verify(test, [ 2 ], 0, { lines: { 2: 1 }, branches: { 1: [ 0, 1 ]},120 functions: {}, statements: { 1: 1 } });121 var cov = verifier.getFileCoverage();122 test.equal(true, cov.branchMap[1].locations[0].skip);123 test.equal(true, cov.branchMap[1].locations[1].skip);124 test.equal(true, cov.statementMap[1].skip);125 test.done();126 }127 },128 "with disabled condition": {129 setUp: function (cb) {130 code = [131 'output = args[0] === 1 ? /* istanbul ignore next */ 1 : 0;'132 ];133 verifier = helper.verifier(__filename, code);134 cb();135 },136 "should ignore conditions": function (test) {137 verifier.verify(test, [ 2 ], 0, { lines: { 1: 1 }, branches: { 1: [ 0, 1 ]},138 functions: {}, statements: { 1: 1 } });139 var cov = verifier.getFileCoverage();140 test.equal(true, cov.branchMap[1].locations[0].skip);141 test.equal(undefined, cov.branchMap[1].locations[1].skip);142 test.equal(undefined, cov.statementMap[1].skip);143 test.done();144 }145 },146 "with a simple logical expression": {147 setUp: function (cb) {148 code = [149 'if (args[0] === 1 || /* istanbul ignore next */ args[0] === 2 ) {',150 ' output = args[0] + 10;',151 '} else {',152 ' output = 20;',153 '}'154 ];155 verifier = helper.verifier(__filename, code);156 cb();157 },158 "should ignore conditions": function (test) {159 verifier.verify(test, [ 1 ], 11, { lines: { 1: 1, 2: 1, 4: 0 }, branches: { 1: [ 1, 0 ], 2: [ 1, 0 ] },160 functions: {}, statements: { 1: 1, 2: 1, 3: 0 } });161 var cov = verifier.getFileCoverage();162 test.equal(true, cov.branchMap[2].locations[1].skip);163 test.done();164 }165 },166 "with a slightly complicated logical expression": {167 setUp: function (cb) {168 code = [169 'if (args[0] === 1 || /* istanbul ignore next */ (args[0] === 2 || args[0] === 3)) {',170 ' output = args[0] + 10;',171 '} else {',172 ' output = 20;',173 '}'174 ];175 verifier = helper.verifier(__filename, code);176 cb();177 },178 "should ignore conditions": function (test) {179 verifier.verify(test, [ 1 ], 11, { lines: { 1: 1, 2: 1, 4: 0 }, branches: { 1: [ 1, 0 ], 2: [ 1, 0, 0 ] },180 functions: {}, statements: { 1: 1, 2: 1, 3: 0 } });181 var cov = verifier.getFileCoverage();182 test.equal(undefined, cov.branchMap[2].locations[0].skip);183 test.equal(true, cov.branchMap[2].locations[1].skip);184 test.equal(true, cov.branchMap[2].locations[2].skip);185 test.done();186 }187 },188 "with a complicated logical expression involving implied operator precedence": {189 setUp: function (cb) {190 code = [191 'if (args[0] === 1 || /* istanbul ignore next */ args[0] === 2 && args[1] === 2) {',192 ' output = args[0] + 10;',193 '} else {',194 ' output = 20;',195 '}'196 ];197 verifier = helper.verifier(__filename, code);198 cb();199 },200 "should ignore conditions": function (test) {201 verifier.verify(test, [ 1, 1 ], 11, { lines: { 1: 1, 2: 1, 4: 0 }, branches: { 1: [ 1, 0 ], 2: [ 1, 0, 0 ] },202 functions: {}, statements: { 1: 1, 2: 1, 3: 0 } });203 var cov = verifier.getFileCoverage();204 test.equal(undefined, cov.branchMap[2].locations[0].skip);205 test.equal(true, cov.branchMap[2].locations[1].skip);206 test.equal(true, cov.branchMap[2].locations[2].skip);207 test.done();208 }209 }...
test-if-with-hints.js
Source:test-if-with-hints.js
...18 "should cover then path": function (test) {19 code[1] = '/* istanbul ignore else */';20 verifier = helper.verifier(__filename, code);21 verifier.verify(test, [ 20, 10 ], 20, { lines: { 1: 1, 3: 1, 4: 1 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1 } });22 var cov = verifier.getFileCoverage();23 test.equal(true, cov.branchMap[1].locations[1].skip);24 test.done();25 },26 "should cover else path": function (test) {27 code[1] = '/* istanbul ignore if */';28 verifier = helper.verifier(__filename, code);29 verifier.verify(test, [ 10, 20 ], -1, { lines: { 1: 1, 3: 1, 4: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });30 var cov = verifier.getFileCoverage();31 test.equal(true, cov.branchMap[1].locations[0].skip);32 test.equal(true, cov.statementMap[3].skip);33 test.done();34 }35 },36 "as a block": {37 setUp: function (cb) {38 code = [39 'output = -1;',40 '/* hint */',41 'if (args[0] > args [1]) {',42 ' output = args[0];',43 '}'44 ];45 cb();46 },47 "should cover then path": function (test) {48 code[1] = '/* istanbul ignore else */';49 verifier = helper.verifier(__filename, code);50 verifier.verify(test, [ 20, 10 ], 20, { lines: { 1: 1, 3: 1, 4: 1 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1 } });51 var cov = verifier.getFileCoverage();52 test.equal(true, cov.branchMap[1].locations[1].skip);53 test.done();54 },55 "should cover else path": function (test) {56 code[1] = '/* istanbul ignore if */';57 verifier = helper.verifier(__filename, code);58 verifier.verify(test, [ 10, 20 ], -1, { lines: { 1: 1, 3: 1, 4: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });59 var cov = verifier.getFileCoverage();60 test.equal(true, cov.branchMap[1].locations[0].skip);61 test.equal(true, cov.statementMap[3].skip);62 test.done();63 }64 },65 "on a single line": {66 "as statement": {67 setUp: function (cb) {68 code = [69 'output = -1;',70 '/* hint */',71 'if (args[0] > args [1]) output = args[0];'72 ];73 cb();74 },75 "should cover then path": function (test) {76 code[1] = '/* istanbul ignore else */';77 verifier = helper.verifier(__filename, code);78 verifier.verify(test, [ 20, 10 ], 20, { lines: { 1: 1, 3: 1 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1 } });79 var cov = verifier.getFileCoverage();80 test.equal(true, cov.branchMap[1].locations[1].skip);81 test.done();82 },83 "should cover else path": function (test) {84 code[1] = '/* istanbul ignore if */';85 verifier = helper.verifier(__filename, code);86 verifier.verify(test, [ 10, 20 ], -1, { lines: { 1: 1, 3: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });87 var cov = verifier.getFileCoverage();88 test.equal(true, cov.branchMap[1].locations[0].skip);89 test.equal(true, cov.statementMap[3].skip);90 test.done();91 }92 },93 "as block": {94 setUp: function (cb) {95 code = [96 'output = -1;',97 '/* hint */',98 'if (args[0] > args [1]) { output = args[0]; }'99 ];100 cb();101 },102 "should cover then path": function (test) {103 code[1] = '/* istanbul ignore else */';104 verifier = helper.verifier(__filename, code);105 verifier.verify(test, [ 20, 10 ], 20, { lines: { 1: 1, 3: 1 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1 } });106 var cov = verifier.getFileCoverage();107 test.equal(true, cov.branchMap[1].locations[1].skip);108 test.done();109 },110 "should cover else path": function (test) {111 code[1] = '/* istanbul ignore if */';112 verifier = helper.verifier(__filename, code);113 verifier.verify(test, [ 10, 20 ], -1, { lines: { 1: 1, 3: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });114 var cov = verifier.getFileCoverage();115 test.equal(true, cov.branchMap[1].locations[0].skip);116 test.equal(true, cov.statementMap[3].skip);117 test.done();118 },119 "should skip if statement completely": function (test) {120 code[1] = '/* istanbul ignore next */';121 verifier = helper.verifier(__filename, code);122 verifier.verify(test, [ 10, 20 ], -1, { lines: { 1: 1, 3: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });123 var cov = verifier.getFileCoverage();124 test.equal(true, cov.branchMap[1].locations[0].skip);125 test.equal(true, cov.branchMap[1].locations[1].skip);126 test.equal(true, cov.statementMap[3].skip);127 test.done();128 }129 }130 }131 },132 "with a simple if-else": {133 "as a statement": {134 setUp: function (cb) {135 code = [136 '// hint',137 'if (args[0] > args [1])',138 ' output = args[0];',139 'else',140 ' output = args[1];'141 ];142 cb(null);143 },144 "should cover then path": function (test) {145 code[0] = '// istanbul ignore else';146 verifier = helper.verifier(__filename, code);147 verifier.verify(test, [ 20, 10 ], 20, { lines: { 2: 1, 3: 1, 5: 1 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });148 var cov = verifier.getFileCoverage();149 test.equal(true, cov.branchMap[1].locations[1].skip);150 test.equal(true, cov.statementMap[3].skip);151 test.done();152 },153 "should cover else path": function (test) {154 code[0] = '// istanbul ignore if ';155 verifier = helper.verifier(__filename, code);156 verifier.verify(test, [ 10, 20 ], 20, { lines: { 2: 1, 3: 1, 5: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 0, '3': 1 } });157 var cov = verifier.getFileCoverage();158 test.equal(true, cov.branchMap[1].locations[0].skip);159 test.equal(true, cov.statementMap[2].skip);160 test.done();161 }162 }163 }...
Using AI Code Generation
1var istanbul = require('istanbul');2var api = require('istanbul-api');3var path = require('path');4var fs = require('fs');5var mapStore = istanbul.createCoverageMap({});6var file = path.resolve(__dirname, 'coverage/coverage-final.json');7var json = JSON.parse(fs.readFileSync(file, 'utf8'));8mapStore.merge(json);9var verifier = new api.Verifier();10verifier.addAll(['json', 'text', 'lcov', 'clover', 'cobertura', 'teamcity']);11verifier.verify(mapStore, {coverageVariable: '__coverage__'}, function (err) {12 if (err) {13 console.error(err);14 process.exit(1);15 }16 console.log('All good');17 process.exit(0);18});19var istanbul = require('istanbul');20var api = require('istanbul-api');21var path = require('path');22var fs = require('fs');23var mapStore = istanbul.createCoverageMap({});24var file = path.resolve(__dirname, 'coverage/coverage-final.json');25var json = JSON.parse(fs.readFileSync(file, 'utf8'));26mapStore.merge(json);27var verifier = new api.Verifier();28verifier.addAll(['json', 'text', 'lcov', 'clover', 'cobertura', 'teamcity']);29var thresholds = {30 global: {31 }32};33var result = verifier.check(mapStore, thresholds);34console.log(result);35Istanbul-api supports all the browsers that are supported by [istanbul-lib-instrument](
Using AI Code Generation
1var fs = require('fs');2var istanbul = require('istanbul');3var verifier = new istanbul.Verifier();4var report = istanbul.utils.readJsonFile('coverage/coverage-final.json');5var fileCoverage = verifier.getFileCoverage(report, 'src/sum.js');6console.log(fileCoverage);7FileCoverage {8 statementMap: { '1': [Object], '2': [Object], '3': [Object] },9 fnMap: { '1': [Object] },10 branchMap: {},11 s: { '1': 0, '2': 0, '3': 0 },12 f: { '1': 0 },13 b: {},14 hash: 'c1a9ac9f7b4f4f3a7c4e4d8a7e4a1e2f7b4f4f3a' }15var fs = require('fs');16var istanbul = require('istanbul');17var verifier = new istanbul.Verifier();18var report = istanbul.utils.readJsonFile('coverage/coverage-final.json');19var fileCoverage = verifier.getFileCoverage(report, 'src/sum.js');20var reporter = new istanbul.Reporter();21reporter.add('text');22reporter.write(fileCoverage);
Using AI Code Generation
1var istanbul = require('istanbul');2var verifier = require('istanbul').verifier;3var path = require('path');4var fs = require('fs');5var report = require('istanbul').Report.create('text');6var collector = new istanbul.Collector();7var coverage = JSON.parse(fs.readFileSync('coverage/coverage.json', 'utf8'));8collector.add(coverage);9report.on('done', function() {10 console.log('done');11});12report.writeReport(collector, true);13verifier.addMatcher('only-files', function(file) {14 return file.match(/\/test\/unit\/.*\.js$/);15});16verifier.addMatcher('exclude-files', function(file) {17 return !file.match(/\/test\/unit\/.*\.js$/);18});19verifier.verify(collector, function(err, success) {20 if (err) {21 console.log(err);22 }23 if (success) {24 console.log('success');25 }26});
Using AI Code Generation
1var istanbul = require('istanbul');2var verifier = new istanbul.Verifier();3verifier.addFileCoverage('coverage/coverage.json');4var summary = verifier.getSummary();5console.log(summary);6process.exit(0);
Using AI Code Generation
1var istanbul = require('istanbul');2var verifier = new istanbul.Verifier();3verifier.addFileCoverage(istanbul.utils.summarizeFileCoverage(istanbul.utils.readCoverageFile('coverage/coverage-final.json')));4verifier.printDetailedReport();5module.exports = function(grunt) {6 grunt.initConfig({7 pkg: grunt.file.readJSON('package.json'),8 clean: {9 },10 instrument: {11 options: {12 }13 },14 mochaTest: {15 test: {16 options: {17 },18 }19 },20 storeCoverage: {21 options: {22 }23 },24 makeReport: {25 options: {26 }27 }28 });29 grunt.loadNpmTasks('grunt-contrib-clean');30 grunt.loadNpmTasks('grunt-istanbul');31 grunt.loadNpmTasks('grunt-mocha-test');32 grunt.registerTask('test', ['clean', 'instrument', 'mochaTest', 'storeCoverage', 'makeReport']);33 grunt.registerTask('default', ['test']);34};35module.exports = function(config) {36 config.set({37 preprocessors: {38 },39 coverageReporter: {
Using AI Code Generation
1var istanbul = require('istanbul-api');2var path = require('path');3var fs = require('fs');4var mapStore = istanbul.libReport.create('json');5var report = istanbul.libReport.create('text');6var testRunner = istanbul.libTestRunner.create({7});8testRunner.run(path.join(__dirname, 'test.js'), function() {9 var coverageMap = mapStore.getFinalCoverage();10 var fileCoverage = coverageMap['test.js'];11 console.log(fileCoverage);12});13var assert = require('assert');14describe('test', function() {15 it('should pass', function() {16 assert.equal(1, 1);17 });18});19{20 "test.js": {21 "s": {22 },23 "b": {},24 "f": {25 },26 "fnMap": {27 "1": {28 "loc": {29 "start": {30 },31 "end": {32 }33 }34 }35 },36 "statementMap": {37 "1": {38 "start": {39 },40 "end": {41 }42 },43 "2": {44 "start": {45 },46 "end": {47 }48 }49 },50 "branchMap": {}51 }52}
Using AI Code Generation
1var fs = require('fs');2var istanbul = require('istanbul');3var verifier = require('istanbul').api.verifier;4var collector = new istanbul.Collector();5var report = fs.readFileSync('coverage/coverage.json', 'utf8');6var coverage = JSON.parse(report);7collector.add(coverage);8var fileCoverage = verifier.getFileCoverage(collector, 'test.js');9var functionCoverage = fileCoverage.getFunctionCoverage();10var lineCoverage = fileCoverage.getLineCoverage();11var branchCoverage = fileCoverage.getBranchCoverage();12var statementCoverage = fileCoverage.getStatementCoverage();13var expressionCoverage = fileCoverage.getExpressionCoverage();14console.log('Function Coverage: ' + functionCoverage);15console.log('Line Coverage: ' + lineCoverage);16console.log('Branch Coverage: ' + branchCoverage);17console.log('Statement Coverage: ' + statementCoverage);18console.log('Expression Coverage: ' + expressionCoverage);
Using AI Code Generation
1var verifier = require('istanbul');2var fs = require('fs');3var report = fs.readFileSync('coverage/coverage.json', 'utf8');4var fileCoverage = verifier.utils.summarizeCoverage(JSON.parse(report));5var file = fileCoverage['test.js'];6console.log(file.s);7console.log(file.b);8console.log(file.f);9console.log(file.l);10console.log(file.fnMap);11console.log(file.statementMap);12console.log(file.branchMap);
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!!