Best JavaScript code snippet using mocha
leakList.js
Source:leakList.js
...277 excludeAnd.prop('checked', true);278 } else {279 excludeOr.prop('checked', true);280 }281 filterLeaks()282}283function filterLeaks() {284 articles = []285 articlesRaw.each(function (_, k) {286 let tags = []287 let tagA = $(k).find(".tags")288 if (tagA.length !== 0)289 tagA.find(".tag").each(function (_, e) {290 tags[tags.length] = ($(e).text())291 })292 let include = includeTags.length === 0293 // include logic294 if (includeOr.is(':checked')) {295 for (let i in tags) {296 let tag = tags[i]297 if (includeTags.includes(tag)) {...
runner.js
Source:runner.js
...96 * @api private97 */98Runner.prototype.checkGlobals = function(test){99 if (this.ignoreLeaks) return;100 var leaks = filterLeaks(this._globals);101 this._globals = this._globals.concat(leaks);102 if (leaks.length > 1) {103 this.fail(test, new Error('global leaks detected: ' + leaks.join(', ') + ''));104 } else if (leaks.length) {105 this.fail(test, new Error('global leak detected: ' + leaks[0]));106 }107};108/**109 * Fail the given `test`.110 *111 * @param {Test} test112 * @param {Error} err113 * @api private114 */115Runner.prototype.fail = function(test, err){116 ++this.failures;117 test.state = 'failed';118 if ('string' == typeof err) {119 err = new Error('the string "' + err + '" was thrown, throw an Error :)');120 }121 this.emit('fail', test, err);122};123/**124 * Fail the given `hook` with `err`.125 *126 * Hook failures (currently) hard-end due127 * to that fact that a failing hook will128 * surely cause subsequent tests to fail,129 * causing jumbled reporting.130 *131 * @param {Hook} hook132 * @param {Error} err133 * @api private134 */135Runner.prototype.failHook = function(hook, err){136 this.fail(hook, err);137 this.emit('end');138};139/**140 * Run hook `name` callbacks and then invoke `fn()`.141 *142 * @param {String} name143 * @param {Function} function144 * @api private145 */146Runner.prototype.hook = function(name, fn){147 var suite = this.suite148 , hooks = suite['_' + name]149 , ms = suite._timeout150 , self = this151 , timer;152 function next(i) {153 var hook = hooks[i];154 if (!hook) return fn();155 self.currentRunnable = hook;156 self.emit('hook', hook);157 hook.on('error', function(err){158 self.failHook(hook, err);159 });160 hook.run(function(err){161 hook.removeAllListeners('error');162 if (err) return self.failHook(hook, err);163 self.emit('hook end', hook);164 next(++i);165 });166 }167 process.nextTick(function(){168 next(0);169 });170};171/**172 * Run hook `name` for the given array of `suites`173 * in order, and callback `fn(err)`.174 *175 * @param {String} name176 * @param {Array} suites177 * @param {Function} fn178 * @api private179 */180Runner.prototype.hooks = function(name, suites, fn){181 var self = this182 , orig = this.suite;183 function next(suite) {184 self.suite = suite;185 if (!suite) {186 self.suite = orig;187 return fn();188 }189 self.hook(name, function(err){190 if (err) {191 self.suite = orig;192 return fn(err);193 }194 next(suites.pop());195 });196 }197 next(suites.pop());198};199/**200 * Run hooks from the top level down.201 *202 * @param {String} name203 * @param {Function} fn204 * @api private205 */206Runner.prototype.hookUp = function(name, fn){207 var suites = [this.suite].concat(this.parents()).reverse();208 this.hooks(name, suites, fn);209};210/**211 * Run hooks from the bottom up.212 *213 * @param {String} name214 * @param {Function} fn215 * @api private216 */217Runner.prototype.hookDown = function(name, fn){218 var suites = [this.suite].concat(this.parents());219 this.hooks(name, suites, fn);220};221/**222 * Return an array of parent Suites from223 * closest to furthest.224 *225 * @return {Array}226 * @api private227 */228Runner.prototype.parents = function(){229 var suite = this.suite230 , suites = [];231 while (suite = suite.parent) suites.push(suite);232 return suites;233};234/**235 * Run the current test and callback `fn(err)`.236 *237 * @param {Function} fn238 * @api private239 */240Runner.prototype.runTest = function(fn){241 var test = this.test242 , self = this;243 try {244 test.on('error', function(err){245 self.fail(test, err);246 });247 test.run(fn);248 } catch (err) {249 fn(err);250 }251};252/**253 * Run tests in the given `suite` and invoke254 * the callback `fn()` when complete.255 *256 * @param {Suite} suite257 * @param {Function} fn258 * @api private259 */260Runner.prototype.runTests = function(suite, fn){261 var self = this262 , tests = suite.tests263 , test;264 function next(err) {265 // if we bail after first err266 if (self.failures && suite._bail) return fn();267 // next test268 test = tests.shift();269 // all done270 if (!test) return fn();271 // grep272 if (!self._grep.test(test.fullTitle())) return next();273 // pending274 if (test.pending) {275 self.emit('pending', test);276 self.emit('test end', test);277 return next();278 }279 // execute test and hook(s)280 self.emit('test', self.test = test);281 self.hookDown('beforeEach', function(){282 self.currentRunnable = self.test;283 self.runTest(function(err){284 test = self.test;285 if (err) {286 self.fail(test, err);287 self.emit('test end', test);288 return self.hookUp('afterEach', next);289 }290 test.state = 'passed';291 self.emit('pass', test);292 self.emit('test end', test);293 self.hookUp('afterEach', next);294 });295 });296 }297 this.next = next;298 next();299};300/**301 * Run the given `suite` and invoke the302 * callback `fn()` when complete.303 *304 * @param {Suite} suite305 * @param {Function} fn306 * @api private307 */308Runner.prototype.runSuite = function(suite, fn){309 var total = this.grepTotal(suite)310 , self = this311 , i = 0;312 debug('run suite %s', suite.fullTitle());313 if (!total) return fn();314 this.emit('suite', this.suite = suite);315 function next() {316 var curr = suite.suites[i++];317 if (!curr) return done();318 self.runSuite(curr, next);319 }320 function done() {321 self.suite = suite;322 self.hook('afterAll', function(){323 self.emit('suite end', suite);324 fn();325 });326 }327 this.hook('beforeAll', function(){328 self.runTests(suite, next);329 });330};331/**332 * Handle uncaught exceptions.333 *334 * @param {Error} err335 * @api private336 */337Runner.prototype.uncaught = function(err){338 debug('uncaught exception');339 var runnable = this.currentRunnable;340 if ('failed' == runnable.state) return;341 runnable.clearTimeout();342 err.uncaught = true;343 this.fail(runnable, err);344 // recover from test345 if ('test' == runnable.type) {346 this.emit('test end', runnable);347 this.hookUp('afterEach', this.next);348 return;349 }350 // bail on hooks351 this.emit('end');352};353/**354 * Run the root suite and invoke `fn(failures)`355 * on completion.356 *357 * @param {Function} fn358 * @return {Runner} for chaining359 * @api public360 */361Runner.prototype.run = function(fn){362 var self = this363 , fn = fn || function(){};364 debug('start');365 // callback366 this.on('end', function(){367 debug('end');368 process.removeListener('uncaughtException', this.uncaught);369 fn(self.failures);370 });371 // run suites372 this.emit('start');373 this.runSuite(this.suite, function(){374 debug('finished running');375 self.emit('end');376 });377 // uncaught exception378 process.on('uncaughtException', function(err){379 self.uncaught(err);380 });381 return this;382};383/**384 * Filter leaks with the given globals flagged as `ok`.385 *386 * @param {Array} ok387 * @return {Array}388 * @api private389 */390function filterLeaks(ok) {391 return filter(keys(global), function(key){392 var matched = filter(ok, function(ok){393 return 0 == key.indexOf(ok.split('*')[0]);394 });395 return matched.length == 0 && (!global.navigator || 'onerror' !== key);396 });...
globalLeaks.js
Source:globalLeaks.js
...77 var globals = this.getGlobals();78 var leaks;79 80 if (ok.length + ignored.length === globals.length) return [];81 leaks = this.filterLeaks(globals);82 this._globals = this._globals.concat(leaks);83 return leaks;84 };85 86 GlobalChecker.prototype.checkGlobals = function(test) {87 if (!this.shouldCheckGlobals(test)) {88 this.testSkiped = true;89 return;90 }91 92 var leaks = this.checkNewGlobals();93 94 if (leaks.length > 1) {95 this.runner.fail(test, new Error(leaks.length + ' global leaks detected: ' + leaks.join(', ') + ''));...
Using AI Code Generation
1var mocha = new Mocha({reporter: 'spec'});2mocha.addFile('./test.js');3mocha.run(function(failures){4 process.on('exit', function () {5 process.exit(failures);6 });7});8 0 passing (9ms)
Using AI Code Generation
1var mocha = require('mocha');2var filterLeaks = mocha.utils.filterLeaks;3var myLeaks = ['myLeak1', 'myLeak2'];4var filteredLeaks = filterLeaks(myLeaks);5var mocha = require('mocha');6var filterLeaks = mocha.utils.filterLeaks;7var myLeaks = ['myLeak1', 'myLeak2'];8var filteredLeaks = filterLeaks(myLeaks);
Using AI Code Generation
1var filterLeaks = require('mocha/lib/utils').filterLeaks;2var leaks = filterLeaks(['global', 'window', 'document', 'console']);3console.log(leaks);4var filterLeaks = require('mocha/lib/utils').filterLeaks;5var leaks = filterLeaks(['global', 'window', 'document', 'console']);6console.log(leaks);7var filterLeaks = require('mocha/lib/utils').filterLeaks;8var leaks = filterLeaks(['global', 'window', 'document', 'console']);9console.log(leaks);10var filterLeaks = require('mocha/lib/utils').filterLeaks;11var leaks = filterLeaks(['global', 'window', 'document', 'console']);12console.log(leaks);13var filterLeaks = require('mocha/lib/utils').filterLeaks;14var leaks = filterLeaks(['global', 'window', 'document', 'console']);15console.log(leaks);16var filterLeaks = require('mocha/lib/utils').filterLeaks;17var leaks = filterLeaks(['global', 'window', 'document', 'console']);18console.log(leaks);19var filterLeaks = require('mocha/lib/utils').filterLeaks;20var leaks = filterLeaks(['global', 'window', 'document', 'console']);21console.log(leaks);22var filterLeaks = require('mocha/lib/utils').filterLeaks;23var leaks = filterLeaks(['global', 'window', 'document', 'console']);24console.log(leaks);
Using AI Code Generation
1var MochaLeaks = require('mocha-leaks');2MochaLeaks.filterLeaks('global', 'console');3var MochaLeaks = require('mocha-leaks');4MochaLeaks.filterLeaks('global', 'console');5module.exports = function(grunt) {6 grunt.loadNpmTasks('grunt-mocha-leaks');7 grunt.initConfig({8 mochaLeaks: {9 options: {10 },11 }12 });13};14var gulp = require('gulp');15var mochaLeaks = require('gulp-mocha-leaks');16gulp.task('default', function() {17 return gulp.src('test/**/*.js')18 .pipe(mochaLeaks({leaks: ['global', 'console']}));19});20module.exports = function(config) {21 config.set({22 preprocessors: {23 },24 mochaLeaksReporter: {25 }26 });27};28var MochaLeaks = require('mocha-leaks');29MochaLeaks.filterLeaks('global');30var MochaLeaks = require('mocha-leaks');31MochaLeaks.filterLeaks('console');32var MochaLeaks = require('mocha-leaks');33MochaLeaks.filterLeaks('process');
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!!