How to use failsWith method in ava

Best JavaScript code snippet using ava

assert.js

Source:assert.js Github

copy

Full Screen

...86 }87 gatheringPromise = gatheringPromise.then(fn);88 return gatheringPromise;89}90function failsWith(t, fn, subset) {91 lastFailure = null;92 fn();93 assertFailure(t, subset);94}95function eventuallyFailsWith(t, fn, subset) {96 return add(() => {97 lastFailure = null;98 return fn().then(() => {99 assertFailure(t, subset);100 });101 });102}103function fails(t, fn) {104 lastFailure = null;105 fn();106 if (lastFailure) {107 t.pass();108 } else {109 t.fail('Expected assertion to fail');110 }111}112/* Might be useful113function eventuallyFails(t, fn) {114 return add(() => {115 lastFailure = null;116 return fn().then(() => {117 if (lastFailure) {118 t.pass();119 } else {120 t.fail('Expected assertion to fail');121 }122 });123 });124}125*/126function passes(t, fn) {127 lastPassed = false;128 lastFailure = null;129 fn();130 if (lastPassed) {131 t.pass();132 } else {133 t.ifError(lastFailure, 'Expected assertion to pass');134 }135}136function eventuallyPasses(t, fn) {137 return add(() => {138 lastPassed = false;139 lastFailure = null;140 return fn().then(() => {141 if (lastPassed) {142 t.pass();143 } else {144 t.ifError(lastFailure, 'Expected assertion to pass');145 }146 });147 });148}149test('.pass()', t => {150 passes(t, () => {151 assertions.pass();152 });153 passes(t, () => {154 const {pass} = assertions;155 pass();156 });157 t.end();158});159test('.fail()', t => {160 failsWith(t, () => {161 assertions.fail();162 }, {163 assertion: 'fail',164 message: 'Test failed via `t.fail()`'165 });166 failsWith(t, () => {167 assertions.fail('my message');168 }, {169 assertion: 'fail',170 message: 'my message'171 });172 failsWith(t, () => {173 const {fail} = assertions;174 fail();175 }, {176 assertion: 'fail',177 message: 'Test failed via `t.fail()`'178 });179 failsWith(t, () => {180 assertions.fail(null);181 }, {182 assertion: 'fail',183 improperUsage: true,184 message: 'The assertion message must be a string',185 values: [{186 label: 'Called with:',187 formatted: /null/188 }]189 });190 t.end();191});192test('.is()', t => {193 passes(t, () => {194 assertions.is('foo', 'foo');195 });196 passes(t, () => {197 const {is} = assertions;198 is('foo', 'foo');199 });200 passes(t, () => {201 assertions.is('', '');202 });203 passes(t, () => {204 assertions.is(true, true);205 });206 passes(t, () => {207 assertions.is(false, false);208 });209 passes(t, () => {210 assertions.is(null, null);211 });212 passes(t, () => {213 assertions.is(undefined, undefined);214 });215 passes(t, () => {216 assertions.is(1, 1);217 });218 passes(t, () => {219 assertions.is(0, 0);220 });221 passes(t, () => {222 assertions.is(-0, -0);223 });224 passes(t, () => {225 assertions.is(Number.NaN, Number.NaN);226 });227 passes(t, () => {228 assertions.is(0 / 0, Number.NaN);229 });230 passes(t, () => {231 const someRef = {foo: 'bar'};232 assertions.is(someRef, someRef);233 });234 fails(t, () => {235 assertions.is(0, -0);236 });237 fails(t, () => {238 assertions.is(0, false);239 });240 fails(t, () => {241 assertions.is('', false);242 });243 fails(t, () => {244 assertions.is('0', 0);245 });246 fails(t, () => {247 assertions.is('17', 17);248 });249 fails(t, () => {250 assertions.is([1, 2], '1,2');251 });252 fails(t, () => {253 // eslint-disable-next-line no-new-wrappers, unicorn/new-for-builtins254 assertions.is(new String('foo'), 'foo');255 });256 fails(t, () => {257 assertions.is(null, undefined);258 });259 fails(t, () => {260 assertions.is(null, false);261 });262 fails(t, () => {263 assertions.is(undefined, false);264 });265 fails(t, () => {266 // eslint-disable-next-line no-new-wrappers, unicorn/new-for-builtins267 assertions.is(new String('foo'), new String('foo'));268 });269 fails(t, () => {270 assertions.is(0, null);271 });272 fails(t, () => {273 assertions.is(0, Number.NaN);274 });275 fails(t, () => {276 assertions.is('foo', Number.NaN);277 });278 failsWith(t, () => {279 assertions.is({foo: 'bar'}, {foo: 'bar'});280 }, {281 assertion: 'is',282 message: '',283 actual: {foo: 'bar'},284 expected: {foo: 'bar'},285 values: [{286 label: 'Values are deeply equal to each other, but they are not the same:',287 formatted: /foo/288 }]289 });290 failsWith(t, () => {291 assertions.is('foo', 'bar');292 }, {293 assertion: 'is',294 message: '',295 raw: {actual: 'foo', expected: 'bar'},296 values: [297 {label: 'Difference:', formatted: /- 'foo'\n\+ 'bar'/}298 ]299 });300 failsWith(t, () => {301 assertions.is('foo', 42);302 }, {303 actual: 'foo',304 assertion: 'is',305 expected: 42,306 message: '',307 values: [308 {label: 'Difference:', formatted: /- 'foo'\n\+ 42/}309 ]310 });311 failsWith(t, () => {312 assertions.is('foo', 42, 'my message');313 }, {314 assertion: 'is',315 message: 'my message',316 values: [317 {label: 'Difference:', formatted: /- 'foo'\n\+ 42/}318 ]319 });320 failsWith(t, () => {321 assertions.is(0, -0, 'my message');322 }, {323 assertion: 'is',324 message: 'my message',325 values: [326 {label: 'Difference:', formatted: /- 0\n\+ -0/}327 ]328 });329 failsWith(t, () => {330 assertions.is(-0, 0, 'my message');331 }, {332 assertion: 'is',333 message: 'my message',334 values: [335 {label: 'Difference:', formatted: /- -0\n\+ 0/}336 ]337 });338 failsWith(t, () => {339 assertions.is(0, 0, null);340 }, {341 assertion: 'is',342 improperUsage: true,343 message: 'The assertion message must be a string',344 values: [{345 label: 'Called with:',346 formatted: /null/347 }]348 });349 t.end();350});351test('.not()', t => {352 passes(t, () => {353 assertions.not('foo', 'bar');354 });355 passes(t, () => {356 const {not} = assertions;357 not('foo', 'bar');358 });359 fails(t, () => {360 assertions.not(Number.NaN, Number.NaN);361 });362 fails(t, () => {363 assertions.not(0 / 0, Number.NaN);364 });365 failsWith(t, () => {366 assertions.not('foo', 'foo');367 }, {368 assertion: 'not',369 message: '',370 raw: {actual: 'foo', expected: 'foo'},371 values: [{label: 'Value is the same as:', formatted: /foo/}]372 });373 failsWith(t, () => {374 assertions.not('foo', 'foo', 'my message');375 }, {376 assertion: 'not',377 message: 'my message',378 values: [{label: 'Value is the same as:', formatted: /foo/}]379 });380 failsWith(t, () => {381 assertions.not(0, 1, null);382 }, {383 assertion: 'not',384 improperUsage: true,385 message: 'The assertion message must be a string',386 values: [{387 label: 'Called with:',388 formatted: /null/389 }]390 });391 t.end();392});393test('.deepEqual()', t => {394 // Tests starting here are to detect regressions in the underlying libraries395 // used to test deep object equality396 fails(t, () => {397 assertions.deepEqual({a: false}, {a: 0});398 });399 passes(t, () => {400 assertions.deepEqual({401 a: 'a',402 b: 'b'403 }, {404 b: 'b',405 a: 'a'406 });407 });408 passes(t, () => {409 const {deepEqual} = assertions;410 deepEqual({a: 'a', b: 'b'}, {b: 'b', a: 'a'});411 });412 passes(t, () => {413 assertions.deepEqual({414 a: 'a',415 b: 'b',416 c: {417 d: 'd'418 }419 }, {420 c: {421 d: 'd'422 },423 b: 'b',424 a: 'a'425 });426 });427 fails(t, () => {428 assertions.deepEqual([1, 2, 3], [1, 2, 3, 4]);429 });430 passes(t, () => {431 assertions.deepEqual([1, 2, 3], [1, 2, 3]);432 });433 fails(t, () => {434 const fnA = a => a;435 const fnB = a => a;436 assertions.deepEqual(fnA, fnB);437 });438 passes(t, () => {439 const x1 = {z: 4};440 const y1 = {x: x1};441 x1.y = y1;442 const x2 = {z: 4};443 const y2 = {x: x2};444 x2.y = y2;445 assertions.deepEqual(x1, x2);446 });447 passes(t, () => {448 function Foo(a) {449 this.a = a;450 }451 const x = new Foo(1);452 const y = new Foo(1);453 assertions.deepEqual(x, y);454 });455 fails(t, () => {456 function Foo(a) {457 this.a = a;458 }459 function Bar(a) {460 this.a = a;461 }462 const x = new Foo(1);463 const y = new Bar(1);464 assertions.deepEqual(x, y);465 });466 fails(t, () => {467 assertions.deepEqual({468 a: 'a',469 b: 'b',470 c: {471 d: false472 }473 }, {474 c: {475 d: 0476 },477 b: 'b',478 a: 'a'479 });480 });481 fails(t, () => {482 assertions.deepEqual({}, []);483 });484 fails(t, () => {485 assertions.deepEqual({0: 'a', 1: 'b'}, ['a', 'b']);486 });487 fails(t, () => {488 assertions.deepEqual({a: 1}, {a: 1, b: undefined});489 });490 fails(t, () => {491 assertions.deepEqual(new Date('1972-08-01'), null);492 });493 fails(t, () => {494 assertions.deepEqual(new Date('1972-08-01'), undefined);495 });496 passes(t, () => {497 assertions.deepEqual(new Date('1972-08-01'), new Date('1972-08-01'));498 });499 passes(t, () => {500 assertions.deepEqual({x: new Date('1972-08-01')}, {x: new Date('1972-08-01')});501 });502 fails(t, () => {503 assertions.deepEqual(() => {}, () => {});504 });505 passes(t, () => {506 assertions.deepEqual(undefined, undefined);507 assertions.deepEqual({x: undefined}, {x: undefined});508 assertions.deepEqual({x: [undefined]}, {x: [undefined]});509 });510 passes(t, () => {511 assertions.deepEqual(null, null);512 assertions.deepEqual({x: null}, {x: null});513 assertions.deepEqual({x: [null]}, {x: [null]});514 });515 passes(t, () => {516 assertions.deepEqual(0, 0);517 assertions.deepEqual(1, 1);518 assertions.deepEqual(3.14, 3.14);519 });520 fails(t, () => {521 assertions.deepEqual(0, 1);522 });523 fails(t, () => {524 assertions.deepEqual(1, -1);525 });526 fails(t, () => {527 assertions.deepEqual(3.14, 2.72);528 });529 fails(t, () => {530 assertions.deepEqual({0: 'a', 1: 'b'}, ['a', 'b']);531 });532 passes(t, () => {533 assertions.deepEqual(534 [535 {foo: {z: 100, y: 200, x: 300}},536 'bar',537 11,538 {baz: {d: 4, a: 1, b: 2, c: 3}}539 ],540 [541 {foo: {x: 300, y: 200, z: 100}},542 'bar',543 11,544 {baz: {c: 3, b: 2, a: 1, d: 4}}545 ]546 );547 });548 passes(t, () => {549 assertions.deepEqual(550 {x: {a: 1, b: 2}, y: {c: 3, d: 4}},551 {y: {d: 4, c: 3}, x: {b: 2, a: 1}}552 );553 });554 // Regression test end here555 passes(t, () => {556 assertions.deepEqual({a: 'a'}, {a: 'a'});557 });558 passes(t, () => {559 assertions.deepEqual(['a', 'b'], ['a', 'b']);560 });561 fails(t, () => {562 assertions.deepEqual({a: 'a'}, {a: 'b'});563 });564 fails(t, () => {565 assertions.deepEqual(['a', 'b'], ['a', 'a']);566 });567 fails(t, () => {568 assertions.deepEqual([['a', 'b'], 'c'], [['a', 'b'], 'd']);569 });570 fails(t, () => {571 const circular = ['a', 'b'];572 circular.push(circular);573 assertions.deepEqual([circular, 'c'], [circular, 'd']);574 });575 failsWith(t, () => {576 assertions.deepEqual('foo', 'bar');577 }, {578 assertion: 'deepEqual',579 message: '',580 raw: {actual: 'foo', expected: 'bar'},581 values: [{label: 'Difference:', formatted: /- 'foo'\n\+ 'bar'/}]582 });583 failsWith(t, () => {584 assertions.deepEqual('foo', 42);585 }, {586 assertion: 'deepEqual',587 message: '',588 raw: {actual: 'foo', expected: 42},589 values: [{label: 'Difference:', formatted: /- 'foo'\n\+ 42/}]590 });591 failsWith(t, () => {592 assertions.deepEqual('foo', 42, 'my message');593 }, {594 assertion: 'deepEqual',595 message: 'my message',596 values: [{label: 'Difference:', formatted: /- 'foo'\n\+ 42/}]597 });598 failsWith(t, () => {599 assertions.deepEqual({}, {}, null);600 }, {601 assertion: 'deepEqual',602 improperUsage: true,603 message: 'The assertion message must be a string',604 values: [{605 label: 'Called with:',606 formatted: /null/607 }]608 });609 t.end();610});611test('.notDeepEqual()', t => {612 passes(t, () => {613 assertions.notDeepEqual({a: 'a'}, {a: 'b'});614 });615 passes(t, () => {616 const {notDeepEqual} = assertions;617 notDeepEqual({a: 'a'}, {a: 'b'});618 });619 passes(t, () => {620 assertions.notDeepEqual(['a', 'b'], ['c', 'd']);621 });622 const actual = {a: 'a'};623 const expected = {a: 'a'};624 failsWith(t, () => {625 assertions.notDeepEqual(actual, expected);626 }, {627 actual,628 assertion: 'notDeepEqual',629 expected,630 message: '',631 raw: {actual, expected},632 values: [{label: 'Value is deeply equal:', formatted: /.*{.*\n.*a: 'a'/}]633 });634 failsWith(t, () => {635 assertions.notDeepEqual(['a', 'b'], ['a', 'b'], 'my message');636 }, {637 assertion: 'notDeepEqual',638 message: 'my message',639 values: [{label: 'Value is deeply equal:', formatted: /.*\[.*\n.*'a',\n.*'b',/}]640 });641 failsWith(t, () => {642 assertions.notDeepEqual({}, [], null);643 }, {644 assertion: 'notDeepEqual',645 improperUsage: true,646 message: 'The assertion message must be a string',647 values: [{648 label: 'Called with:',649 formatted: /null/650 }]651 });652 t.end();653});654test('.like()', t => {655 fails(t, () => {656 assertions.like({a: false}, {a: 0});657 });658 passes(t, () => {659 assertions.like({660 a: 'a',661 b: 'b'662 }, {663 b: 'b',664 a: 'a'665 });666 });667 passes(t, () => {668 const {like} = assertions;669 like({a: 'a', b: 'b'}, {b: 'b', a: 'a'});670 });671 passes(t, () => {672 assertions.like({673 a: 'a',674 b: 'b',675 c: {676 d: 'd',677 x: 'x'678 },679 x: 'x'680 }, {681 c: {682 d: 'd'683 },684 b: 'b',685 a: 'a'686 });687 });688 fails(t, () => {689 assertions.like([1, 2, 3], [1, 2, 3, 4]);690 });691 fails(t, () => {692 assertions.like({693 a: [1, 2, 3]694 }, {695 a: [1, 2, 3, 4]696 });697 });698 passes(t, () => {699 assertions.like({700 a: [1, 2, 3],701 x: 'x'702 }, {703 a: [1, 2, 3]704 });705 });706 passes(t, () => {707 const actual = {708 a: 'a',709 extra: 'irrelevant'710 };711 actual.circular = actual;712 const likePattern = {713 a: 'a'714 };715 assertions.like(actual, likePattern);716 });717 fails(t, () => {718 const fnA = a => a;719 const fnB = a => a;720 assertions.like(fnA, fnB);721 });722 fails(t, () => {723 const fnA = a => a;724 const fnB = a => a;725 assertions.like({726 fn: fnA727 }, {728 fn: fnB729 });730 });731 fails(t, () => {732 function Foo(a) {733 this.a = a;734 }735 function Bar(a) {736 this.a = a;737 }738 const x = new Foo(1);739 const y = new Bar(1);740 assertions.like(x, y);741 });742 passes(t, () => {743 assertions.like({a: 'a'}, {a: 'a'});744 });745 passes(t, () => {746 assertions.like({a: 'a', b: 'b'}, {a: 'a'});747 });748 passes(t, () => {749 assertions.like({ab: ['a', 'b']}, {ab: ['a', 'b']});750 });751 passes(t, () => {752 assertions.like({ab: ['a', 'b'], c: 'c'}, {ab: ['a', 'b']});753 });754 fails(t, () => {755 assertions.like({a: 'a'}, {a: 'b'});756 });757 fails(t, () => {758 assertions.like({a: 'a', b: 'b'}, {a: 'b'});759 });760 fails(t, () => {761 assertions.like({ab: ['a', 'b']}, {ab: ['a', 'a']});762 });763 fails(t, () => {764 assertions.like({ab: ['a', 'b'], c: 'c'}, {ab: ['a', 'a']});765 });766 fails(t, () => {767 assertions.like([['a', 'b'], 'c'], [['a', 'b'], 'd']);768 });769 fails(t, () => {770 const circular = ['a', 'b'];771 circular.push(circular);772 assertions.like([circular, 'c'], [circular, 'd']);773 });774 fails(t, () => {775 const circular = ['a', 'b'];776 circular.push(circular);777 assertions.like({xc: [circular, 'c']}, {xc: [circular, 'd']});778 });779 failsWith(t, () => {780 assertions.like({a: 'a'}, {});781 }, {782 assertion: 'like',783 message: '`t.like()` selector must be a non-empty object',784 values: [{label: 'Called with:', formatted: '{}'}]785 });786 failsWith(t, () => {787 assertions.like('foo', 'bar');788 }, {789 assertion: 'like',790 message: '`t.like()` selector must be a non-empty object',791 values: [{label: 'Called with:', formatted: '\'bar\''}]792 });793 failsWith(t, () => {794 const likePattern = {795 a: 'a'796 };797 likePattern.circular = likePattern;798 assertions.like({}, likePattern);799 }, {800 assertion: 'like',801 message: '`t.like()` selector must not contain circular references',802 values: [{label: 'Called with:', formatted: '{\n a: \'a\',\n circular: [Circular],\n}'}]803 });804 failsWith(t, () => {805 assertions.like({}, {}, null);806 }, {807 assertion: 'like',808 improperUsage: true,809 message: 'The assertion message must be a string',810 values: [{811 label: 'Called with:',812 formatted: /null/813 }]814 });815 failsWith(t, () => {816 assertions.like({a: 'foo', b: 'irrelevant'}, {a: 'bar'});817 }, {818 assertion: 'like',819 message: '',820 values: [{label: 'Difference:', formatted: /{\n-\s*a: 'foo',\n\+\s*a: 'bar',\n\s*}/}]821 });822 t.end();823});824test('.throws()', gather(t => {825 // Fails because function doesn't throw.826 failsWith(t, () => {827 assertions.throws(() => {});828 }, {829 assertion: 'throws',830 message: '',831 values: [{label: 'Function returned:', formatted: /undefined/}]832 });833 failsWith(t, () => {834 const {throws} = assertions;835 throws(() => {});836 }, {837 assertion: 'throws',838 message: '',839 values: [{label: 'Function returned:', formatted: /undefined/}]840 });841 // Fails because function doesn't throw. Asserts that 'my message' is used842 // as the assertion message (*not* compared against the error).843 failsWith(t, () => {844 assertions.throws(() => {}, undefined, 'my message');845 }, {846 assertion: 'throws',847 message: 'my message',848 values: [{label: 'Function returned:', formatted: /undefined/}]849 });850 // Fails because the function returned a promise.851 failsWith(t, () => {852 assertions.throws(() => Promise.resolve());853 }, {854 assertion: 'throws',855 message: '',856 values: [{label: 'Function returned a promise. Use `t.throwsAsync()` instead:', formatted: /Promise/}]857 });858 // Fails because thrown exception is not an error859 failsWith(t, () => {860 assertions.throws(() => {861 const err = 'foo';862 throw err;863 });864 }, {865 assertion: 'throws',866 message: '',867 values: [868 {label: 'Function threw exception that is not an error:', formatted: /'foo'/}869 ]870 });871 // Passes because an error is thrown.872 passes(t, () => {873 assertions.throws(() => {874 throw new Error('foo');875 });876 });877 // Passes because the correct error is thrown.878 passes(t, () => {879 const err = new Error('foo');880 assertions.throws(() => {881 throw err;882 }, {is: err});883 });884 // Fails because the thrown value is not an error885 fails(t, () => {886 const object = {};887 assertions.throws(() => {888 throw object;889 }, {is: object});890 });891 // Fails because the thrown value is not the right one892 fails(t, () => {893 const err = new Error('foo');894 assertions.throws(() => {895 throw err;896 }, {is: {}});897 });898 // Passes because the correct error is thrown.899 passes(t, () => {900 assertions.throws(() => {901 throw new TypeError();902 }, {name: 'TypeError'});903 });904 // Fails because the thrown value is not an error905 fails(t, () => {906 assertions.throws(() => {907 const err = {name: 'Bob'};908 throw err;909 }, {name: 'Bob'});910 });911 // Fails because the thrown value is not the right one912 fails(t, () => {913 assertions.throws(() => {914 throw new Error('foo');915 }, {name: 'TypeError'});916 });917 // Passes because the correct error is thrown.918 passes(t, () => {919 assertions.throws(() => {920 const err = new TypeError();921 err.code = 'ERR_TEST';922 throw err;923 }, {code: 'ERR_TEST'});924 });925 // Passes because the correct error is thrown.926 passes(t, () => {927 assertions.throws(() => {928 const err = new TypeError();929 err.code = 42;930 throw err;931 }, {code: 42});932 });933 // Fails because the thrown value is not the right one934 fails(t, () => {935 assertions.throws(() => {936 const err = new TypeError();937 err.code = 'ERR_NOPE';938 throw err;939 }, {code: 'ERR_TEST'});940 });941 fails(t, () => {942 assertions.throws(() => {943 const err = new TypeError();944 err.code = 1;945 throw err;946 }, {code: 42});947 });948 // Regression test for https://github.com/avajs/ava/issues/1676949 fails(t, () => {950 assertions.throws(() => {951 throw new Error('foo');952 }, false);953 });954 passes(t, () => {955 assertions.throws(() => {956 throw new Error('foo');957 }, undefined);958 });959 passes(t, async () => {960 await assertions.throwsAsync(() => {961 return Promise.reject(new Error('foo'));962 }, undefined);963 });964 failsWith(t, () => {965 assertions.throws(() => {}, undefined, null);966 }, {967 assertion: 'throws',968 improperUsage: true,969 message: 'The assertion message must be a string',970 values: [{971 label: 'Called with:',972 formatted: /null/973 }]974 });975}));976test('.throws() returns the thrown error', t => {977 const expected = new Error();978 const actual = assertions.throws(() => {979 throw expected;980 });981 t.is(actual, expected);982 t.end();983});984test('.throwsAsync()', gather(t => {985 // Fails because the promise is resolved, not rejected.986 eventuallyFailsWith(t, () => assertions.throwsAsync(Promise.resolve('foo')), {987 assertion: 'throwsAsync',988 message: '',989 values: [{label: 'Promise resolved with:', formatted: /'foo'/}]990 });991 eventuallyFailsWith(t, () => {992 const {throwsAsync} = assertions;993 return throwsAsync(Promise.resolve('foo'));994 }, {995 assertion: 'throwsAsync',996 message: '',997 values: [{label: 'Promise resolved with:', formatted: /'foo'/}]998 });999 // Fails because the promise is resolved with an Error1000 eventuallyFailsWith(t, () => assertions.throwsAsync(Promise.resolve(new Error())), {1001 assertion: 'throwsAsync',1002 message: '',1003 values: [{label: 'Promise resolved with:', formatted: /Error/}]1004 });1005 // Fails because the function returned a promise that resolved, not rejected.1006 eventuallyFailsWith(t, () => assertions.throwsAsync(() => Promise.resolve('foo')), {1007 assertion: 'throwsAsync',1008 message: '',1009 values: [{label: 'Returned promise resolved with:', formatted: /'foo'/}]1010 });1011 // Passes because the promise was rejected with an error.1012 eventuallyPasses(t, () => assertions.throwsAsync(Promise.reject(new Error())));1013 // Passes because the function returned a promise rejected with an error.1014 eventuallyPasses(t, () => assertions.throwsAsync(() => Promise.reject(new Error())));1015 // Fails because the function throws synchronously1016 eventuallyFailsWith(t, () => assertions.throwsAsync(() => {1017 throw new Error('sync');1018 }, undefined, 'message'), {1019 assertion: 'throwsAsync',1020 message: 'message',1021 values: [1022 {label: 'Function threw synchronously. Use `t.throws()` instead:', formatted: /Error/}1023 ]1024 });1025 // Fails because the function did not return a promise1026 eventuallyFailsWith(t, () => assertions.throwsAsync(() => {}, undefined, 'message'), {1027 assertion: 'throwsAsync',1028 message: 'message',1029 values: [1030 {label: 'Function returned:', formatted: /undefined/}1031 ]1032 });1033 eventuallyFailsWith(t, () => assertions.throwsAsync(Promise.resolve(), undefined, null), {1034 assertion: 'throwsAsync',1035 improperUsage: true,1036 message: 'The assertion message must be a string',1037 values: [{1038 label: 'Called with:',1039 formatted: /null/1040 }]1041 });1042}));1043test('.throwsAsync() returns the rejection reason of promise', t => {1044 const expected = new Error();1045 return assertions.throwsAsync(Promise.reject(expected)).then(actual => {1046 t.is(actual, expected);1047 t.end();1048 });1049});1050test('.throwsAsync() returns the rejection reason of a promise returned by the function', t => {1051 const expected = new Error();1052 return assertions.throwsAsync(() => {1053 return Promise.reject(expected);1054 }).then(actual => {1055 t.is(actual, expected);1056 t.end();1057 });1058});1059test('.throws() fails if passed a bad value', t => {1060 failsWith(t, () => {1061 assertions.throws('not a function');1062 }, {1063 assertion: 'throws',1064 message: '`t.throws()` must be called with a function',1065 values: [{label: 'Called with:', formatted: /not a function/}]1066 });1067 t.end();1068});1069test('.throwsAsync() fails if passed a bad value', t => {1070 failsWith(t, () => {1071 assertions.throwsAsync('not a function');1072 }, {1073 assertion: 'throwsAsync',1074 message: '`t.throwsAsync()` must be called with a function or promise',1075 values: [{label: 'Called with:', formatted: /not a function/}]1076 });1077 t.end();1078});1079test('.throws() fails if passed a bad expectation', t => {1080 failsWith(t, () => {1081 assertions.throws(() => {}, true);1082 }, {1083 assertion: 'throws',1084 message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`',1085 values: [{label: 'Called with:', formatted: /true/}]1086 });1087 failsWith(t, () => {1088 assertions.throws(() => {}, 'foo');1089 }, {1090 assertion: 'throws',1091 message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`',1092 values: [{label: 'Called with:', formatted: /foo/}]1093 });1094 failsWith(t, () => {1095 assertions.throws(() => {}, /baz/);1096 }, {1097 assertion: 'throws',1098 message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`',1099 values: [{label: 'Called with:', formatted: /baz/}]1100 });1101 failsWith(t, () => {1102 assertions.throws(() => {}, class Bar {});1103 }, {1104 assertion: 'throws',1105 message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`',1106 values: [{label: 'Called with:', formatted: /Bar/}]1107 });1108 failsWith(t, () => {1109 assertions.throws(() => {}, {});1110 }, {1111 assertion: 'throws',1112 message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`',1113 values: [{label: 'Called with:', formatted: /{}/}]1114 });1115 failsWith(t, () => {1116 assertions.throws(() => {}, []);1117 }, {1118 assertion: 'throws',1119 message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`',1120 values: [{label: 'Called with:', formatted: /\[]/}]1121 });1122 failsWith(t, () => {1123 assertions.throws(() => {}, {code: {}});1124 }, {1125 assertion: 'throws',1126 message: 'The `code` property of the second argument to `t.throws()` must be a string or number',1127 values: [{label: 'Called with:', formatted: /code: {}/}]1128 });1129 failsWith(t, () => {1130 assertions.throws(() => {}, {instanceOf: null});1131 }, {1132 assertion: 'throws',1133 message: 'The `instanceOf` property of the second argument to `t.throws()` must be a function',1134 values: [{label: 'Called with:', formatted: /instanceOf: null/}]1135 });1136 failsWith(t, () => {1137 assertions.throws(() => {}, {message: null});1138 }, {1139 assertion: 'throws',1140 message: 'The `message` property of the second argument to `t.throws()` must be a string or regular expression',1141 values: [{label: 'Called with:', formatted: /message: null/}]1142 });1143 failsWith(t, () => {1144 assertions.throws(() => {}, {name: null});1145 }, {1146 assertion: 'throws',1147 message: 'The `name` property of the second argument to `t.throws()` must be a string',1148 values: [{label: 'Called with:', formatted: /name: null/}]1149 });1150 failsWith(t, () => {1151 assertions.throws(() => {}, {is: {}, message: '', name: '', of() {}, foo: null});1152 }, {1153 assertion: 'throws',1154 message: 'The second argument to `t.throws()` contains unexpected properties',1155 values: [{label: 'Called with:', formatted: /foo: null/}]1156 });1157 t.end();1158});1159test('.throwsAsync() fails if passed a bad expectation', t => {1160 failsWith(t, () => {1161 assertions.throwsAsync(() => {}, true);1162 }, {1163 assertion: 'throwsAsync',1164 message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`',1165 values: [{label: 'Called with:', formatted: /true/}]1166 });1167 failsWith(t, () => {1168 assertions.throwsAsync(() => {}, 'foo');1169 }, {1170 assertion: 'throwsAsync',1171 message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`',1172 values: [{label: 'Called with:', formatted: /foo/}]1173 });1174 failsWith(t, () => {1175 assertions.throwsAsync(() => {}, /baz/);1176 }, {1177 assertion: 'throwsAsync',1178 message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`',1179 values: [{label: 'Called with:', formatted: /baz/}]1180 });1181 failsWith(t, () => {1182 assertions.throwsAsync(() => {}, class Bar {});1183 }, {1184 assertion: 'throwsAsync',1185 message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`',1186 values: [{label: 'Called with:', formatted: /Bar/}]1187 });1188 failsWith(t, () => {1189 assertions.throwsAsync(() => {}, {});1190 }, {1191 assertion: 'throwsAsync',1192 message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`',1193 values: [{label: 'Called with:', formatted: /{}/}]1194 });1195 failsWith(t, () => {1196 assertions.throwsAsync(() => {}, []);1197 }, {1198 assertion: 'throwsAsync',1199 message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`',1200 values: [{label: 'Called with:', formatted: /\[]/}]1201 });1202 failsWith(t, () => {1203 assertions.throwsAsync(() => {}, {code: {}});1204 }, {1205 assertion: 'throwsAsync',1206 message: 'The `code` property of the second argument to `t.throwsAsync()` must be a string or number',1207 values: [{label: 'Called with:', formatted: /code: {}/}]1208 });1209 failsWith(t, () => {1210 assertions.throwsAsync(() => {}, {instanceOf: null});1211 }, {1212 assertion: 'throwsAsync',1213 message: 'The `instanceOf` property of the second argument to `t.throwsAsync()` must be a function',1214 values: [{label: 'Called with:', formatted: /instanceOf: null/}]1215 });1216 failsWith(t, () => {1217 assertions.throwsAsync(() => {}, {message: null});1218 }, {1219 assertion: 'throwsAsync',1220 message: 'The `message` property of the second argument to `t.throwsAsync()` must be a string or regular expression',1221 values: [{label: 'Called with:', formatted: /message: null/}]1222 });1223 failsWith(t, () => {1224 assertions.throwsAsync(() => {}, {name: null});1225 }, {1226 assertion: 'throwsAsync',1227 message: 'The `name` property of the second argument to `t.throwsAsync()` must be a string',1228 values: [{label: 'Called with:', formatted: /name: null/}]1229 });1230 failsWith(t, () => {1231 assertions.throwsAsync(() => {}, {is: {}, message: '', name: '', of() {}, foo: null});1232 }, {1233 assertion: 'throwsAsync',1234 message: 'The second argument to `t.throwsAsync()` contains unexpected properties',1235 values: [{label: 'Called with:', formatted: /foo: null/}]1236 });1237 t.end();1238});1239test('.throws() fails if passed null expectation', t => {1240 const asserter = new AssertionsBase();1241 failsWith(t, () => {1242 asserter.throws(() => {}, null);1243 }, {1244 assertion: 'throws',1245 message: 'The second argument to `t.throws()` must be an expectation object or `undefined`',1246 values: [{label: 'Called with:', formatted: /null/}]1247 });1248 t.end();1249});1250test('.throwsAsync() fails if passed null', t => {1251 const asserter = new AssertionsBase();1252 failsWith(t, () => {1253 asserter.throwsAsync(() => {}, null);1254 }, {1255 assertion: 'throwsAsync',1256 message: 'The second argument to `t.throwsAsync()` must be an expectation object or `undefined`',1257 values: [{label: 'Called with:', formatted: /null/}]1258 });1259 t.end();1260});1261test('.notThrows()', gather(t => {1262 // Passes because the function doesn't throw1263 passes(t, () => {1264 assertions.notThrows(() => {});1265 });1266 passes(t, () => {1267 const {notThrows} = assertions;1268 notThrows(() => {});1269 });1270 // Fails because the function throws.1271 failsWith(t, () => {1272 assertions.notThrows(() => {1273 throw new Error('foo');1274 });1275 }, {1276 assertion: 'notThrows',1277 message: '',1278 values: [{label: 'Function threw:', formatted: /foo/}]1279 });1280 // Fails because the function throws. Asserts that message is used for the1281 // assertion, not to validate the thrown error.1282 failsWith(t, () => {1283 assertions.notThrows(() => {1284 throw new Error('foo');1285 }, 'my message');1286 }, {1287 assertion: 'notThrows',1288 message: 'my message',1289 values: [{label: 'Function threw:', formatted: /foo/}]1290 });1291 failsWith(t, () => {1292 assertions.notThrows(() => {}, null);1293 }, {1294 assertion: 'notThrows',1295 improperUsage: true,1296 message: 'The assertion message must be a string',1297 values: [{1298 label: 'Called with:',1299 formatted: /null/1300 }]1301 });1302}));1303test('.notThrowsAsync()', gather(t => {1304 // Passes because the promise is resolved1305 eventuallyPasses(t, () => assertions.notThrowsAsync(Promise.resolve()));1306 eventuallyPasses(t, () => {1307 const {notThrowsAsync} = assertions;1308 return notThrowsAsync(Promise.resolve());1309 });1310 // Fails because the promise is rejected1311 eventuallyFailsWith(t, () => assertions.notThrowsAsync(Promise.reject(new Error())), {1312 assertion: 'notThrowsAsync',1313 message: '',1314 values: [{label: 'Promise rejected with:', formatted: /Error/}]1315 });1316 // Passes because the function returned a resolved promise1317 eventuallyPasses(t, () => assertions.notThrowsAsync(() => Promise.resolve()));1318 // Fails because the function returned a rejected promise1319 eventuallyFailsWith(t, () => assertions.notThrowsAsync(() => Promise.reject(new Error())), {1320 assertion: 'notThrowsAsync',1321 message: '',1322 values: [{label: 'Returned promise rejected with:', formatted: /Error/}]1323 });1324 // Fails because the function throws synchronously1325 eventuallyFailsWith(t, () => assertions.notThrowsAsync(() => {1326 throw new Error('sync');1327 }, 'message'), {1328 assertion: 'notThrowsAsync',1329 message: 'message',1330 values: [1331 {label: 'Function threw:', formatted: /Error/}1332 ]1333 });1334 // Fails because the function did not return a promise1335 eventuallyFailsWith(t, () => assertions.notThrowsAsync(() => {}, 'message'), {1336 assertion: 'notThrowsAsync',1337 message: 'message',1338 values: [1339 {label: 'Function did not return a promise. Use `t.notThrows()` instead:', formatted: /undefined/}1340 ]1341 });1342 eventuallyFailsWith(t, () => assertions.notThrowsAsync(Promise.resolve(), null), {1343 assertion: 'notThrowsAsync',1344 improperUsage: true,1345 message: 'The assertion message must be a string',1346 values: [{1347 label: 'Called with:',1348 formatted: /null/1349 }]1350 });1351}));1352test('.notThrowsAsync() returns undefined for a fulfilled promise', t => {1353 return assertions.notThrowsAsync(Promise.resolve(Symbol(''))).then(actual => {1354 t.is(actual, undefined);1355 });1356});1357test('.notThrowsAsync() returns undefined for a fulfilled promise returned by the function', t => {1358 return assertions.notThrowsAsync(() => {1359 return Promise.resolve(Symbol(''));1360 }).then(actual => {1361 t.is(actual, undefined);1362 });1363});1364test('.notThrows() fails if passed a bad value', t => {1365 failsWith(t, () => {1366 assertions.notThrows('not a function');1367 }, {1368 assertion: 'notThrows',1369 message: '`t.notThrows()` must be called with a function',1370 values: [{label: 'Called with:', formatted: /not a function/}]1371 });1372 t.end();1373});1374test('.notThrowsAsync() fails if passed a bad value', t => {1375 failsWith(t, () => {1376 assertions.notThrowsAsync('not a function');1377 }, {1378 assertion: 'notThrowsAsync',1379 message: '`t.notThrowsAsync()` must be called with a function or promise',1380 values: [{label: 'Called with:', formatted: /not a function/}]1381 });1382 t.end();1383});1384test('.snapshot()', t => {1385 // Set to `true` to update the snapshot, then run:1386 // npx tap test-tap/assert.js1387 //1388 // Ignore errors and make sure not to run tests with the `-b` (bail) option.1389 const updating = false;1390 const projectDir = path.join(__dirname, 'fixture');1391 const manager = snapshotManager.load({1392 file: path.join(projectDir, 'assert.js'),1393 projectDir,1394 fixedLocation: null,1395 recordNewSnapshots: updating,1396 updating1397 });1398 const setup = _title => {1399 return new class extends assertions.constructor {1400 constructor(title) {1401 super({1402 compareWithSnapshot: assertionOptions => {1403 const {record, ...result} = manager.compare({1404 belongsTo: this.title,1405 expected: assertionOptions.expected,1406 index: this.snapshotInvocationCount++,1407 label: assertionOptions.message || `Snapshot ${this.snapshotInvocationCount}`1408 });1409 if (record) {1410 record();1411 }1412 return result;1413 }1414 });1415 this.title = title;1416 this.snapshotInvocationCount = 0;1417 }1418 }(_title);1419 };1420 {1421 const assertions = setup('passes');1422 passes(t, () => {1423 assertions.snapshot({foo: 'bar'});1424 });1425 passes(t, () => {1426 const {snapshot} = assertions;1427 snapshot({foo: 'bar'});1428 });1429 }1430 {1431 const assertions = setup('fails');1432 if (updating) {1433 assertions.snapshot({foo: 'bar'});1434 } else {1435 failsWith(t, () => {1436 assertions.snapshot({foo: 'not bar'});1437 }, {1438 assertion: 'snapshot',1439 message: 'Did not match snapshot',1440 values: [{label: 'Difference:', formatted: ' {\n- foo: \'not bar\',\n+ foo: \'bar\',\n }'}]1441 });1442 }1443 }1444 {1445 const assertions = setup('fails');1446 if (updating) {1447 assertions.snapshot({foo: 'bar'}, 'my message');1448 } else {1449 failsWith(t, () => {1450 assertions.snapshot({foo: 'not bar'}, 'my message');1451 }, {1452 assertion: 'snapshot',1453 message: 'my message',1454 values: [{label: 'Difference:', formatted: ' {\n- foo: \'not bar\',\n+ foo: \'bar\',\n }'}]1455 });1456 }1457 }1458 {1459 const assertions = setup('bad message');1460 failsWith(t, () => {1461 assertions.snapshot(null, null);1462 }, {1463 assertion: 'snapshot',1464 improperUsage: true,1465 message: 'The assertion message must be a string',1466 values: [{1467 label: 'Called with:',1468 formatted: /null/1469 }]1470 });1471 failsWith(t, () => {1472 assertions.snapshot(null, '');1473 }, {1474 assertion: 'snapshot',1475 improperUsage: true,1476 message: 'The snapshot assertion message must be a non-empty string',1477 values: [{1478 label: 'Called with:',1479 formatted: '\'\''1480 }]1481 });1482 }1483 {1484 // See https://github.com/avajs/ava/issues/26691485 const assertions = setup('id');1486 failsWith(t, () => {1487 assertions.snapshot({foo: 'bar'}, {id: 'an id'});1488 }, {1489 assertion: 'snapshot',1490 improperUsage: true,1491 message: 'AVA 4 no longer supports snapshot IDs',1492 values: [{1493 label: 'Called with id:',1494 formatted: '\'an id\''1495 }]1496 });1497 }1498 manager.save();1499 t.end();1500});1501test('.truthy()', t => {1502 failsWith(t, () => {1503 assertions.truthy(0);1504 }, {1505 assertion: 'truthy',1506 message: '',1507 operator: '!!',1508 values: [{label: 'Value is not truthy:', formatted: /0/}]1509 });1510 failsWith(t, () => {1511 assertions.truthy(false, 'my message');1512 }, {1513 assertion: 'truthy',1514 message: 'my message',1515 operator: '!!',1516 values: [{label: 'Value is not truthy:', formatted: /false/}]1517 });1518 passes(t, () => {1519 assertions.truthy(1);1520 assertions.truthy(true);1521 });1522 passes(t, () => {1523 const {truthy} = assertions;1524 truthy(1);1525 truthy(true);1526 });1527 failsWith(t, () => {1528 assertions.truthy(true, null);1529 }, {1530 assertion: 'truthy',1531 improperUsage: true,1532 message: 'The assertion message must be a string',1533 values: [{1534 label: 'Called with:',1535 formatted: /null/1536 }]1537 });1538 t.end();1539});1540test('.falsy()', t => {1541 failsWith(t, () => {1542 assertions.falsy(1);1543 }, {1544 assertion: 'falsy',1545 message: '',1546 operator: '!',1547 values: [{label: 'Value is not falsy:', formatted: /1/}]1548 });1549 failsWith(t, () => {1550 assertions.falsy(true, 'my message');1551 }, {1552 assertion: 'falsy',1553 message: 'my message',1554 operator: '!',1555 values: [{label: 'Value is not falsy:', formatted: /true/}]1556 });1557 passes(t, () => {1558 assertions.falsy(0);1559 assertions.falsy(false);1560 });1561 passes(t, () => {1562 const {falsy} = assertions;1563 falsy(0);1564 falsy(false);1565 });1566 failsWith(t, () => {1567 assertions.falsy(false, null);1568 }, {1569 assertion: 'falsy',1570 improperUsage: true,1571 message: 'The assertion message must be a string',1572 values: [{1573 label: 'Called with:',1574 formatted: /null/1575 }]1576 });1577 t.end();1578});1579test('.true()', t => {1580 failsWith(t, () => {1581 assertions.true(1);1582 }, {1583 assertion: 'true',1584 message: '',1585 values: [{label: 'Value is not `true`:', formatted: /1/}]1586 });1587 failsWith(t, () => {1588 assertions.true(0);1589 }, {1590 assertion: 'true',1591 message: '',1592 values: [{label: 'Value is not `true`:', formatted: /0/}]1593 });1594 failsWith(t, () => {1595 assertions.true(false);1596 }, {1597 assertion: 'true',1598 message: '',1599 values: [{label: 'Value is not `true`:', formatted: /false/}]1600 });1601 failsWith(t, () => {1602 assertions.true('foo', 'my message');1603 }, {1604 assertion: 'true',1605 message: 'my message',1606 values: [{label: 'Value is not `true`:', formatted: /foo/}]1607 });1608 passes(t, () => {1609 assertions.true(true);1610 });1611 passes(t, () => {1612 const {true: trueFn} = assertions;1613 trueFn(true);1614 });1615 failsWith(t, () => {1616 assertions.true(true, null);1617 }, {1618 assertion: 'true',1619 improperUsage: true,1620 message: 'The assertion message must be a string',1621 values: [{1622 label: 'Called with:',1623 formatted: /null/1624 }]1625 });1626 t.end();1627});1628test('.false()', t => {1629 failsWith(t, () => {1630 assertions.false(0);1631 }, {1632 assertion: 'false',1633 message: '',1634 values: [{label: 'Value is not `false`:', formatted: /0/}]1635 });1636 failsWith(t, () => {1637 assertions.false(1);1638 }, {1639 assertion: 'false',1640 message: '',1641 values: [{label: 'Value is not `false`:', formatted: /1/}]1642 });1643 failsWith(t, () => {1644 assertions.false(true);1645 }, {1646 assertion: 'false',1647 message: '',1648 values: [{label: 'Value is not `false`:', formatted: /true/}]1649 });1650 failsWith(t, () => {1651 assertions.false('foo', 'my message');1652 }, {1653 assertion: 'false',1654 message: 'my message',1655 values: [{label: 'Value is not `false`:', formatted: /foo/}]1656 });1657 passes(t, () => {1658 assertions.false(false);1659 });1660 passes(t, () => {1661 const {false: falseFn} = assertions;1662 falseFn(false);1663 });1664 failsWith(t, () => {1665 assertions.false(false, null);1666 }, {1667 assertion: 'false',1668 improperUsage: true,1669 message: 'The assertion message must be a string',1670 values: [{1671 label: 'Called with:',1672 formatted: /null/1673 }]1674 });1675 t.end();1676});1677test('.regex()', t => {1678 passes(t, () => {1679 assertions.regex('abc', /^abc$/);1680 });1681 passes(t, () => {1682 const {regex} = assertions;1683 regex('abc', /^abc$/);1684 });1685 failsWith(t, () => {1686 assertions.regex('foo', /^abc$/);1687 }, {1688 assertion: 'regex',1689 message: '',1690 values: [1691 {label: 'Value must match expression:', formatted: /foo/},1692 {label: 'Regular expression:', formatted: /\/\^abc\$\//}1693 ]1694 });1695 failsWith(t, () => {1696 assertions.regex('foo', /^abc$/, 'my message');1697 }, {1698 assertion: 'regex',1699 message: 'my message',1700 values: [1701 {label: 'Value must match expression:', formatted: /foo/},1702 {label: 'Regular expression:', formatted: /\/\^abc\$\//}1703 ]1704 });1705 failsWith(t, () => {1706 assertions.regex('foo', /^abc$/, null);1707 }, {1708 assertion: 'regex',1709 improperUsage: true,1710 message: 'The assertion message must be a string',1711 values: [{1712 label: 'Called with:',1713 formatted: /null/1714 }]1715 });1716 t.end();1717});1718test('.regex() fails if passed a bad value', t => {1719 failsWith(t, () => {1720 assertions.regex(42, /foo/);1721 }, {1722 assertion: 'regex',1723 improperUsage: true,1724 message: '`t.regex()` must be called with a string',1725 values: [{label: 'Called with:', formatted: /42/}]1726 });1727 failsWith(t, () => {1728 assertions.regex('42', {});1729 }, {1730 assertion: 'regex',1731 message: '`t.regex()` must be called with a regular expression',1732 values: [{label: 'Called with:', formatted: /{}/}]1733 });1734 t.end();1735});1736test('.notRegex()', t => {1737 passes(t, () => {1738 assertions.notRegex('abc', /def/);1739 });1740 passes(t, () => {1741 const {notRegex} = assertions;1742 notRegex('abc', /def/);1743 });1744 failsWith(t, () => {1745 assertions.notRegex('abc', /abc/);1746 }, {1747 assertion: 'notRegex',1748 message: '',1749 values: [1750 {label: 'Value must not match expression:', formatted: /abc/},1751 {label: 'Regular expression:', formatted: /\/abc\//}1752 ]1753 });1754 failsWith(t, () => {1755 assertions.notRegex('abc', /abc/, 'my message');1756 }, {1757 assertion: 'notRegex',1758 message: 'my message',1759 values: [1760 {label: 'Value must not match expression:', formatted: /abc/},1761 {label: 'Regular expression:', formatted: /\/abc\//}1762 ]1763 });1764 failsWith(t, () => {1765 assertions.notRegex('abc', /abc/, null);1766 }, {1767 assertion: 'notRegex',1768 improperUsage: true,1769 message: 'The assertion message must be a string',1770 values: [{1771 label: 'Called with:',1772 formatted: /null/1773 }]1774 });1775 t.end();1776});1777test('.notRegex() fails if passed a bad value', t => {1778 failsWith(t, () => {1779 assertions.notRegex(42, /foo/);1780 }, {1781 assertion: 'notRegex',1782 message: '`t.notRegex()` must be called with a string',1783 values: [{label: 'Called with:', formatted: /42/}]1784 });1785 failsWith(t, () => {1786 assertions.notRegex('42', {});1787 }, {1788 assertion: 'notRegex',1789 message: '`t.notRegex()` must be called with a regular expression',1790 values: [{label: 'Called with:', formatted: /{}/}]1791 });1792 t.end();1793});1794test('.assert()', t => {1795 failsWith(t, () => {1796 assertions.assert(0);1797 }, {1798 assertion: 'assert',1799 message: '',1800 operator: '!!',1801 values: [{label: 'Value is not truthy:', formatted: /0/}]1802 });1803 failsWith(t, () => {1804 assertions.assert(false, 'my message');1805 }, {1806 assertion: 'assert',1807 message: 'my message',1808 operator: '!!',1809 values: [{label: 'Value is not truthy:', formatted: /false/}]1810 });1811 passes(t, () => {1812 assertions.assert(1);1813 assertions.assert(true);1814 });1815 passes(t, () => {1816 const {assert} = assertions;1817 assert(1);1818 assert(true);1819 });1820 failsWith(t, () => {1821 assertions.assert(null, null);1822 }, {1823 assertion: 'assert',1824 improperUsage: true,1825 message: 'The assertion message must be a string',1826 values: [{1827 label: 'Called with:',1828 formatted: /null/1829 }]1830 });1831 t.end();...

Full Screen

Full Screen

parser.js

Source:parser.js Github

copy

Full Screen

...17 } else {18 return _.isEqual(r1, r2);19 }20}21function failsWith(p, tokens, message) {22 try {23 p.parse(tokens);24 return false;25 } catch (exc) {26 return message === undefined || exc.message === message;27 }28}29describe("parser", function () {30 describe("pure", function () {31 jsc.property("always succeeds", "nat", "array nat", function (n, tokens) {32 var p = parser.pure(n);33 return p.parse(tokens) === n;34 });35 });36 describe("fail", function () {37 jsc.property("always fails", "string", "array nat", function (err, tokens) {38 var p = parser.fail(err);39 return failsWith(p, tokens, err);40 });41 });42 describe("map", function () {43 jsc.property("maps successful result", "nat", "nat -> nat", function (n, f) {44 var p = parser.pure(n).map(f);45 return p.parse([]) === f(n);46 });47 jsc.property("is chainable", "nat", "nat -> nat", "nat -> nat", function (n, f, g) {48 var p = parser.pure(n).map(f).map(g);49 return p.parse([]) === g(f(n));50 });51 jsc.property("forms functor", "nat", "nat -> nat", "nat -> nat", function (n, f, g) {52 var p1 = parser.pure(n).map(f).map(g);53 var p2 = parser.pure(n).map(_.compose(g, f));54 var x1 = p1.parse([]);55 var x2 = p2.parse([]);56 return x1 === x2 && x1 === g(f(n));57 });58 jsc.property("doesn't touch errorneous parse", "string", "nat -> nat", function (err, f) {59 var p = parser.fail(err).map(f);60 return failsWith(p, [], err);61 });62 });63 describe("any", function () {64 jsc.property("fails on empty input", function () {65 return failsWith(parser.any, []);66 });67 });68 describe("combine", function () {69 jsc.property("with single parser ≡ p.map(f)", "nat", "nat -> nat", function (n, f) {70 var p = parser.combine(parser.pure(n), f);71 return p.parse([]) === f(n);72 });73 jsc.property("with single parser ≡ p.map(f), error", "string", "nat -> nat", function (err, f) {74 var p = parser.combine(parser.fail(err), f);75 return failsWith(p, [], err);76 });77 jsc.property("applicative: identity law", "nat", function (n) {78 var p = parser.combine(parser.pure(_.identity), parser.pure(n), apply);79 return p.parse([]) === n;80 });81 jsc.property("applicative: identity law, any", "array nat", function (tokens) {82 var p1 = parser.combine(parser.pure(_.identity), parser.any, apply);83 var p2 = parser.any;84 var r1 = p1(tokens, 0);85 var r2 = p2(tokens, 0);86 return resultsEqual(r1, r2);87 });88 jsc.property("applicative: identity law, error", "string", function (err) {89 var p = parser.combine(parser.pure(_.identity), parser.fail(err), apply);90 return failsWith(p, [], err);91 });92 jsc.property("applicative: two errors - first returned", "string", "string", function (err1, err2) {93 var p = parser.combine(parser.fail(err1), parser.fail(err2), apply);94 return failsWith(p, [], err1);95 });96 jsc.property("applicative: homomorphism law", "nat -> nat", "nat", "array nat", function (f, n, tokens) {97 var p1 = parser.combine(parser.pure(f), parser.pure(n), apply);98 var p2 = parser.pure(f(n));99 var x1 = p1.parse(tokens);100 var x2 = p2.parse(tokens);101 var x3 = f(n);102 return x1 === x2 && x2 === x3;103 });104 jsc.property("parse two first nats", "array nat", function (tokens) {105 var p = parser.combine(parser.any, parser.any, function (a, b) { return a + b; });106 if (tokens.length < 2) {107 return failsWith(p, tokens);108 } else {109 return p.parse(tokens) === tokens[0] + tokens[1];110 }111 });112 jsc.property("parse two first nats, spread", "array nat", function (tokens) {113 var p = parser.combine([114 parser.any,115 parser.any,116 ], function (a, b) { return a + b; });117 if (tokens.length < 2) {118 return failsWith(p, tokens);119 } else {120 return p.parse(tokens) === tokens[0] + tokens[1];121 }122 });123 });124 describe("choice", function () {125 jsc.property("empty choice ≡ fail", "array nat", function (tokens) {126 var p = parser.choice();127 return failsWith(p, tokens);128 });129 jsc.property("empty choice ≡ fail, empty arrays as input", "array nat", function (tokens) {130 var p = parser.choice([], [], []);131 return failsWith(p, tokens);132 });133 jsc.property("fail is left identity", "array nat", function (tokens) {134 var p1 = parser.choice(parser.fail("err"), parser.any);135 var p2 = parser.any;136 var r1 = p1(tokens, 0);137 var r2 = p2(tokens, 0);138 return resultsAlmostEqual(r1, r2);139 });140 jsc.property("fail is right identity", "array nat", function (tokens) {141 var p1 = parser.choice(parser.any, parser.fail("err"));142 var p2 = parser.any;143 var r1 = p1(tokens, 0);144 var r2 = p2(tokens, 0);145 return resultsEqual(r1, r2);146 });147 jsc.property("may cause combinatorial explosion", "array nat", function (tokens) {148 var p = parser.choice(parser.any, parser.any);149 if (tokens.length < 1) {150 return failsWith(p, tokens);151 } else {152 return p.parse(tokens) === tokens[0];153 }154 });155 jsc.property("may cause combinatorial explosion, 2", "array nat", function (tokens) {156 var one = parser.choice(parser.any, parser.any);157 var p = parser.combine(one, one, function (a, b) { return a + b; });158 if (tokens.length < 2) {159 return failsWith(p, tokens);160 } else {161 var r = p(tokens, 0);162 return r.length === 4 && r.every(function (x) {163 return x[0] === tokens[0] + tokens[1];164 });165 }166 });167 });168 describe("uniq", function () {169 jsc.property("may help with combinatorial explosion, 1", "array nat", function (tokens) {170 var one = parser.choice(parser.any, parser.any);171 var p = parser.combine(one.uniq(), one, function (a, b) { return a + b; });172 if (tokens.length < 2) {173 return failsWith(p, tokens);174 } else {175 var r = p(tokens, 0);176 return r.length === 2 && r.every(function (x) {177 return x[0] === tokens[0] + tokens[1];178 });179 }180 });181 jsc.property("may help with combinatorial explosion, 2", "array nat", function (tokens) {182 var one = parser.choice(parser.any, parser.any);183 var p = parser.combine(one, one.uniq(), function (a, b) { return a + b; });184 if (tokens.length < 2) {185 return failsWith(p, tokens);186 } else {187 var r = p(tokens, 0);188 return r.length === 2 && r.every(function (x) {189 return x[0] === tokens[0] + tokens[1];190 });191 }192 });193 jsc.property("may help with combinatorial explosion, 2", "array nat", function (tokens) {194 var one = parser.choice(parser.any, parser.any);195 var p = parser.combine(one, one, function (a, b) { return a + b; }).uniq();196 if (tokens.length < 2) {197 return failsWith(p, tokens);198 } else {199 var r = p(tokens, 0);200 return r.length === 1 && r[0][0] === tokens[0] + tokens[1];201 }202 });203 });204 describe("satisfy", function () {205 jsc.property("p.satisfy(constant(false)) ≡ fail", "nat", "array nat", function (n, tokens) {206 var p = parser.pure(n).satisfy(function () { return false; });207 return failsWith(p, tokens);208 });209 jsc.property("p.satisfy(constant(true)) ≡ p", "array nat", function (tokens) {210 var p1 = parser.any;211 var p2 = p1.satisfy(function () { return true; });212 var r1 = p1(tokens, 0);213 var r2 = p2(tokens, 0);214 return resultsEqual(r1, r2);215 });216 jsc.property("is idempotent", "nat -> bool", "array nat", function (predicate, tokens) {217 var p1 = parser.any.satisfy(predicate);218 var p2 = p1.satisfy(predicate);219 var r1 = p1(tokens, 0);220 var r2 = p2(tokens, 0);221 return resultsEqual(r1, r2);222 });223 });224 describe("end", function () {225 jsc.property("succeeds if on end-of-input", "nat", function (n) {226 var p = parser.pure(n).end();227 return p.parse([]) === n;228 });229 jsc.property("fails if there is input", "nat", "nat", function (n, m) {230 var p = parser.pure(n).end();231 return failsWith(p, [m]);232 });233 });234 describe("many", function () {235 jsc.property("any.many.end returns token list as is", "array nat", function (tokens) {236 var p = parser.any.many().end();237 return _.isEqual(p.parse(tokens), tokens);238 });239 jsc.property("any.many returns as much results as there are tokens + empty one", "array nat", function (tokens) {240 var p = parser.any.many();241 return p(tokens, 0).length === tokens.length + 1;242 });243 jsc.property("many.many fails", function () {244 try {245 parser.any.many().many();246 return false;247 } catch (exc) {248 return true;249 }250 });251 });252 describe("some", function () {253 jsc.property("any.some.end returns non-empty token list as is", "nearray nat", function (tokens) {254 var p = parser.any.some().end();255 return _.isEqual(p.parse(tokens), tokens);256 });257 jsc.property("some fails on empty input", function () {258 var p = parser.any.some().end();259 return failsWith(p, []);260 });261 jsc.property("many.some fails", function () {262 try {263 parser.any.many().some();264 return false;265 } catch (exc) {266 return true;267 }268 });269 });270 describe("left", function () {271 jsc.property("a.left(b) ≡ combine(a, b, (x, y) ⇒ x)", "array nat", function (tokens) {272 var a = parser.any;273 var b = parser.any;274 var p1 = a.left(b);275 var p2 = parser.combine(a, b, function (x) { return x; });276 var r1 = p1(tokens, 0);277 var r2 = p2(tokens, 0);278 return resultsEqual(r1, r2);279 });280 jsc.property("a.left(b) ≡ combine(a, b, (x, y) ⇒ x), choice", "array nat", function (tokens) {281 var a = parser.choice(parser.any, parser.any);282 var b = parser.choice(parser.any, parser.any);283 var p1 = a.left(b);284 var p2 = parser.combine(a, b.uniq(), function (x) { return x; });285 var r1 = p1(tokens, 0);286 var r2 = p2(tokens, 0);287 return resultsEqual(r1, r2);288 });289 jsc.property("with pure on left", "nat", "array nat", function (n, tokens) {290 var p1 = parser.any.left(parser.pure(n));291 var p2 = parser.any;292 var r1 = p1(tokens, 0);293 var r2 = p2(tokens, 0);294 return resultsEqual(r1, r2);295 });296 jsc.property("with pure on right", "nat", "array nat", function (n, tokens) {297 var p = parser.pure(n).left(parser.any);298 if (tokens.length === 0) {299 return failsWith(p, tokens);300 } else {301 return p.parse(tokens) === n;302 }303 });304 });305 describe("right", function () {306 jsc.property("a.right(b) ≡ combine(a, b, (x, y) ⇒ y)", "array nat", function (tokens) {307 var a = parser.any;308 var b = parser.any;309 var p1 = a.right(b);310 var p2 = parser.combine(a, b, function (x, y) { return y; });311 var r1 = p1(tokens, 0);312 var r2 = p2(tokens, 0);313 return resultsEqual(r1, r2);314 });315 jsc.property("a.right(b) ≡ combine(a, b, (x, y) ⇒ y), choice", "array nat", function (tokens) {316 var a = parser.choice(parser.any, parser.any);317 var b = parser.choice(parser.any, parser.any);318 var p1 = a.right(b);319 var p2 = parser.combine(a.uniq(), b, function (x, y) { return y; });320 var r1 = p1(tokens, 0);321 var r2 = p2(tokens, 0);322 return resultsEqual(r1, r2);323 });324 jsc.property("a.right(b) ≡ combine(a, b, (x, y) ⇒ y), special", "array nat", function (tokens) {325 var a = parser.choice(parser.any, parser.any.left(parser.any));326 var b = parser.any.left(parser.any);327 var p1 = a.right(b);328 var p2 = parser.combine(a, b, function (x, y) { return y; });329 var r1 = p1(tokens, 0);330 var r2 = p2(tokens, 0);331 return resultsEqual(r1, r2);332 });333 jsc.property("with pure on left", "nat", "array nat", function (n, tokens) {334 var p = parser.any.right(parser.pure(n));335 if (tokens.length === 0) {336 return failsWith(p, tokens);337 } else {338 return p.parse(tokens) === n;339 }340 });341 jsc.property("with pure on right", "nat", "array nat", function (n, tokens) {342 var p1 = parser.pure(n).right(parser.any);343 var p2 = parser.any;344 var r1 = p1(tokens, 0);345 var r2 = p2(tokens, 0);346 return resultsEqual(r1, r2);347 });348 });349 describe("lazy", function () {350 var parser0 = parser.any.satisfy(function (x) { return x === 0; });...

Full Screen

Full Screen

quantityValidator.js

Source:quantityValidator.js Github

copy

Full Screen

...24) => {25 let error = '';26 const isValid = !validators.some((validator) => {27 if (28 validator.failsWith({29 previousQuantity,30 newQuantity,31 cartTotalQuantity,32 })33 ) {34 error = validator.errorMsg;35 return true;36 }37 return false;38 });39 return { isValid, error };...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import test from 'ava';2test('foo', t => {3 t.failsWith(() => {4 throw new Error('bar');5 }, 'bar');6});7import test from 'ava';8test('foo', t => {9 t.failsWith(() => {10 throw new Error('bar');11 }, 'bar');12});13import test from 'ava';14test('foo', t => {15 t.failsWith(() => {16 throw new Error('bar');17 }, 'bar');18});19import test from 'ava';20test('foo', t => {21 t.failsWith(() => {22 throw new Error('bar');23 }, 'bar');24});25import test from 'ava';26test('foo', t => {27 t.failsWith(() => {28 throw new Error('bar');29 }, 'bar');30});31import test from 'ava';32test('foo', t => {33 t.failsWith(() => {34 throw new Error('bar');35 }, 'bar');36});37import test from 'ava';38test('foo', t => {39 t.failsWith(() => {40 throw new Error('bar');41 }, 'bar');42});43import test from 'ava';44test('foo', t => {45 t.failsWith(() => {46 throw new Error('bar');47 }, 'bar');48});49import test from 'ava';50test('foo', t => {51 t.failsWith(() => {52 throw new Error('bar');53 }, 'bar');54});55import test from 'ava';56test('foo', t => {57 t.failsWith(() => {58 throw new Error('bar');59 }, 'bar');60});

Full Screen

Using AI Code Generation

copy

Full Screen

1import test from 'ava';2import { failsWith } from 'ava-assertions';3test('failsWith', async t => {4 const error = await failsWith(() => {5 throw new Error('error');6 });7 t.is(error.message, 'error');8 t.is(error instanceof Error, true);9});10import test from 'ava';11import { failsWith } from 'ava-assertions';12test('failsWith', async t => {13 const error = await failsWith(() => {14 throw new Error('error');15 });16 t.is(error.message, 'error');17 t.is(error instanceof Error, true);18});19import test from 'ava';20import { failsWith } from 'ava-assertions';21test('failsWith', async t => {22 const error = await failsWith(() => {23 throw new Error('error');24 });25 t.is(error.message, 'error');26 t.is(error instanceof Error, true);27});28import test from 'ava';29import { failsWith } from 'ava-assertions';30test('failsWith', async t => {31 const error = await failsWith(() => {32 throw new Error('error');33 });34 t.is(error.message, 'error');35 t.is(error instanceof Error, true);36});37import test from 'ava';38import { failsWith } from 'ava-assertions';39test('failsWith', async t => {40 const error = await failsWith(() => {41 throw new Error('error');42 });43 t.is(error.message, 'error');44 t.is(error instanceof Error, true);45});46import test from 'ava';47import { failsWith } from 'ava-assertions';48test('failsWith', async t => {49 const error = await failsWith(() => {50 throw new Error('error');51 });52 t.is(error.message, 'error');53 t.is(error instanceof Error, true);54});55import test from 'ava';56import { failsWith } from

Full Screen

Using AI Code Generation

copy

Full Screen

1const test = require('ava')2test('foo', t => {3 t.failsWith(() => {4 throw new TypeError('foo')5 }, TypeError)6})7const test = require('ava')8test('foo', async t => {9 const bar = Promise.resolve('bar')10 t.throwsAsync(bar, 'bar')11})12const test = require('ava')13test('foo', async t => {14 const bar = Promise.resolve('bar')15 t.notThrowsAsync(bar)16})17const test = require('ava')18test('foo', async t => {19 const bar = Promise.resolve('bar')20 t.failsWithAsync(bar, 'bar')21})22const test = require('ava')23test('foo', t => {24 t.regex('I love unicorns', /love/)25})26const test = require('ava')27test('foo', t => {28 t.notRegex('I love unicorns', /rainbows/)29})30const test = require('ava')31test('foo', t => {32 t.ifError(null)33})34const test = require('ava')35test('foo', t => {36 t.snapshot('unicorn')37})38const test = require('ava')39test('foo', t => {40 t.log('Hello')41})42const test = require('ava')

Full Screen

Using AI Code Generation

copy

Full Screen

1import test from 'ava';2import {failsWith} from 'ava-utility';3test('failsWith', t => {4 failsWith(t, () => {5 throw new Error('foo');6 }, 'foo');7});8import {failsWith} from 'ava-utility';9failsWith(t => {10 throw new Error('foo');11}, 'foo');12import test from 'ava';13import {failsWith} from 'ava-utility';14test('failsWith', t => {15 failsWith(t, () => {16 throw new Error('foo');17 }, 'foo');18});19MIT © [Siddharth Kshetrapal](

Full Screen

Using AI Code Generation

copy

Full Screen

1import test from 'ava';2import { failsWith } from 'ava-assertions';3import { foo } from './foo';4test('foo throws an error', async t => {5 const error = await failsWith(t, foo());6 t.is(error.message, 'foo error');7});8### `failsWith(t, promise, [message])`9- [ava-assertions](

Full Screen

Using AI Code Generation

copy

Full Screen

1const test = require('ava')2const { failsWith } = require('fails-with')3const { get } = require('axios')4test('fails with 404', async t => {5 await failsWith(6})7test('fails with 404', async t => {8 await failsWith(9})10test('fails with 404', async t => {11 await failsWith(12})13const test = require('ava')14const { failsWith } = require('fails-with')15const { get } = require('axios')16test('fails with 404', async t => {17 await failsWith(18})19test('fails with 404', async t => {20 await failsWith(21})22test('fails with 404', async t => {23 await failsWith(24})25const test = require('ava')26const { failsWith } = require('fails-with')27const { get } = require('axios')28test('fails with 404', async t => {29 await failsWith(30})31test('fails with 404', async t => {32 await failsWith(

Full Screen

Using AI Code Generation

copy

Full Screen

1import test from 'ava';2import { failsWith } from 'ava-pattern-match';3import { add } from './index';4test('add should throw an error when passed a non-number', t => {5 failsWith(t, add(1, 'foo'), 'foo is not a number');6});7export function add(a, b) {8 if (typeof a !== 'number') {9 throw new Error('a is not a number');10 }11 if (typeof b !== 'number') {12 throw new Error('b is not a number');13 }14 return a + b;15}16### `failsWith(t, fn, pattern)`17- [ava-pattern-match](

Full Screen

Using AI Code Generation

copy

Full Screen

1import test from 'ava';2import { expect } from 'chai';3import { failsWith } from 'chai-fail-fast';4test('failsWith', t => {5 expect(() => {6 t.fail('Error');7 }).to.failsWith('Error');8});9import { expect } from 'chai';10import 'chai-fail-fast';11describe('FailFast', () => {12 it('should fail fast', () => {13 expect(() => {14 throw new Error('Error');15 }).to.throw('Error');16 });17});

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