Best JavaScript code snippet using mocha
html.js
Source:html.js
...65 on(passesLink, 'click', function(){66 unhide();67 var name = /pass/.test(report.className) ? '' : ' pass';68 report.className = report.className.replace(/fail|pass/g, '') + name;69 if (report.className.trim()) hideSuitesWithout('test pass');70 });71 // failure toggle72 on(failuresLink, 'click', function(){73 unhide();74 var name = /fail/.test(report.className) ? '' : ' fail';75 report.className = report.className.replace(/fail|pass/g, '') + name;76 if (report.className.trim()) hideSuitesWithout('test fail');77 });78 root.appendChild(stat);79 root.appendChild(report);80 if (progress) progress.size(40);81 runner.on('suite', function(suite){82 if (suite.root) return;83 // suite84 var url = self.suiteURL(suite);85 var el = fragment('<li class="suite"><h1><a href="%s">%s</a></h1></li>', url, escape(suite.title));86 // container87 stack[0].appendChild(el);88 stack.unshift(document.createElement('ul'));89 el.appendChild(stack[0]);90 });91 runner.on('suite end', function(suite){92 if (suite.root) return;93 stack.shift();94 });95 runner.on('fail', function(test, err){96 if ('hook' == test.type) runner.emit('test end', test);97 });98 runner.on('test end', function(test){99 // TODO: add to stats100 var percent = stats.tests / this.total * 100 | 0;101 if (progress) progress.update(percent).draw(ctx);102 // update stats103 var ms = new Date - stats.start;104 text(passes, stats.passes);105 text(failures, stats.failures);106 text(duration, (ms / 1000).toFixed(2));107 // test108 if ('passed' == test.state) {109 var url = self.testURL(test);110 var el = fragment('<li class="test pass %e"><h2>%e<span class="duration">%ems</span> <a href="%s" class="replay">â£</a></h2></li>', test.speed, test.title, test.duration, url);111 } else if (test.pending) {112 var el = fragment('<li class="test pass pending"><h2>%e</h2></li>', test.title);113 } else {114 var el = fragment('<li class="test fail"><h2>%e <a href="?grep=%e" class="replay">â£</a></h2></li>', test.title, encodeURIComponent(test.fullTitle()));115 var str = test.err.stack || test.err.toString();116 // FF / Opera do not add the message117 if (!~str.indexOf(test.err.message)) {118 str = test.err.message + '\n' + str;119 }120 // <=IE7 stringifies to [Object Error]. Since it can be overloaded, we121 // check for the result of the stringifying.122 if ('[object Error]' == str) str = test.err.message;123 // Safari doesn't give you a stack. Let's at least provide a source line.124 if (!test.err.stack && test.err.sourceURL && test.err.line !== undefined) {125 str += "\n(" + test.err.sourceURL + ":" + test.err.line + ")";126 }127 el.appendChild(fragment('<pre class="error">%e</pre>', str));128 }129 // toggle code130 // TODO: defer131 if (!test.pending) {132 var h2 = el.getElementsByTagName('h2')[0];133 on(h2, 'click', function(){134 pre.style.display = 'none' == pre.style.display135 ? 'block'136 : 'none';137 });138 var pre = fragment('<pre><code>%e</code></pre>', utils.clean(test.fn.toString()));139 el.appendChild(pre);140 pre.style.display = 'none';141 }142 // Don't call .appendChild if #mocha-report was already .shift()'ed off the stack.143 if (stack[0]) stack[0].appendChild(el);144 });145}146/**147 * Provide suite URL148 *149 * @param {Object} [suite]150 */151HTML.prototype.suiteURL = function(suite){152 return '?grep=' + encodeURIComponent(suite.fullTitle());153};154/**155 * Provide test URL156 *157 * @param {Object} [test]158 */159HTML.prototype.testURL = function(test){160 return '?grep=' + encodeURIComponent(test.fullTitle());161};162/**163 * Display error `msg`.164 */165function error(msg) {166 document.body.appendChild(fragment('<div id="mocha-error">%s</div>', msg));167}168/**169 * Return a DOM fragment from `html`.170 */171function fragment(html) {172 var args = arguments173 , div = document.createElement('div')174 , i = 1;175 div.innerHTML = html.replace(/%([se])/g, function(_, type){176 switch (type) {177 case 's': return String(args[i++]);178 case 'e': return escape(args[i++]);179 }180 });181 return div.firstChild;182}183/**184 * Check for suites that do not have elements185 * with `classname`, and hide them.186 */187function hideSuitesWithout(classname) {188 var suites = document.getElementsByClassName('suite');189 for (var i = 0; i < suites.length; i++) {190 var els = suites[i].getElementsByClassName(classname);191 if (0 == els.length) suites[i].className += ' hidden';192 }193}194/**195 * Unhide .hidden suites.196 */197function unhide() {198 var els = document.getElementsByClassName('suite hidden');199 for (var i = 0; i < els.length; ++i) {200 els[i].className = els[i].className.replace('suite hidden', 'suite');201 }...
html-report.js
Source:html-report.js
1function hideSuitesWithout(className) {2 var suites = document.querySelectorAll('#report > .suite');3 for (var i = 0; i < suites.length; i++) {4 let suite = suites[i];5 var els = suite.getElementsByClassName(className);6 if (!els.length) {7 suite.classList.add('hidden');8 }9 }10}11function unhide() {12 var suites = document.getElementsByClassName('suite hidden');13 while (suites.length) {14 suites[0].classList.remove('hidden');15 }16}17function togglePasses() {18 var report = document.getElementById('report');19 report.classList.toggle('hide-passes');20 if (report.classList.contains('hide-passes')) {21 hideSuitesWithout('test fail');22 } else {23 unhide();24 }25}26document.getElementById('toggle-passes').addEventListener('click', function(e) {27 e.preventDefault();28 29 this.classList.toggle('checked');30 togglePasses();31});32var tests = document.getElementsByClassName('test');33for (var i = 0; i < tests.length; i++) {34 var elem = tests[i];35 var head = elem.children[0];...
Using AI Code Generation
1var Mocha = require('mocha'),2 fs = require('fs'),3 path = require('path');4var mocha = new Mocha();5var testDir = 'test';6fs.readdirSync(testDir).filter(function(file){7 return file.substr(-3) === '.js';8}).forEach(function(file){9 mocha.addFile(10 path.join(testDir, file)11 );12});13mocha.run(function(failures){14 process.on('exit', function () {15 });16});17var Mocha = require('mocha'),18 fs = require('fs'),19 path = require('path');20var mocha = new Mocha();21var testDir = 'test';22fs.readdirSync(testDir).filter(function(file){23 return file.substr(-3) === '.js';24}).forEach(function(file){25 mocha.addFile(26 path.join(testDir, file)27 );28});29mocha.run(function(failures){30 process.on('exit', function () {31 });32});
Using AI Code Generation
1var Mocha = require('mocha');2var mocha = new Mocha();3mocha.hideSuitesWithout('smoke');4mocha.addFile('test1.js');5mocha.addFile('test2.js');6mocha.run();
Using AI Code Generation
1var Mocha = require('mocha');2var mocha = new Mocha();3mocha.addFile('test.js');4mocha.run(function(failures){5 process.on('exit', function () {6 });7});8var assert = require('assert');9describe('Array', function() {10 describe('#indexOf()', function() {11 it('should return -1 when the value is not present', function() {12 assert.equal([1,2,3].indexOf(4), -1);13 });14 });15});
Using AI Code Generation
1var Mocha = require('mocha');2var mocha = new Mocha({3});4mocha.addFile(5);6mocha.run(function(failures){7 process.on('exit', function () {8 });9});10var Mocha = require('mocha');11var mocha = new Mocha({12});13mocha.addFile(14);15mocha.grep('test name').run(function(failures){16 process.on('exit', function () {17 });18});
Using AI Code Generation
1var Mocha = require('mocha');2var mocha = new Mocha();3mocha.hideSuitesWithout('smoke');4mocha.addFile('test.js');5mocha.run(function(failures){6 process.on('exit', function () {7 });8});9var Mocha = require('mocha');10var mocha = new Mocha();11mocha.before(function(){12});13mocha.addFile('test.js');14mocha.run(function(failures){15 process.on('exit', function () {16 process.exit(failures);17 });18});19var Mocha = require('mocha');20var mocha = new Mocha();21mocha.after(function(){22});23mocha.addFile('test.js');24mocha.run(function(failures){25 process.on('exit', function () {
Using AI Code Generation
1var Mocha = require('mocha');2var mocha = new Mocha({3});4mocha.hideSuitesWithout('smoke');5mocha.addFile('test/test.js');6mocha.run(function(failures){7 process.on('exit', function () {8 });9});
Using AI Code Generation
1var Mocha = require('mocha');2var mocha = new Mocha();3mocha.hideSuitesWithout(/should/);4mocha.run(function(failures){5 process.on('exit', function () {6 });7});8var Mocha = require('mocha');9var mocha = new Mocha();10mocha.hideSuitesWithout(/should/);11mocha.run(function(failures){12 process.on('exit', function () {13 });14});15var Mocha = require('mocha');16var mocha = new Mocha();17mocha.hideSuitesWithout(/should/);18mocha.run(function(failures){19 process.on('exit', function () {20 });21});22var Mocha = require('mocha');23var mocha = new Mocha();24mocha.hideSuitesWithout(/should/);25mocha.run(function(failures){26 process.on('exit', function () {27 });28});29var Mocha = require('mocha');30var mocha = new Mocha();31mocha.hideSuitesWithout(/should/);32mocha.run(function(failures){33 process.on('exit', function () {
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!!