How to use getErrorWithLongStackTrace method in ava

Best JavaScript code snippet using ava

assert.js

Source:assert.js Github

copy

Full Screen

...47 this.statements = [];48 if (options.savedError) {49 this.savedError = options.savedError;50 } else {51 this.savedError = getErrorWithLongStackTrace();52 }53 }54}55exports.AssertionError = AssertionError;56function checkAssertionMessage(assertion, message) {57 if (typeof message === 'undefined' || typeof message === 'string') {58 return true;59 }60 return new AssertionError({61 assertion,62 improperUsage: true,63 message: 'The assertion message must be a string',64 values: [formatWithLabel('Called with:', message)]65 });66}67exports.checkAssertionMessage = checkAssertionMessage;68function getErrorWithLongStackTrace() {69 const limitBefore = Error.stackTraceLimit;70 Error.stackTraceLimit = Number.POSITIVE_INFINITY;71 const error = new Error(); // eslint-disable-line unicorn/error-message72 Error.stackTraceLimit = limitBefore;73 return error;74}75function validateExpectations(assertion, expectations, numberArgs) { // eslint-disable-line complexity76 if (numberArgs === 1 || expectations === null || expectations === undefined) {77 if (expectations === null) {78 throw new AssertionError({79 assertion,80 message: `The second argument to \`t.${assertion}()\` must be an expectation object or \`undefined\``,81 values: [formatWithLabel('Called with:', expectations)]82 });83 }84 expectations = {};85 } else if (86 typeof expectations === 'function' ||87 typeof expectations === 'string' ||88 expectations instanceof RegExp ||89 typeof expectations !== 'object' ||90 Array.isArray(expectations) ||91 Object.keys(expectations).length === 092 ) {93 throw new AssertionError({94 assertion,95 message: `The second argument to \`t.${assertion}()\` must be an expectation object, \`null\` or \`undefined\``,96 values: [formatWithLabel('Called with:', expectations)]97 });98 } else {99 if (hasOwnProperty(expectations, 'instanceOf') && typeof expectations.instanceOf !== 'function') {100 throw new AssertionError({101 assertion,102 message: `The \`instanceOf\` property of the second argument to \`t.${assertion}()\` must be a function`,103 values: [formatWithLabel('Called with:', expectations)]104 });105 }106 if (hasOwnProperty(expectations, 'message') && typeof expectations.message !== 'string' && !(expectations.message instanceof RegExp)) {107 throw new AssertionError({108 assertion,109 message: `The \`message\` property of the second argument to \`t.${assertion}()\` must be a string or regular expression`,110 values: [formatWithLabel('Called with:', expectations)]111 });112 }113 if (hasOwnProperty(expectations, 'name') && typeof expectations.name !== 'string') {114 throw new AssertionError({115 assertion,116 message: `The \`name\` property of the second argument to \`t.${assertion}()\` must be a string`,117 values: [formatWithLabel('Called with:', expectations)]118 });119 }120 if (hasOwnProperty(expectations, 'code') && typeof expectations.code !== 'string' && typeof expectations.code !== 'number') {121 throw new AssertionError({122 assertion,123 message: `The \`code\` property of the second argument to \`t.${assertion}()\` must be a string or number`,124 values: [formatWithLabel('Called with:', expectations)]125 });126 }127 for (const key of Object.keys(expectations)) {128 switch (key) {129 case 'instanceOf':130 case 'is':131 case 'message':132 case 'name':133 case 'code':134 continue;135 default:136 throw new AssertionError({137 assertion,138 message: `The second argument to \`t.${assertion}()\` contains unexpected properties`,139 values: [formatWithLabel('Called with:', expectations)]140 });141 }142 }143 }144 return expectations;145}146// Note: this function *must* throw exceptions, since it can be used147// as part of a pending assertion for promises.148function assertExpectations({assertion, actual, expectations, message, prefix, savedError}) {149 if (!isError(actual)) {150 throw new AssertionError({151 assertion,152 message,153 savedError,154 values: [formatWithLabel(`${prefix} exception that is not an error:`, actual)]155 });156 }157 const actualStack = actual.stack;158 if (hasOwnProperty(expectations, 'is') && actual !== expectations.is) {159 throw new AssertionError({160 assertion,161 message,162 savedError,163 actualStack,164 values: [165 formatWithLabel(`${prefix} unexpected exception:`, actual),166 formatWithLabel('Expected to be strictly equal to:', expectations.is)167 ]168 });169 }170 if (expectations.instanceOf && !(actual instanceof expectations.instanceOf)) {171 throw new AssertionError({172 assertion,173 message,174 savedError,175 actualStack,176 values: [177 formatWithLabel(`${prefix} unexpected exception:`, actual),178 formatWithLabel('Expected instance of:', expectations.instanceOf)179 ]180 });181 }182 if (typeof expectations.name === 'string' && actual.name !== expectations.name) {183 throw new AssertionError({184 assertion,185 message,186 savedError,187 actualStack,188 values: [189 formatWithLabel(`${prefix} unexpected exception:`, actual),190 formatWithLabel('Expected name to equal:', expectations.name)191 ]192 });193 }194 if (typeof expectations.message === 'string' && actual.message !== expectations.message) {195 throw new AssertionError({196 assertion,197 message,198 savedError,199 actualStack,200 values: [201 formatWithLabel(`${prefix} unexpected exception:`, actual),202 formatWithLabel('Expected message to equal:', expectations.message)203 ]204 });205 }206 if (expectations.message instanceof RegExp && !expectations.message.test(actual.message)) {207 throw new AssertionError({208 assertion,209 message,210 savedError,211 actualStack,212 values: [213 formatWithLabel(`${prefix} unexpected exception:`, actual),214 formatWithLabel('Expected message to match:', expectations.message)215 ]216 });217 }218 if (typeof expectations.code !== 'undefined' && actual.code !== expectations.code) {219 throw new AssertionError({220 assertion,221 message,222 savedError,223 actualStack,224 values: [225 formatWithLabel(`${prefix} unexpected exception:`, actual),226 formatWithLabel('Expected code to equal:', expectations.code)227 ]228 });229 }230}231class Assertions {232 constructor({233 pass = notImplemented,234 pending = notImplemented,235 fail = notImplemented,236 skip = notImplemented,237 compareWithSnapshot = notImplemented,238 powerAssert,239 experiments = {},240 disableSnapshots = false241 } = {}) {242 const withSkip = assertionFn => {243 assertionFn.skip = skip;244 return assertionFn;245 };246 // When adding new enhanced functions with new patterns, don't forget to247 // enable the pattern in the power-assert compilation step in @ava/babel.248 const withPowerAssert = (pattern, assertionFn) => powerAssert.empower(assertionFn, {249 onError: event => {250 if (event.powerAssertContext) {251 event.error.statements = powerAssert.format(event.powerAssertContext, formatPowerAssertValue);252 }253 fail(event.error);254 },255 onSuccess: () => {256 pass();257 },258 bindReceiver: false,259 patterns: [pattern]260 });261 const checkMessage = (assertion, message, powerAssert = false) => {262 const result = checkAssertionMessage(assertion, message);263 if (result === true) {264 return this.true;265 }266 if (powerAssert) {267 throw result;268 }269 fail(result);270 return false;271 };272 this.pass = withSkip(() => {273 pass();274 });275 this.fail = withSkip(message => {276 if (!checkMessage('fail', message)) {277 return;278 }279 fail(new AssertionError({280 assertion: 'fail',281 message: message || 'Test failed via `t.fail()`'282 }));283 });284 this.is = withSkip((actual, expected, message) => {285 if (!checkMessage('is', message)) {286 return;287 }288 if (Object.is(actual, expected)) {289 pass();290 } else {291 const result = concordance.compare(actual, expected, concordanceOptions);292 const actualDescriptor = result.actual || concordance.describe(actual, concordanceOptions);293 const expectedDescriptor = result.expected || concordance.describe(expected, concordanceOptions);294 if (result.pass) {295 fail(new AssertionError({296 assertion: 'is',297 message,298 raw: {actual, expected},299 values: [formatDescriptorWithLabel('Values are deeply equal to each other, but they are not the same:', actualDescriptor)]300 }));301 } else {302 fail(new AssertionError({303 assertion: 'is',304 message,305 raw: {actual, expected},306 values: [formatDescriptorDiff(actualDescriptor, expectedDescriptor)]307 }));308 }309 }310 });311 this.not = withSkip((actual, expected, message) => {312 if (!checkMessage('not', message)) {313 return;314 }315 if (Object.is(actual, expected)) {316 fail(new AssertionError({317 assertion: 'not',318 message,319 raw: {actual, expected},320 values: [formatWithLabel('Value is the same as:', actual)]321 }));322 } else {323 pass();324 }325 });326 this.deepEqual = withSkip((actual, expected, message) => {327 if (!checkMessage('deepEqual', message)) {328 return;329 }330 const result = concordance.compare(actual, expected, concordanceOptions);331 if (result.pass) {332 pass();333 } else {334 const actualDescriptor = result.actual || concordance.describe(actual, concordanceOptions);335 const expectedDescriptor = result.expected || concordance.describe(expected, concordanceOptions);336 fail(new AssertionError({337 assertion: 'deepEqual',338 message,339 raw: {actual, expected},340 values: [formatDescriptorDiff(actualDescriptor, expectedDescriptor)]341 }));342 }343 });344 this.notDeepEqual = withSkip((actual, expected, message) => {345 if (!checkMessage('notDeepEqual', message)) {346 return;347 }348 const result = concordance.compare(actual, expected, concordanceOptions);349 if (result.pass) {350 const actualDescriptor = result.actual || concordance.describe(actual, concordanceOptions);351 fail(new AssertionError({352 assertion: 'notDeepEqual',353 message,354 raw: {actual, expected},355 values: [formatDescriptorWithLabel('Value is deeply equal:', actualDescriptor)]356 }));357 } else {358 pass();359 }360 });361 this.like = withSkip((actual, selector, message) => {362 if (!checkMessage('like', message)) {363 return;364 }365 if (!isLikeSelector(selector)) {366 fail(new AssertionError({367 assertion: 'like',368 improperUsage: true,369 message: '`t.like()` selector must be a non-empty object',370 values: [formatWithLabel('Called with:', selector)]371 }));372 return;373 }374 let comparable;375 try {376 comparable = selectComparable(actual, selector);377 } catch (error) {378 if (error === CIRCULAR_SELECTOR) {379 fail(new AssertionError({380 assertion: 'like',381 improperUsage: true,382 message: '`t.like()` selector must not contain circular references',383 values: [formatWithLabel('Called with:', selector)]384 }));385 return;386 }387 throw error;388 }389 const result = concordance.compare(comparable, selector, concordanceOptions);390 if (result.pass) {391 pass();392 } else {393 const actualDescriptor = result.actual || concordance.describe(comparable, concordanceOptions);394 const expectedDescriptor = result.expected || concordance.describe(selector, concordanceOptions);395 fail(new AssertionError({396 assertion: 'like',397 message,398 values: [formatDescriptorDiff(actualDescriptor, expectedDescriptor)]399 }));400 }401 });402 this.throws = withSkip((...args) => {403 // Since arrow functions do not support 'arguments', we are using rest404 // operator, so we can determine the total number of arguments passed405 // to the function.406 let [fn, expectations, message] = args;407 if (!checkMessage('throws', message)) {408 return;409 }410 if (typeof fn !== 'function') {411 fail(new AssertionError({412 assertion: 'throws',413 improperUsage: true,414 message: '`t.throws()` must be called with a function',415 values: [formatWithLabel('Called with:', fn)]416 }));417 return;418 }419 try {420 expectations = validateExpectations('throws', expectations, args.length, experiments);421 } catch (error) {422 fail(error);423 return;424 }425 let retval;426 let actual = null;427 try {428 retval = fn();429 if (isPromise(retval)) {430 // Here isPromise() checks if something is "promise like". Cast to an actual promise.431 Promise.resolve(retval).catch(noop);432 fail(new AssertionError({433 assertion: 'throws',434 message,435 values: [formatWithLabel('Function returned a promise. Use `t.throwsAsync()` instead:', retval)]436 }));437 return;438 }439 } catch (error) {440 actual = error;441 }442 if (!actual) {443 fail(new AssertionError({444 assertion: 'throws',445 message,446 values: [formatWithLabel('Function returned:', retval)]447 }));448 return;449 }450 try {451 assertExpectations({452 assertion: 'throws',453 actual,454 expectations,455 message,456 prefix: 'Function threw'457 });458 pass();459 return actual;460 } catch (error) {461 fail(error);462 }463 });464 this.throwsAsync = withSkip((...args) => {465 let [thrower, expectations, message] = args;466 if (!checkMessage('throwsAsync', message)) {467 return Promise.resolve();468 }469 if (typeof thrower !== 'function' && !isPromise(thrower)) {470 fail(new AssertionError({471 assertion: 'throwsAsync',472 improperUsage: true,473 message: '`t.throwsAsync()` must be called with a function or promise',474 values: [formatWithLabel('Called with:', thrower)]475 }));476 return Promise.resolve();477 }478 try {479 expectations = validateExpectations('throwsAsync', expectations, args.length, experiments);480 } catch (error) {481 fail(error);482 return Promise.resolve();483 }484 const handlePromise = (promise, wasReturned) => {485 // Create an error object to record the stack before it gets lost in the promise chain.486 const savedError = getErrorWithLongStackTrace();487 // Handle "promise like" objects by casting to a real Promise.488 const intermediate = Promise.resolve(promise).then(value => { // eslint-disable-line promise/prefer-await-to-then489 throw new AssertionError({490 assertion: 'throwsAsync',491 message,492 savedError,493 values: [formatWithLabel(`${wasReturned ? 'Returned promise' : 'Promise'} resolved with:`, value)]494 });495 }, error => {496 assertExpectations({497 assertion: 'throwsAsync',498 actual: error,499 expectations,500 message,501 prefix: `${wasReturned ? 'Returned promise' : 'Promise'} rejected with`,502 savedError503 });504 return error;505 });506 pending(intermediate);507 // Don't reject the returned promise, even if the assertion fails.508 return intermediate.catch(noop);509 };510 if (isPromise(thrower)) {511 return handlePromise(thrower, false);512 }513 let retval;514 let actual = null;515 try {516 retval = thrower();517 } catch (error) {518 actual = error;519 }520 if (actual) {521 fail(new AssertionError({522 assertion: 'throwsAsync',523 message,524 actualStack: actual.stack,525 values: [formatWithLabel('Function threw synchronously. Use `t.throws()` instead:', actual)]526 }));527 return Promise.resolve();528 }529 if (isPromise(retval)) {530 return handlePromise(retval, true);531 }532 fail(new AssertionError({533 assertion: 'throwsAsync',534 message,535 values: [formatWithLabel('Function returned:', retval)]536 }));537 return Promise.resolve();538 });539 this.notThrows = withSkip((fn, message) => {540 if (!checkMessage('notThrows', message)) {541 return;542 }543 if (typeof fn !== 'function') {544 fail(new AssertionError({545 assertion: 'notThrows',546 improperUsage: true,547 message: '`t.notThrows()` must be called with a function',548 values: [formatWithLabel('Called with:', fn)]549 }));550 return;551 }552 try {553 fn();554 } catch (error) {555 fail(new AssertionError({556 assertion: 'notThrows',557 message,558 actualStack: error.stack,559 values: [formatWithLabel('Function threw:', error)]560 }));561 return;562 }563 pass();564 });565 this.notThrowsAsync = withSkip((nonThrower, message) => {566 if (!checkMessage('notThrowsAsync', message)) {567 return Promise.resolve();568 }569 if (typeof nonThrower !== 'function' && !isPromise(nonThrower)) {570 fail(new AssertionError({571 assertion: 'notThrowsAsync',572 improperUsage: true,573 message: '`t.notThrowsAsync()` must be called with a function or promise',574 values: [formatWithLabel('Called with:', nonThrower)]575 }));576 return Promise.resolve();577 }578 const handlePromise = (promise, wasReturned) => {579 // Create an error object to record the stack before it gets lost in the promise chain.580 const savedError = getErrorWithLongStackTrace();581 // Handle "promise like" objects by casting to a real Promise.582 const intermediate = Promise.resolve(promise).then(noop, error => { // eslint-disable-line promise/prefer-await-to-then583 throw new AssertionError({584 assertion: 'notThrowsAsync',585 message,586 savedError,587 values: [formatWithLabel(`${wasReturned ? 'Returned promise' : 'Promise'} rejected with:`, error)]588 });589 });590 pending(intermediate);591 // Don't reject the returned promise, even if the assertion fails.592 return intermediate.catch(noop);593 };594 if (isPromise(nonThrower)) {...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import test from 'ava';2import getErrorWithLongStackTrace from 'ava/lib/get-error-with-long-stack-trace';3test('my passing test', t => {4 t.pass();5});6test('my failing test', t => {7 t.fail();8});9test('my failing test with long stack trace', t => {10 const error = getErrorWithLongStackTrace();11 t.fail(error);12});13When using the [`esm` package](

Full Screen

Using AI Code Generation

copy

Full Screen

1const test = require('ava');2const getErrorWithLongStackTrace = require('ava/lib/get-error-with-long-stack-trace');3test('test', t => {4 const error = new Error('foo');5 error.stack = 'bar';6 t.throws(() => {7 throw getErrorWithLongStackTrace(error);8 });9});10const test = require('ava');11const getErrorWithLongStackTrace = require('ava/lib/get-error-with-long-stack-trace');12test('test', t => {13 const error = new Error('foo');14 error.stack = 'bar';15 t.throws(() => {16 throw getErrorWithLongStackTrace(error);17 });18});19const test = require('ava');20const getErrorWithLongStackTrace = require('ava/lib/get-error-with-long-stack-trace');21test('test', t => {22 const error = new Error('foo');23 error.stack = 'bar';24 t.throws(() => {25 throw getErrorWithLongStackTrace(error);26 });27});28const test = require('ava');29const getErrorWithLongStackTrace = require('ava/lib/get-error-with-long-stack-trace');30test('test', t => {31 const error = new Error('foo');32 error.stack = 'bar';33 t.throws(() => {34 throw getErrorWithLongStackTrace(error);35 });36});

Full Screen

Using AI Code Generation

copy

Full Screen

1import test from 'ava';2import getErrorWithLongStackTrace from 'ava/lib/get-error-with-long-stack-trace';3function fn() {4 return Promise.reject(new Error('foo'));5}6test(t => {7 return fn().catch(getErrorWithLongStackTrace).then(err => {8 t.is(err.message, 'foo');9 });10});

Full Screen

Using AI Code Generation

copy

Full Screen

1import test from 'ava';2import getErrorWithLongStackTrace from 'ava/lib/get-error-with-long-stack-trace';3test('test', t => {4 const error = new Error('error');5 error.stack = 'stack';6 t.throws(() => {7 throw getErrorWithLongStackTrace(error);8 }, 'error');9});

Full Screen

Using AI Code Generation

copy

Full Screen

1import test from 'ava';2import { getErrorWithLongStackTrace } from 'ava/lib/worker/serialization';3const err = new Error('foo');4test('foo', t => {5 t.is(getErrorWithLongStackTrace(err).stack, err.stack);6});7### getErrorWithLongStackTrace(error)8- [ava](

Full Screen

Using AI Code Generation

copy

Full Screen

1import test from 'ava';2import { getErrorWithLongStackTrace } from 'ava/lib/assert';3test('test', t => {4 const err = new Error('foo');5 const errWithLongStackTrace = getErrorWithLongStackTrace(err);6 t.is(errWithLongStackTrace.stack, err.stack);7});8MIT © [Vadim Demedes](

Full Screen

Using AI Code Generation

copy

Full Screen

1const { getErrorWithLongStackTrace } = require('ava/lib/worker/serialization');2const { test } = require('ava');3test('test', t => {4 const err = getErrorWithLongStackTrace(new Error('foo'));5 console.log(err.stack);6});7MIT © [Kamalakannan](

Full Screen

Using AI Code Generation

copy

Full Screen

1import { getErrorWithLongStackTrace } from 'ava/lib/worker/child'2import { createRequire } from 'module'3const require = createRequire(import.meta.url)4const { test } = require('ava')5test('test', t => {6 const error = new Error()7 const errorWithStack = getErrorWithLongStackTrace(error)8 t.is(error, errorWithStack)9})10import test from 'ava'11test('test', t => {12 const error = new Error()13 t.is(error, error)14})15 Error {16 }17 › Object.<anonymous> (test.js:3:16)18 › test (node_modules/ava/lib/test.js:25:9)19 › ava (node_modules/ava/lib/runner.js:45:25)20 › Object.<anonymous> (test.js:1:1)21 › Module._compile (node:internal/modules/cjs/loader:1108:14)22 › Module._compile (node_modules/pirates/lib/index.js:99:24)23 › Module._extensions..js (node:internal/modules/cjs/loader:1137:10)24 › Object.newLoader [as .js] (node_modules/pirates/lib/index.js:104:7)25 › Module.load (node:internal/modules/cjs/loader:973:32)26 › Function.Module._load (node:internal/modules/cjs/loader:813:14)27 › Module.require (node:internal/modules/cjs/loader:996:19)28 › require (node:internal/modules/cjs/helpers:92:18)29 › Object.<anonymous> (test.js:1:1)30 › Module._compile (node:internal/modules/cjs/loader:1108:14)31 › Module._compile (node_modules/pirates/lib/index.js:99:24)32 › Module._extensions..js (node:internal/modules/cjs/loader:1137:10)33 › Object.newLoader [as .js] (node_modules/pirates/lib/index.js:104:7)34 › Module.load (node:internal/modules/cjs/loader:973:32)35 › Function.Module._load (node:internal/modules/cjs/loader:813

Full Screen

Using AI Code Generation

copy

Full Screen

1import { getErrorWithLongStackTrace } from 'ava/lib/worker/serialization';2import { readFileSync } from 'fs';3import { join } from 'path';4const error = new Error('foo');5const longStack = getErrorWithLongStackTrace(error);6console.log(longStack.stack);

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 ava 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