How to use thrown2Error method in Mocha

Best JavaScript code snippet using mocha

runner.js

Source:runner.js Github

copy

Full Screen

...113 }114 ++this.failures;115 test.state = 'failed';116 if (!isError(err)) {117 err = thrown2Error(err);118 }119 // TODO filter stack trace120 this.emit('fail', test, err);121 }122 failHook(hook, err) {123 hook.originalTitle = hook.originalTitle || hook.title;124 if (hook.ctx && hook.ctx.currentTest) {125 hook.title =126 hook.originalTitle + ' for ' + `"${hook.ctx.currentTest.title}"`;127 } else {128 var parentTitle;129 if (hook.parent.title) {130 parentTitle = hook.parent.title;131 } else {132 parentTitle = hook.parent.root ? '{root}' : '';133 }134 hook.title = hook.originalTitle + ' in ' + `"parentTitle"`;135 }136 this.fail(hook, err);137 }138 hook(name, fn) {139 var suite = this.suite;140 var hooks = suite.getHooks(name);141 var self = this;142 function next(i) {143 var hook = hooks[i];144 if (!hook) {145 return fn();146 }147 self.currentRunnable = hook;148 if (name === 'beforeAll') {149 hook.ctx.currentTest = hook.parent.tests[0];150 } else if (name === 'afterAll') {151 hook.ctx.currentTest = hook.parent.tests[hook.parent.tests.length - 1];152 } else {153 hook.ctx.currentTest = self.test;154 }155 hook.allowUncaught = self.allowUncaught;156 self.emit('hook', hook);157 if (!hook.listeners('error').length) {158 hook.on('error', function(err) {159 self.failHook(hook, err);160 });161 }162 hook.run(function(err) {163 var testError = hook.error();164 if (testError) {165 self.fail(self.test, testError);166 }167 if (err) {168 if (err instanceof Pending) {169 if (name === 'afterAll') {170 throw new Error('Skipping a test within an "after all" hook is forbidden.');171 }172 if (name === 'beforeEach' || name === 'afterEach') {173 if (self.test) {174 self.test.pending = true;175 }176 } else {177 suite.tests.forEach(function(test) {178 test.pending = true;179 });180 suite.suites.forEach(function(suite) {181 suite.pending = true;182 });183 hook.pending = true;184 }185 } else {186 self.failHook(hook, err);187 return fn(err);188 }189 }190 self.emit('hook end', hook);191 delete hook.ctx.currentTest;192 next(++i);193 });194 }195 next(0);196 }197 hooks(name, suites, fn) {198 var self = this;199 var orig = this.suite;200 function next(suite) {201 self.suite = suite;202 if (!suite) {203 self.suite = orig;204 return fn();205 }206 self.hook(name, function(err) {207 if (err) {208 var errSuite = self.suite;209 self.suite = orig;210 return fn(err, errSuite);211 }212 next(suites.pop());213 });214 }215 next(suites.pop());216 }217 hookUp(name, fn) {218 var suites = [this.suite].concat(this.parents()).reverse();219 this.hooks(name, suites, fn);220 }221 hookDown(name, fn) {222 var suites = [this.suite].concat(this.parents());223 this.hooks(name, suites, fn);224 }225 parents() {226 var suite = this.suite;227 var suites = [];228 while (suite.parent) {229 suite = suite.parent;230 suites.push(suite);231 }232 return suites;233 }234 runTest(fn) {235 var self = this;236 var test = this.test;237 if (!test) {238 return;239 }240 var suite = this.parents().reverse()[0] || this.suite;241 if (this.forbidOnly && suite.hasOnly()) {242 fn(new Error('`.only` forbidden'));243 return;244 }245 test.on('error', function(err) {246 self.fail(test, err);247 });248 if (this.allowUncaught) {249 test.allowUncaught = true;250 return test.run(fn);251 }252 try {253 test.run(fn);254 } catch (err) {255 fn(err);256 }257 }258 runTests(suite, fn) {259 var self = this;260 var tests = suite.tests.slice();261 var test;262 function hookErr(_, errSuite, after) {263 var orig = self.suite;264 self.suite = after ? errSuite.parent : errSuite;265 if (self.suite) {266 self.hookUp('afterEach', function(err2, errSuite2) {267 self.suite = orig;268 if (err2) {269 return hookErr(err2, errSuite2, true);270 }271 fn(errSuite);272 });273 } else {274 self.suite = orig;275 fn(errSuite);276 }277 }278 function next(err, errSuite) {279 if (self.failures && suite._bail) {280 tests = [];281 }282 if (self._abort) {283 return fn();284 }285 if (err) {286 return hookErr(err, errSuite, true);287 }288 test = tests.shift();289 if (!test) {290 return fn();291 }292 var match = self._grep.test(test.fullTitle());293 if (self._invert) {294 match = !match;295 }296 if (!match) {297 next();298 return;299 }300 if (test.isPending()) {301 if (self.forbidPending) {302 test.isPending = () => false;303 self.fail(test, new Error('Pending test forbidden'));304 delete test.isPending;305 } else {306 self.emit('pending', test);307 }308 self.emit('test end', test);309 return next();310 }311 self.emit('test', (self.test = test));312 self.hookDown('beforeEach', function(err, errSuite) {313 if (test.isPending()) {314 if (self.forbidPending) {315 test.isPending = () => false;316 self.fail(test, new Error('Pending test forbidden'));317 delete test.isPending;318 } else {319 self.emit('pending', test);320 }321 self.emit('test end', test);322 return next();323 }324 if (err) {325 return hookErr(err, errSuite, false);326 }327 self.currentRunnable = self.test;328 self.runTest(function(err) {329 test = self.test;330 if (err) {331 var retry = test.currentRetry();332 if (err instanceof Pending && self.forbidPending) {333 self.fail(test, new Error('Pending test forbidden'));334 } else if (err instanceof Pending) {335 test.pending = true;336 self.emit('pending', test);337 } else if (retry < test.retries()) {338 var clonedTest = test.clone();339 clonedTest.currentRetry(retry + 1);340 tests.unshift(clonedTest);341 self.emit('retry', test, err);342 return self.hookUp('afterEach', next);343 } else {344 self.fail(test, err);345 }346 self.emit('test end', test);347 if (err instanceof Pending) {348 return next();349 }350 return self.hookUp('afterEach', next);351 }352 test.state = 'passed';353 self.emit('pass', test);354 self.emit('test end', test);355 self.hookUp('afterEach', next);356 });357 });358 }359 this.next = next;360 this.hookErr = hookErr;361 next();362 }363 runSuite(suite, fn) {364 var i = 0;365 var self = this;366 var total = this.grepTotal(suite);367 var afterAllHookCalled = false;368 if (!total || (self.failures && suite._bail)) {369 return fn();370 }371 this.emit('suite', (this.suite = suite));372 function next(errSuite) {373 if (errSuite) {374 if (errSuite === suite) {375 return done();376 }377 return done(errSuite);378 }379 if (self._abort) {380 return done();381 }382 var curr = suite.suites[i++];383 if (!curr) {384 return done();385 }386 self.runSuite(curr, next);387 }388 function done(errSuite) {389 self.suite = suite;390 self.nextSuite = next;391 if (afterAllHookCalled) {392 fn(errSuite);393 } else {394 afterAllHookCalled = true;395 delete self.test;396 self.hook('afterAll', function() {397 self.emit('suite end', suite);398 fn(errSuite);399 });400 }401 }402 this.nextSuite = next;403 this.hook('beforeAll', function(err) {404 if (err) {405 return done();406 }407 self.runTests(suite, next);408 });409 }410 uncaught(err) {411 if (err instanceof Pending) {412 return;413 }414 if (!err) {415 err = createInvalidExceptionError(416 'Caught falsy/undefined exception which would otherwise be uncaught. No stack trace found; try a debugger',417 err418 );419 }420 if (!isError(err)) {421 err = thrown2Error(err);422 }423 err.uncaught = true;424 var runnable = this.currentRunnable;425 if (!runnable) {426 runnable = new Runnable('Uncaught error outside test suite');427 runnable.parent = this.suite;428 if (this.started) {429 this.fail(runnable, err);430 } else {431 this.emit('start');432 this.fail(runnable, err);433 this.emit('end');434 }435 return;436 }437 if (runnable.isFailed() || runnable.isPending()) {438 return;439 }440 var alreadyPassed = runnable.isPassed();441 this.fail(runnable, err);442 if (!alreadyPassed) {443 if (runnable.type === 'test') {444 this.emit('test end', runnable);445 this.hookUp('afterEach', this.next);446 return;447 }448 var errSuite = this.suite;449 if (runnable.fullTitle().indexOf('after each') > -1) {450 return this.hookErr(err, errSuite, true);451 }452 if (runnable.fullTitle().indexOf('before each') > -1) {453 return this.hookErr(err, errSuite, false);454 }455 return this.nextSuite(errSuite);456 }457 this.emit('end');458 }459 run(fn) {460 var self = this;461 var rootSuite = this.suite;462 fn = fn || function() {};463 function uncaught(err) {464 self.uncaught(err);465 }466 function start() {467 if (rootSuite.hasOnly()) {468 rootSuite.filterOnly();469 }470 self.started = true;471 if (self._delay) {472 self.emit('ready');473 }474 self.emit('start');475 self.runSuite(rootSuite, function() {476 self.emit('end');477 });478 }479 this.on('suite end', function(suite) {480 suite.cleanReferences();481 });482 this.on('end', function() {483 process.removeListener('uncaughtException', uncaught);484 fn(self.failures);485 });486 process.on('uncaughtException', uncaught);487 if (this._delay) {488 this.emit('waiting', rootSuite);489 rootSuite.once('run', start);490 } else {491 start();492 }493 return this;494 }495 abort() {496 this._abort = true;497 return this;498 }499}500function filterLeaks(ok, globals) {501 return globals.filter(function(key) {502 if (/^\d+/.test(key)) {503 return false;504 }505 if (/^mocha-/.test(key)) {506 return false;507 }508 var matched = ok.filter(function(ok) {509 if (~ok.indexOf('*')) {510 return key.indexOf(ok.split('*')[0]) === 0;511 }512 return key === ok;513 });514 return !matched.length && (!global.navigator || key !== 'onerror');515 });516}517function isError(err) {518 return err instanceof Error || (err && typeof err.message === 'string');519}520function thrown2Error(err) {521 return new Error(522 util.inspect(err) + ' was thrown, throw an Error :)'523 );524}525function extraGlobals() {526 if (typeof process === 'object' && typeof process.version === 'string') {527 var parts = process.version.split('.');528 var nodeVersion = parts.reduce(function(a, v) {529 return (a << 8) | v;530 });531 // 'errno' was renamed to process._errno in v0.9.11.532 if (nodeVersion < 0x00090b) {533 return ['errno'];534 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var should = require('should');2var assert = require('assert');3var foo = 'bar';4var beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };5describe('Test', function() {6 describe('Test', function() {7 it('should test something', function(done) {8 try {9 assert.equal(foo, 'bar');10 assert.equal(beverages, { tea: [ 'chai', 'matcha', 'oolong' ] });11 assert.ok('everything', 'everything is ok');12 assert.equal(null, undefined);13 } catch (e) {14 done(e);15 }16 });17 });18});

