Best JavaScript code snippet using ava
assert.js
Source:assert.js
...85 if (expectBoolean) {86 t.notOk(retval);87 }88}89function throwsAsyncFails(t, fn, subset) {90 return add(() => {91 lastFailure = null;92 return fn().then(retval => {93 t.equal(retval, undefined);94 assertFailure(t, subset);95 });96 });97}98function notThrowsAsyncFails(t, fn, subset) {99 return add(() => {100 lastFailure = null;101 return fn().then(retval => {102 t.equal(retval, undefined);103 assertFailure(t, subset);104 });105 });106}107function fails(t, fn) {108 lastFailure = null;109 const retval = fn();110 if (lastFailure) {111 t.notOk(retval);112 } else {113 t.fail('Expected assertion to fail');114 }115}116function passes(t, fn, {expectBoolean = true} = {}) {117 lastPassed = false;118 lastFailure = null;119 const retval = fn();120 if (lastPassed) {121 if (expectBoolean) {122 t.ok(retval);123 } else {124 t.pass();125 }126 } else {127 t.error(lastFailure, 'Expected assertion to pass');128 }129}130function throwsAsyncPasses(t, fn) {131 return add(() => {132 lastPassed = false;133 lastFailure = null;134 return fn().then(() => {135 if (lastPassed) {136 t.pass();137 } else {138 t.error(lastFailure, 'Expected assertion to pass');139 }140 });141 });142}143const notThrowsAsyncPasses = throwsAsyncPasses;144test('.pass()', t => {145 passes(t, () => assertions.pass());146 passes(t, () => assertions.pass());147 t.end();148});149test('.fail()', t => {150 failsWith(t, () => assertions.fail(), {151 assertion: 'fail',152 message: 'Test failed via `t.fail()`',153 });154 failsWith(t, () => assertions.fail('my message'), {155 assertion: 'fail',156 message: 'my message',157 });158 failsWith(t, () => assertions.fail(), {159 assertion: 'fail',160 message: 'Test failed via `t.fail()`',161 });162 failsWith(t, () => assertions.fail(null), {163 assertion: 'fail',164 improperUsage: true,165 message: 'The assertion message must be a string',166 values: [{167 label: 'Called with:',168 formatted: /null/,169 }],170 });171 t.end();172});173test('.is()', t => {174 passes(t, () => assertions.is('foo', 'foo'));175 passes(t, () => assertions.is('foo', 'foo'));176 passes(t, () => assertions.is('', ''));177 passes(t, () => assertions.is(true, true));178 passes(t, () => assertions.is(false, false));179 passes(t, () => assertions.is(null, null));180 passes(t, () => assertions.is(undefined, undefined));181 passes(t, () => assertions.is(1, 1));182 passes(t, () => assertions.is(0, 0));183 passes(t, () => assertions.is(-0, -0));184 passes(t, () => assertions.is(Number.NaN, Number.NaN));185 passes(t, () => assertions.is(0 / 0, Number.NaN));186 passes(t, () => {187 const someRef = {foo: 'bar'};188 return assertions.is(someRef, someRef);189 });190 fails(t, () => assertions.is(0, -0));191 fails(t, () => assertions.is(0, false));192 fails(t, () => assertions.is('', false));193 fails(t, () => assertions.is('0', 0));194 fails(t, () => assertions.is('17', 17));195 fails(t, () => assertions.is([1, 2], '1,2'));196 fails(t, () =>197 // eslint-disable-next-line no-new-wrappers, unicorn/new-for-builtins198 assertions.is(new String('foo'), 'foo'),199 );200 fails(t, () => assertions.is(null, undefined));201 fails(t, () => assertions.is(null, false));202 fails(t, () => assertions.is(undefined, false));203 fails(t, () =>204 // eslint-disable-next-line no-new-wrappers, unicorn/new-for-builtins205 assertions.is(new String('foo'), new String('foo')),206 );207 fails(t, () => assertions.is(0, null));208 fails(t, () => assertions.is(0, Number.NaN));209 fails(t, () => assertions.is('foo', Number.NaN));210 failsWith(t, () => assertions.is({foo: 'bar'}, {foo: 'bar'}), {211 assertion: 'is',212 message: '',213 actual: {foo: 'bar'},214 expected: {foo: 'bar'},215 values: [{216 label: 'Values are deeply equal to each other, but they are not the same:',217 formatted: /foo/,218 }],219 });220 failsWith(t, () => assertions.is('foo', 'bar'), {221 assertion: 'is',222 message: '',223 raw: {actual: 'foo', expected: 'bar'},224 values: [225 {label: 'Difference:', formatted: /- 'foo'\n\+ 'bar'/},226 ],227 });228 failsWith(t, () => assertions.is('foo', 42), {229 actual: 'foo',230 assertion: 'is',231 expected: 42,232 message: '',233 values: [234 {label: 'Difference:', formatted: /- 'foo'\n\+ 42/},235 ],236 });237 failsWith(t, () => assertions.is('foo', 42, 'my message'), {238 assertion: 'is',239 message: 'my message',240 values: [241 {label: 'Difference:', formatted: /- 'foo'\n\+ 42/},242 ],243 });244 failsWith(t, () => assertions.is(0, -0, 'my message'), {245 assertion: 'is',246 message: 'my message',247 values: [248 {label: 'Difference:', formatted: /- 0\n\+ -0/},249 ],250 });251 failsWith(t, () => assertions.is(-0, 0, 'my message'), {252 assertion: 'is',253 message: 'my message',254 values: [255 {label: 'Difference:', formatted: /- -0\n\+ 0/},256 ],257 });258 failsWith(t, () => assertions.is(0, 0, null), {259 assertion: 'is',260 improperUsage: true,261 message: 'The assertion message must be a string',262 values: [{263 label: 'Called with:',264 formatted: /null/,265 }],266 });267 t.end();268});269test('.not()', t => {270 passes(t, () => assertions.not('foo', 'bar'));271 passes(t, () => assertions.not('foo', 'bar'));272 fails(t, () => assertions.not(Number.NaN, Number.NaN));273 fails(t, () => assertions.not(0 / 0, Number.NaN));274 failsWith(t, () => assertions.not('foo', 'foo'), {275 assertion: 'not',276 message: '',277 raw: {actual: 'foo', expected: 'foo'},278 values: [{label: 'Value is the same as:', formatted: /foo/}],279 });280 failsWith(t, () => assertions.not('foo', 'foo', 'my message'), {281 assertion: 'not',282 message: 'my message',283 values: [{label: 'Value is the same as:', formatted: /foo/}],284 });285 failsWith(t, () => assertions.not(0, 1, null), {286 assertion: 'not',287 improperUsage: true,288 message: 'The assertion message must be a string',289 values: [{290 label: 'Called with:',291 formatted: /null/,292 }],293 });294 t.end();295});296test('.deepEqual()', t => {297 // Tests starting here are to detect regressions in the underlying libraries298 // used to test deep object equality299 fails(t, () => assertions.deepEqual({a: false}, {a: 0}));300 passes(t, () => assertions.deepEqual({301 a: 'a',302 b: 'b',303 }, {304 b: 'b',305 a: 'a',306 }));307 passes(t, () => assertions.deepEqual({a: 'a', b: 'b'}, {b: 'b', a: 'a'}));308 passes(t, () => assertions.deepEqual({309 a: 'a',310 b: 'b',311 c: {312 d: 'd',313 },314 }, {315 c: {316 d: 'd',317 },318 b: 'b',319 a: 'a',320 }));321 fails(t, () => assertions.deepEqual([1, 2, 3], [1, 2, 3, 4]));322 passes(t, () => assertions.deepEqual([1, 2, 3], [1, 2, 3]));323 fails(t, () => {324 const fnA = a => a;325 const fnB = a => a;326 return assertions.deepEqual(fnA, fnB);327 });328 passes(t, () => {329 const x1 = {z: 4};330 const y1 = {x: x1};331 x1.y = y1;332 const x2 = {z: 4};333 const y2 = {x: x2};334 x2.y = y2;335 return assertions.deepEqual(x1, x2);336 });337 passes(t, () => {338 function Foo(a) {339 this.a = a;340 }341 const x = new Foo(1);342 const y = new Foo(1);343 return assertions.deepEqual(x, y);344 });345 fails(t, () => {346 function Foo(a) {347 this.a = a;348 }349 function Bar(a) {350 this.a = a;351 }352 const x = new Foo(1);353 const y = new Bar(1);354 return assertions.deepEqual(x, y);355 });356 fails(t, () => assertions.deepEqual({357 a: 'a',358 b: 'b',359 c: {360 d: false,361 },362 }, {363 c: {364 d: 0,365 },366 b: 'b',367 a: 'a',368 }));369 fails(t, () => assertions.deepEqual({}, []));370 fails(t, () => assertions.deepEqual({0: 'a', 1: 'b'}, ['a', 'b']));371 fails(t, () => assertions.deepEqual({a: 1}, {a: 1, b: undefined}));372 fails(t, () => assertions.deepEqual(new Date('1972-08-01'), null));373 fails(t, () => assertions.deepEqual(new Date('1972-08-01'), undefined));374 passes(t, () => assertions.deepEqual(new Date('1972-08-01'), new Date('1972-08-01')));375 passes(t, () => assertions.deepEqual({x: new Date('1972-08-01')}, {x: new Date('1972-08-01')}));376 fails(t, () => assertions.deepEqual(() => {}, () => {}));377 passes(t, () => assertions.deepEqual(undefined, undefined)378 && assertions.deepEqual({x: undefined}, {x: undefined})379 && assertions.deepEqual({x: [undefined]}, {x: [undefined]}));380 passes(t, () => assertions.deepEqual(null, null)381 && assertions.deepEqual({x: null}, {x: null})382 && assertions.deepEqual({x: [null]}, {x: [null]}));383 passes(t, () => assertions.deepEqual(0, 0)384 && assertions.deepEqual(1, 1)385 && assertions.deepEqual(3.14, 3.14));386 fails(t, () => assertions.deepEqual(0, 1));387 fails(t, () => assertions.deepEqual(1, -1));388 fails(t, () => assertions.deepEqual(3.14, 2.72));389 fails(t, () => assertions.deepEqual({0: 'a', 1: 'b'}, ['a', 'b']));390 passes(t, () => assertions.deepEqual(391 [392 {foo: {z: 100, y: 200, x: 300}},393 'bar',394 11,395 {baz: {d: 4, a: 1, b: 2, c: 3}},396 ],397 [398 {foo: {x: 300, y: 200, z: 100}},399 'bar',400 11,401 {baz: {c: 3, b: 2, a: 1, d: 4}},402 ],403 ));404 passes(t, () => assertions.deepEqual(405 {x: {a: 1, b: 2}, y: {c: 3, d: 4}},406 {y: {d: 4, c: 3}, x: {b: 2, a: 1}},407 ));408 // Regression test end here409 passes(t, () => assertions.deepEqual({a: 'a'}, {a: 'a'}));410 passes(t, () => assertions.deepEqual(['a', 'b'], ['a', 'b']));411 fails(t, () => assertions.deepEqual({a: 'a'}, {a: 'b'}));412 fails(t, () => assertions.deepEqual(['a', 'b'], ['a', 'a']));413 fails(t, () => assertions.deepEqual([['a', 'b'], 'c'], [['a', 'b'], 'd']));414 fails(t, () => {415 const circular = ['a', 'b'];416 circular.push(circular);417 return assertions.deepEqual([circular, 'c'], [circular, 'd']);418 });419 failsWith(t, () => assertions.deepEqual('foo', 'bar'), {420 assertion: 'deepEqual',421 message: '',422 raw: {actual: 'foo', expected: 'bar'},423 values: [{label: 'Difference:', formatted: /- 'foo'\n\+ 'bar'/}],424 });425 failsWith(t, () => assertions.deepEqual('foo', 42), {426 assertion: 'deepEqual',427 message: '',428 raw: {actual: 'foo', expected: 42},429 values: [{label: 'Difference:', formatted: /- 'foo'\n\+ 42/}],430 });431 failsWith(t, () => assertions.deepEqual('foo', 42, 'my message'), {432 assertion: 'deepEqual',433 message: 'my message',434 values: [{label: 'Difference:', formatted: /- 'foo'\n\+ 42/}],435 });436 failsWith(t, () => assertions.deepEqual({}, {}, null), {437 assertion: 'deepEqual',438 improperUsage: true,439 message: 'The assertion message must be a string',440 values: [{441 label: 'Called with:',442 formatted: /null/,443 }],444 });445 t.end();446});447test('.notDeepEqual()', t => {448 passes(t, () => assertions.notDeepEqual({a: 'a'}, {a: 'b'}));449 passes(t, () => assertions.notDeepEqual({a: 'a'}, {a: 'b'}));450 passes(t, () => assertions.notDeepEqual(['a', 'b'], ['c', 'd']));451 const actual = {a: 'a'};452 const expected = {a: 'a'};453 failsWith(t, () => assertions.notDeepEqual(actual, expected), {454 actual,455 assertion: 'notDeepEqual',456 expected,457 message: '',458 raw: {actual, expected},459 values: [{label: 'Value is deeply equal:', formatted: /.*{.*\n.*a: 'a'/}],460 });461 failsWith(t, () => assertions.notDeepEqual(['a', 'b'], ['a', 'b'], 'my message'), {462 assertion: 'notDeepEqual',463 message: 'my message',464 values: [{label: 'Value is deeply equal:', formatted: /.*\[.*\n.*'a',\n.*'b',/}],465 });466 failsWith(t, () => assertions.notDeepEqual({}, [], null), {467 assertion: 'notDeepEqual',468 improperUsage: true,469 message: 'The assertion message must be a string',470 values: [{471 label: 'Called with:',472 formatted: /null/,473 }],474 });475 t.end();476});477test('.like()', t => {478 fails(t, () => assertions.like({a: false}, {a: 0}));479 passes(t, () => assertions.like({480 a: 'a',481 b: 'b',482 }, {483 b: 'b',484 a: 'a',485 }));486 passes(t, () => assertions.like({a: 'a', b: 'b'}, {b: 'b', a: 'a'}));487 passes(t, () => assertions.like({488 a: 'a',489 b: 'b',490 c: {491 d: 'd',492 x: 'x',493 },494 x: 'x',495 }, {496 c: {497 d: 'd',498 },499 b: 'b',500 a: 'a',501 }));502 fails(t, () => assertions.like([1, 2, 3], [1, 2, 3, 4]));503 fails(t, () => assertions.like({504 a: [1, 2, 3],505 }, {506 a: [1, 2, 3, 4],507 }));508 passes(t, () => assertions.like({509 a: [1, 2, 3],510 x: 'x',511 }, {512 a: [1, 2, 3],513 }));514 passes(t, () => {515 const actual = {516 a: 'a',517 extra: 'irrelevant',518 };519 actual.circular = actual;520 const likePattern = {521 a: 'a',522 };523 return assertions.like(actual, likePattern);524 });525 fails(t, () => {526 const fnA = a => a;527 const fnB = a => a;528 return assertions.like(fnA, fnB);529 });530 fails(t, () => {531 const fnA = a => a;532 const fnB = a => a;533 return assertions.like({534 fn: fnA,535 }, {536 fn: fnB,537 });538 });539 fails(t, () => {540 function Foo(a) {541 this.a = a;542 }543 function Bar(a) {544 this.a = a;545 }546 const x = new Foo(1);547 const y = new Bar(1);548 return assertions.like(x, y);549 });550 passes(t, () => assertions.like({a: 'a'}, {a: 'a'}));551 passes(t, () => assertions.like({a: 'a', b: 'b'}, {a: 'a'}));552 passes(t, () => assertions.like({ab: ['a', 'b']}, {ab: ['a', 'b']}));553 passes(t, () => assertions.like({ab: ['a', 'b'], c: 'c'}, {ab: ['a', 'b']}));554 fails(t, () => assertions.like({a: 'a'}, {a: 'b'}));555 fails(t, () => assertions.like({a: 'a', b: 'b'}, {a: 'b'}));556 fails(t, () => assertions.like({ab: ['a', 'b']}, {ab: ['a', 'a']}));557 fails(t, () => assertions.like({ab: ['a', 'b'], c: 'c'}, {ab: ['a', 'a']}));558 fails(t, () => assertions.like([['a', 'b'], 'c'], [['a', 'b'], 'd']));559 fails(t, () => {560 const circular = ['a', 'b'];561 circular.push(circular);562 return assertions.like([circular, 'c'], [circular, 'd']);563 });564 fails(t, () => {565 const circular = ['a', 'b'];566 circular.push(circular);567 return assertions.like({xc: [circular, 'c']}, {xc: [circular, 'd']});568 });569 failsWith(t, () => assertions.like({a: 'a'}, {}), {570 assertion: 'like',571 message: '`t.like()` selector must be a non-empty object',572 values: [{label: 'Called with:', formatted: '{}'}],573 });574 failsWith(t, () => assertions.like('foo', 'bar'), {575 assertion: 'like',576 message: '`t.like()` selector must be a non-empty object',577 values: [{label: 'Called with:', formatted: '\'bar\''}],578 });579 failsWith(t, () => {580 const likePattern = {581 a: 'a',582 };583 likePattern.circular = likePattern;584 return assertions.like({}, likePattern);585 }, {586 assertion: 'like',587 message: '`t.like()` selector must not contain circular references',588 values: [{label: 'Called with:', formatted: '{\n a: \'a\',\n circular: [Circular],\n}'}],589 });590 failsWith(t, () => assertions.like({}, {}, null), {591 assertion: 'like',592 improperUsage: true,593 message: 'The assertion message must be a string',594 values: [{595 label: 'Called with:',596 formatted: /null/,597 }],598 });599 failsWith(t, () => assertions.like({a: 'foo', b: 'irrelevant'}, {a: 'bar'}), {600 assertion: 'like',601 message: '',602 values: [{label: 'Difference:', formatted: /{\n-\s*a: 'foo',\n\+\s*a: 'bar',\n\s*}/}],603 });604 t.end();605});606test('.throws()', gather(t => {607 // Fails because function doesn't throw.608 failsWith(t, () => assertions.throws(() => {}), {609 assertion: 'throws',610 message: '',611 values: [{label: 'Function returned:', formatted: /undefined/}],612 });613 failsWith(t, () => assertions.throws(() => {}), {614 assertion: 'throws',615 message: '',616 values: [{label: 'Function returned:', formatted: /undefined/}],617 });618 // Fails because function doesn't throw. Asserts that 'my message' is used619 // as the assertion message (*not* compared against the error).620 failsWith(t, () => assertions.throws(() => {}, undefined, 'my message'), {621 assertion: 'throws',622 message: 'my message',623 values: [{label: 'Function returned:', formatted: /undefined/}],624 });625 // Fails because the function returned a promise.626 failsWith(t, () => assertions.throws(() => Promise.resolve()), {627 assertion: 'throws',628 message: '',629 values: [{label: 'Function returned a promise. Use `t.throwsAsync()` instead:', formatted: /Promise/}],630 });631 // Fails because thrown exception is not an error632 failsWith(t, () => assertions.throws(() => {633 const error = 'foo';634 throw error;635 }), {636 assertion: 'throws',637 message: '',638 values: [639 {label: 'Function threw exception that is not an error:', formatted: /'foo'/},640 ],641 });642 // Passes because an error is thrown.643 passes(t, () => assertions.throws(() => {644 throw new Error('foo');645 }));646 // Passes because the correct error is thrown.647 passes(t, () => {648 const error = new Error('foo');649 return assertions.throws(() => {650 throw error;651 }, {is: error});652 });653 // Fails because the thrown value is not an error654 fails(t, () => {655 const object = {};656 return assertions.throws(() => {657 throw object;658 }, {is: object});659 });660 // Fails because the thrown value is not the right one661 fails(t, () => {662 const error = new Error('foo');663 return assertions.throws(() => {664 throw error;665 }, {is: {}});666 });667 // Passes because the correct error is thrown.668 passes(t, () => assertions.throws(() => {669 throw new TypeError();670 }, {name: 'TypeError'}));671 // Fails because the thrown value is not an error672 fails(t, () => assertions.throws(() => {673 const error = {name: 'Bob'};674 throw error;675 }, {name: 'Bob'}));676 // Fails because the thrown value is not the right one677 fails(t, () => assertions.throws(() => {678 throw new Error('foo');679 }, {name: 'TypeError'}));680 // Passes because the correct error is thrown.681 passes(t, () => assertions.throws(() => {682 const error = new TypeError();683 error.code = 'ERR_TEST';684 throw error;685 }, {code: 'ERR_TEST'}));686 // Passes because the correct error is thrown.687 passes(t, () => assertions.throws(() => {688 const error = new TypeError();689 error.code = 42;690 throw error;691 }, {code: 42}));692 // Fails because the thrown value is not the right one693 fails(t, () => assertions.throws(() => {694 const error = new TypeError();695 error.code = 'ERR_NOPE';696 throw error;697 }, {code: 'ERR_TEST'}));698 fails(t, () => assertions.throws(() => {699 const error = new TypeError();700 error.code = 1;701 throw error;702 }, {code: 42}));703 // Regression test for https://github.com/avajs/ava/issues/1676704 fails(t, () => assertions.throws(() => {705 throw new Error('foo');706 }, false));707 passes(t, () => assertions.throws(() => {708 throw new Error('foo');709 }, undefined));710 passes(t, async () => {711 await assertions.throwsAsync(() => Promise.reject(new Error('foo')), undefined);712 });713 failsWith(t, () => assertions.throws(() => {}, undefined, null), {714 assertion: 'throws',715 improperUsage: true,716 message: 'The assertion message must be a string',717 values: [{718 label: 'Called with:',719 formatted: /null/,720 }],721 });722 // Fails because the string in the message is incorrect723 failsWith(724 t,725 () =>726 assertions.throws(727 () => {728 throw new Error('error');729 },730 {message: 'my error'},731 ),732 {733 assertion: 'throws',734 message: '',735 values: [736 {label: 'Function threw unexpected exception:', formatted: /error/},737 {label: 'Expected message to equal:', formatted: /my error/},738 ],739 },740 );741 passes(t, () => assertions.throws(() => {742 throw new Error('error');743 }, {message: 'error'}));744 // Fails because the regular expression in the message is incorrect745 failsWith(746 t,747 () =>748 assertions.throws(749 () => {750 throw new Error('error');751 },752 {message: /my error/},753 ),754 {755 assertion: 'throws',756 message: '',757 values: [758 {label: 'Function threw unexpected exception:', formatted: /error/},759 {label: 'Expected message to match:', formatted: /my error/},760 ],761 },762 );763 passes(t, () => assertions.throws(() => {764 throw new Error('error');765 }, {message: /error/}));766 // Fails because the function in the message returns false767 failsWith(768 t,769 () =>770 assertions.throws(771 () => {772 throw new Error('error');773 },774 {message: () => false},775 ),776 {777 assertion: 'throws',778 message: '',779 values: [780 {label: 'Function threw unexpected exception:', formatted: /error/},781 {label: 'Expected message to return true:', formatted: /Function/},782 ],783 },784 );785 passes(t, () => assertions.throws(() => {786 throw new Error('error');787 }, {message: () => true}));788}));789test('.throws() returns the thrown error', t => {790 const expected = new Error();791 const actual = assertions.throws(() => {792 throw expected;793 });794 t.equal(actual, expected);795 t.end();796});797test('.throwsAsync()', gather(t => {798 // Fails because the promise is resolved, not rejected.799 throwsAsyncFails(t, () => assertions.throwsAsync(Promise.resolve('foo')), {800 assertion: 'throwsAsync',801 message: '',802 values: [{label: 'Promise resolved with:', formatted: /'foo'/}],803 });804 throwsAsyncFails(t, () => assertions.throwsAsync(Promise.resolve('foo')), {805 assertion: 'throwsAsync',806 message: '',807 values: [{label: 'Promise resolved with:', formatted: /'foo'/}],808 });809 // Fails because the promise is resolved with an Error810 throwsAsyncFails(t, () => assertions.throwsAsync(Promise.resolve(new Error())), {811 assertion: 'throwsAsync',812 message: '',813 values: [{label: 'Promise resolved with:', formatted: /Error/}],814 });815 // Fails because the function returned a promise that resolved, not rejected.816 throwsAsyncFails(t, () => assertions.throwsAsync(() => Promise.resolve('foo')), {817 assertion: 'throwsAsync',818 message: '',819 values: [{label: 'Returned promise resolved with:', formatted: /'foo'/}],820 });821 // Passes because the promise was rejected with an error.822 throwsAsyncPasses(t, () => assertions.throwsAsync(Promise.reject(new Error())));823 // Passes because the function returned a promise rejected with an error.824 throwsAsyncPasses(t, () => assertions.throwsAsync(() => Promise.reject(new Error())));825 // Fails because the function throws synchronously826 throwsAsyncFails(t, () => assertions.throwsAsync(() => {827 throw new Error('sync');828 }, undefined, 'message'), {829 assertion: 'throwsAsync',830 message: 'message',831 values: [832 {label: 'Function threw synchronously. Use `t.throws()` instead:', formatted: /Error/},833 ],834 });835 // Fails because the function did not return a promise836 throwsAsyncFails(t, () => assertions.throwsAsync(() => {}, undefined, 'message'), {837 assertion: 'throwsAsync',838 message: 'message',839 values: [840 {label: 'Function returned:', formatted: /undefined/},841 ],842 });843 throwsAsyncFails(t, () => assertions.throwsAsync(Promise.resolve(), undefined, null), {844 assertion: 'throwsAsync',845 improperUsage: true,846 message: 'The assertion message must be a string',847 values: [{848 label: 'Called with:',849 formatted: /null/,850 }],851 });852}));853test('.throwsAsync() returns the rejection reason of promise', t => {854 const expected = new Error();855 return assertions.throwsAsync(Promise.reject(expected)).then(actual => {856 t.equal(actual, expected);857 t.end();...
Using AI Code Generation
1const test = require('ava');2const throwsAsyncFails = require('throws-async-fails');3test('throwsAsyncFails', async t => {4 await throwsAsyncFails(5 async () => {6 throw new Error('Error message');7 },8 );9});10const test = require('ava');11const throwsAsyncPasses = require('throws-async-passes');12test('throwsAsyncPasses', async t => {13 await throwsAsyncPasses(14 async () => {15 throw new Error('Error message');16 },17 );18});19- [throws-async](
Using AI Code Generation
1test('throwsAsyncFails', async t => {2 await t.throwsAsync(Promise.reject(new Error('foo')), 'bar');3});4test('throwsAsyncPasses', async t => {5 await t.throwsAsync(Promise.reject(new Error('foo')), 'foo');6});7test('throwsAsyncThrows', async t => {8 await t.throwsAsync(Promise.resolve(), 'foo');9});10test('throwsAsyncThrows', async t => {11 await t.throwsAsync(Promise.reject(new Error('foo')), 'bar');12});13test('throwsAsyncThrows', async t => {14 await t.throwsAsync(Promise.resolve(), 'foo');15});16test('throwsAsyncThrows', async t => {17 await t.throwsAsync(Promise.reject(new Error('foo')), 'bar');18});19test('throwsAsyncThrows', async t => {20 await t.throwsAsync(Promise.resolve(), 'foo');21});22test('throwsAsyncThrows', async t => {23 await t.throwsAsync(Promise.reject(new Error('foo')), 'bar');24});25test('throwsAsyncThrows', async t => {26 await t.throwsAsync(Promise.resolve(), 'foo');27});28test('throwsAsyncThrows', async t => {29 await t.throwsAsync(Promise.reject(new Error('foo')), 'bar');30});31test('throwsAsyncThrows', async t => {32 await t.throwsAsync(Promise.resolve(), 'foo');33});34test('throwsAsyncThrows', async t => {35 await t.throwsAsync(Promise.reject(new
Using AI Code Generation
1import test from 'ava'2test('throwsAsyncFails', async t => {3 await t.throwsAsync(Promise.reject(new Error('foo')))4 await t.throwsAsync(Promise.reject(new Error('bar')))5})6import test from 'ava'7test('throwsAsync', async t => {8 await t.throwsAsync(Promise.reject(new Error('foo')))9 await t.throwsAsync(Promise.reject(new Error('foo')))10})
Using AI Code Generation
1import test from 'ava';2import {throwsAsyncFails} from 'ava-throws-async-fails';3test('throwsAsyncFails', throwsAsyncFails, async () => {4 await Promise.reject(new Error('foo'));5}, 'foo');6## throwsAsyncFails(testFn, expected, [message])
Using AI Code Generation
1const test = require('ava');2const throwsAsyncFails = require('ava-throws-async-fails');3const { throwsAsync } = require('ava-throws-async-fails');4const fn = () => Promise.reject(new Error('foo'));5test('throwsAsyncFails', throwsAsyncFails, fn, 'foo');6const test = require('ava');7const { throwsAsync } = require('ava-throws-async-fails');8const fn = () => Promise.reject(new Error('foo'));9test('throwsAsync', throwsAsync, fn, 'foo');10- [ava](
Using AI Code Generation
1import test from 'ava';2import fn from './';3test('throws async fails', async t => {4 await t.throwsAsync(fn(), 'foo');5});6### throwsAsync(fn, error, [message])7### throwsAsync(fn, [message])8- [throws-async](
Using AI Code Generation
1const test = require('ava');2const {throws} = require('assert');3const {promisify} = require('util');4const fs = require('fs');5const readFile = promisify(fs.readFile);6test('throwsAsyncFails', async t => {7 await throwsAsyncFails(async () => {8 await readFile('test.js', 'utf8');9 }, 'test.js');10});
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!!