Full Screen

Using AI Code Generation

copy

Full Screen

1const { thrown2Error } = require('mocha');2const chai = require('chai');3const expect = chai.expect;4const chaiHttp = require('chai-http');5chai.use(chaiHttp);6const app = require('../app');7describe('Test', function () {8 it('should return 200 status code', function (done) {9 chai.request(app)10 .get('/')11 .end((err, res) => {12 if (err) {13 thrown2Error(err);14 }15 expect(res).to.have.status(200);16 done();17 });18 });19});

Full Screen

Using AI Code Generation

copy

Full Screen

1var assert = require('chai').assert;2var expect = require('chai').expect;3var should = require('chai').should();4var thrown2Error = require('chai').thrown2Error;5describe('test', function() {6 it('should throw an error', function() {7 expect(function() {8 throw new Error('Error!');9 }).to.throw2Error();10 });11});

Full Screen

Using AI Code Generation

copy

Full Screen

1var assert = require('assert');2var expect = require('chai').expect;3var should = require('chai').should();4describe('Test', function() {5 it('should throw an error', function() {6 expect(function() {7 throw new Error('error');8 }).to.throw('error');9 });10});11var assert = require('assert');12var expect = require('chai').expect;13var should = require('chai').should();14describe('Test', function() {15 it('should throw an error', function() {16 expect(function() {17 throw new Error('error');18 }).to.throw('error');19 });20});21var assert = require('assert');22var expect = require('chai').expect;23var should = require('chai').should();24describe('Test', function() {25 it('should throw an error', function() {26 expect(function() {27 throw new Error('error');28 }).to.throw('error');29 });30});31var assert = require('assert');32var expect = require('chai').expect;33var should = require('chai').should();34describe('Test', function() {35 it('should throw an error', function() {36 expect(function() {37 throw new Error('error');38 }).to.throw('error');39 });40});41var assert = require('assert');42var expect = require('chai').expect;43var should = require('chai').should();44describe('Test', function() {45 it('should throw an error', function() {46 expect(function() {47 throw new Error('error');48 }).to.throw('error');49 });50});51var assert = require('assert');52var expect = require('chai').expect;53var should = require('chai').should();54describe('Test', function() {55 it('should throw an error', function() {56 expect(function() {57 throw new Error('error');58 }).to.throw('error');59 });60});

Full Screen

Using AI Code Generation

copy

Full Screen

1const assert = require('assert');2const { test } = require('mocha');3function add(a, b) {4 return a + b;5}6it('should return 5 when add(2,3)', () => {7 assert.equal(add(2, 3), 5);8});9const assert = require('assert');10const { test } = require('mocha');11function add(a, b) {12 return a + b;13}14describe('Math', () => {15 before(() => {16 console.log('before');17 });18 beforeEach(() => {19 console.log('beforeEach');20 });21 after(() => {22 console.log('after');23 });24 afterEach(() => {25 console.log('afterEach');26 });27 it('should return 5 when add(2,3)', () => {28 assert.equal(add(2, 3), 5);29 });30 it('should return 5 when add(2,3)', () => {31 assert.equal(add(2, 3), 5);32 });33});34const assert = require('assert');35const { test } = require('mocha');36function add(a, b) {37 return a + b;38}39describe('Math', () => {40 it('should return 5 when add(2,3)', () => {41 assert.equal(add(2, 3), 5);42 });43 it('should return 5 when add(2,3)', () => {

Full Screen

Using AI Code Generation

copy

Full Screen

1var thrown2Error = require('mocha-thrown2error');2var chai = require('chai');3var expect = chai.expect;4describe('test', function () {5 it('should throw an error', function () {6 var error = thrown2Error(function () {7 throw new Error('test');8 });9 expect(error).to.be.an('error');10 expect(error.message).to.equal('test');11 });12});

Full Screen

Using AI Code Generation

copy

Full Screen

1const assert = require('assert');2const should = require('should');3const {expect} = require('chai');4const {add, subtract, multiply, divide} = require('../src/math');5describe('Math', () => {6 describe('add', () => {7 it('should add two numbers', () => {8 const result = add(1, 2);9 result.should.equal(3);10 });11 it('should throw an error if any argument is not a number', () => {12 expect(() => add('a', 2)).to.throw();13 });14 });15 describe('subtract', () => {16 it('should subtract two numbers', () => {17 const result = subtract(1, 2);18 result.should.equal(-1);19 });20 it('should throw an error if any argument is not a number', () => {21 expect(() => subtract('a', 2)).to.throw();22 });23 });24 describe('multiply', () => {25 it('should multiply two numbers', () => {26 const result = multiply(1, 2);27 result.should.equal(2);28 });29 it('should throw an error if any argument is not a number', () => {30 expect(() => multiply('a', 2)).to.throw();31 });32 });33 describe('divide', () => {34 it('should divide two numbers', () => {35 const result = divide(1, 2);36 result.should.equal(0.5);37 });38 it('should throw an error if any argument is not a number', () => {39 expect(() => divide('a', 2)).to.throw();40 });41 });42});43const add = (a, b) => {44 if (typeof a !== 'number' || typeof b !== 'number') {45 throw new Error('a and b must be numbers');46 }47 return a + b;48};49const subtract = (a, b) => {50 if (typeof a !== 'number' || typeof b !== 'number') {51 throw new Error('a and b must be numbers');52 }53 return a - b;54};55const multiply = (a, b) => {56 if (typeof a !== 'number' || typeof b !== 'number') {57 throw new Error('a and b must be numbers');58 }

Full Screen

Using AI Code Generation

copy

Full Screen

1const {thrown2Error} = require('mocha/lib/utils');2const {expect} = require('chai');3describe('thow2Error', () => {4 it('should throw an error', () => {5 const error = thrown2Error(() => {6 throw new Error('foo');7 });8 expect(error).to.be.an.instanceOf(Error);9 expect(error.message).to.equal('foo');10 });11 it('should throw an error with a string', () => {12 const error = thrown2Error(() => {13 throw 'foo';14 });15 expect(error).to.be.an.instanceOf(Error);16 expect(error.message).to.equal('foo');17 });18 it('should throw an error with a string and a function', () => {19 const error = thrown2Error(() => {20 throw 'foo';21 }, 'bar');22 expect(error).to.be.an.instanceOf(Error);23 expect(error.message).to.equal('bar: foo');24 });25 it('should throw an error with a string and a function and a string', () => {26 const error = thrown2Error(() => {27 throw 'foo';28 }, 'bar', 'baz');29 expect(error).to.be.an.instanceOf(Error);30 expect(error.message).to.equal('baz: bar: foo');31 });32});33const {expect} = require('chai');34const {thrown2Error} = require('mocha/lib/utils');35describe('thrown2Error', () => {36 it('should throw an error', () => {37 const error = thrown2Error(() => {38 throw new Error('foo');39 });40 expect(error).to.be.an.instanceOf(Error);41 expect(error.message).to.equal('foo');42 });43 it('should throw an error with a string', () => {44 const error = thrown2Error(() => {45 throw 'foo';46 });47 expect(error).to.be.an.instanceOf(Error);48 expect(error.message).to.equal('foo');49 });50 it('should throw an error with a string and a function', () => {51 const error = thrown2Error(() => {52 throw 'foo';53 }, 'bar');54 expect(error).to.be.an.instanceOf(Error);55 expect(error.message).to.equal('bar: foo');56 });57 it('should throw an error with a string and a function and a string', ()

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Mocha automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful