How to use notThrowsAsyncFails method in ava

Best JavaScript code snippet using ava

assert.js

Source:assert.js Github

copy

Full Screen

...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();858	});859});860test('.throwsAsync() returns the rejection reason of a promise returned by the function', t => {861	const expected = new Error();862	return assertions.throwsAsync(() => Promise.reject(expected)).then(actual => {863		t.equal(actual, expected);864		t.end();865	});866});867test('.throws() fails if passed a bad value', t => {868	failsWith(t, () => assertions.throws('not a function'), {869		assertion: 'throws',870		message: '`t.throws()` must be called with a function',871		values: [{label: 'Called with:', formatted: /not a function/}],872	});873	t.end();874});875test('.throwsAsync() fails if passed a bad value', t => {876	failsWith(t, () => assertions.throwsAsync('not a function'), {877		assertion: 'throwsAsync',878		message: '`t.throwsAsync()` must be called with a function or promise',879		values: [{label: 'Called with:', formatted: /not a function/}],880	}, {expectBoolean: false});881	t.end();882});883test('.throws() fails if passed a bad expectation', t => {884	failsWith(t, () => assertions.throws(() => {}, true), {885		assertion: 'throws',886		message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`',887		values: [{label: 'Called with:', formatted: /true/}],888	});889	failsWith(t, () => assertions.throws(() => {}, 'foo'), {890		assertion: 'throws',891		message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`',892		values: [{label: 'Called with:', formatted: /foo/}],893	});894	failsWith(t, () => assertions.throws(() => {}, /baz/), {895		assertion: 'throws',896		message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`',897		values: [{label: 'Called with:', formatted: /baz/}],898	});899	failsWith(t, () => assertions.throws(() => {}, class Bar {}), {900		assertion: 'throws',901		message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`',902		values: [{label: 'Called with:', formatted: /Bar/}],903	});904	failsWith(t, () => assertions.throws(() => {}, {}), {905		assertion: 'throws',906		message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`',907		values: [{label: 'Called with:', formatted: /{}/}],908	});909	failsWith(t, () => assertions.throws(() => {}, []), {910		assertion: 'throws',911		message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`',912		values: [{label: 'Called with:', formatted: /\[]/}],913	});914	failsWith(t, () => assertions.throws(() => {}, {code: {}}), {915		assertion: 'throws',916		message: 'The `code` property of the second argument to `t.throws()` must be a string or number',917		values: [{label: 'Called with:', formatted: /code: {}/}],918	});919	failsWith(t, () => assertions.throws(() => {}, {instanceOf: null}), {920		assertion: 'throws',921		message: 'The `instanceOf` property of the second argument to `t.throws()` must be a function',922		values: [{label: 'Called with:', formatted: /instanceOf: null/}],923	});924	failsWith(t, () => assertions.throws(() => {}, {message: null}), {925		assertion: 'throws',926		message: 'The `message` property of the second argument to `t.throws()` must be a string, regular expression or a function',927		values: [{label: 'Called with:', formatted: /message: null/}],928	});929	failsWith(t, () => assertions.throws(() => {}, {name: null}), {930		assertion: 'throws',931		message: 'The `name` property of the second argument to `t.throws()` must be a string',932		values: [{label: 'Called with:', formatted: /name: null/}],933	});934	failsWith(t, () => assertions.throws(() => {}, {is: {}, message: '', name: '', of() {}, foo: null}), {935		assertion: 'throws',936		message: 'The second argument to `t.throws()` contains unexpected properties',937		values: [{label: 'Called with:', formatted: /foo: null/}],938	});939	t.end();940});941test('.throwsAsync() fails if passed a bad expectation', t => {942	failsWith(t, () => assertions.throwsAsync(() => {}, true), {943		assertion: 'throwsAsync',944		message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`',945		values: [{label: 'Called with:', formatted: /true/}],946	}, {expectBoolean: false});947	failsWith(t, () => assertions.throwsAsync(() => {}, 'foo'), {948		assertion: 'throwsAsync',949		message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`',950		values: [{label: 'Called with:', formatted: /foo/}],951	}, {expectBoolean: false});952	failsWith(t, () => assertions.throwsAsync(() => {}, /baz/), {953		assertion: 'throwsAsync',954		message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`',955		values: [{label: 'Called with:', formatted: /baz/}],956	}, {expectBoolean: false});957	failsWith(t, () => assertions.throwsAsync(() => {}, class Bar {}), {958		assertion: 'throwsAsync',959		message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`',960		values: [{label: 'Called with:', formatted: /Bar/}],961	}, {expectBoolean: false});962	failsWith(t, () => assertions.throwsAsync(() => {}, {}), {963		assertion: 'throwsAsync',964		message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`',965		values: [{label: 'Called with:', formatted: /{}/}],966	}, {expectBoolean: false});967	failsWith(t, () => assertions.throwsAsync(() => {}, []), {968		assertion: 'throwsAsync',969		message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`',970		values: [{label: 'Called with:', formatted: /\[]/}],971	}, {expectBoolean: false});972	failsWith(t, () => assertions.throwsAsync(() => {}, {code: {}}), {973		assertion: 'throwsAsync',974		message: 'The `code` property of the second argument to `t.throwsAsync()` must be a string or number',975		values: [{label: 'Called with:', formatted: /code: {}/}],976	}, {expectBoolean: false});977	failsWith(t, () => assertions.throwsAsync(() => {}, {instanceOf: null}), {978		assertion: 'throwsAsync',979		message: 'The `instanceOf` property of the second argument to `t.throwsAsync()` must be a function',980		values: [{label: 'Called with:', formatted: /instanceOf: null/}],981	}, {expectBoolean: false});982	failsWith(t, () => assertions.throwsAsync(() => {}, {message: null}), {983		assertion: 'throwsAsync',984		message: 'The `message` property of the second argument to `t.throwsAsync()` must be a string, regular expression or a function',985		values: [{label: 'Called with:', formatted: /message: null/}],986	}, {expectBoolean: false});987	failsWith(t, () => assertions.throwsAsync(() => {}, {name: null}), {988		assertion: 'throwsAsync',989		message: 'The `name` property of the second argument to `t.throwsAsync()` must be a string',990		values: [{label: 'Called with:', formatted: /name: null/}],991	}, {expectBoolean: false});992	failsWith(t, () => assertions.throwsAsync(() => {}, {is: {}, message: '', name: '', of() {}, foo: null}), {993		assertion: 'throwsAsync',994		message: 'The second argument to `t.throwsAsync()` contains unexpected properties',995		values: [{label: 'Called with:', formatted: /foo: null/}],996	}, {expectBoolean: false});997	t.end();998});999test('.throws() fails if passed null expectation', t => {1000	const asserter = new AssertionsBase();1001	failsWith(t, () => {1002		asserter.throws(() => {}, null);1003	}, {1004		assertion: 'throws',1005		message: 'The second argument to `t.throws()` must be an expectation object or `undefined`',1006		values: [{label: 'Called with:', formatted: /null/}],1007	});1008	t.end();1009});1010test('.throwsAsync() fails if passed null', t => {1011	const asserter = new AssertionsBase();1012	failsWith(t, () => {1013		asserter.throwsAsync(() => {}, null);1014	}, {1015		assertion: 'throwsAsync',1016		message: 'The second argument to `t.throwsAsync()` must be an expectation object or `undefined`',1017		values: [{label: 'Called with:', formatted: /null/}],1018	});1019	t.end();1020});1021test('.notThrows()', gather(t => {1022	// Passes because the function doesn't throw1023	passes(t, () => assertions.notThrows(() => {}), {expectBoolean: false});1024	passes(t, () => assertions.notThrows(() => {}), {expectBoolean: false});1025	// Fails because the function throws.1026	failsWith(t, () => assertions.notThrows(() => {1027		throw new Error('foo');1028	}), {1029		assertion: 'notThrows',1030		message: '',1031		values: [{label: 'Function threw:', formatted: /foo/}],1032	}, {expectBoolean: false});1033	// Fails because the function throws. Asserts that message is used for the1034	// assertion, not to validate the thrown error.1035	failsWith(t, () => assertions.notThrows(() => {1036		throw new Error('foo');1037	}, 'my message'), {1038		assertion: 'notThrows',1039		message: 'my message',1040		values: [{label: 'Function threw:', formatted: /foo/}],1041	}, {expectBoolean: false});1042	failsWith(t, () => assertions.notThrows(() => {}, null), {1043		assertion: 'notThrows',1044		improperUsage: true,1045		message: 'The assertion message must be a string',1046		values: [{1047			label: 'Called with:',1048			formatted: /null/,1049		}],1050	}, {expectBoolean: false});1051}));1052test('.notThrowsAsync()', gather(t => {1053	// Passes because the promise is resolved1054	notThrowsAsyncPasses(t, () => assertions.notThrowsAsync(Promise.resolve()));1055	notThrowsAsyncPasses(t, () => assertions.notThrowsAsync(Promise.resolve()));1056	// Fails because the promise is rejected1057	notThrowsAsyncFails(t, () => assertions.notThrowsAsync(Promise.reject(new Error())), {1058		assertion: 'notThrowsAsync',1059		message: '',1060		values: [{label: 'Promise rejected with:', formatted: /Error/}],1061	});1062	// Passes because the function returned a resolved promise1063	notThrowsAsyncPasses(t, () => assertions.notThrowsAsync(() => Promise.resolve()));1064	// Fails because the function returned a rejected promise1065	notThrowsAsyncFails(t, () => assertions.notThrowsAsync(() => Promise.reject(new Error())), {1066		assertion: 'notThrowsAsync',1067		message: '',1068		values: [{label: 'Returned promise rejected with:', formatted: /Error/}],1069	});1070	// Fails because the function throws synchronously1071	notThrowsAsyncFails(t, () => assertions.notThrowsAsync(() => {1072		throw new Error('sync');1073	}, 'message'), {1074		assertion: 'notThrowsAsync',1075		message: 'message',1076		values: [1077			{label: 'Function threw:', formatted: /Error/},1078		],1079	});1080	// Fails because the function did not return a promise1081	notThrowsAsyncFails(t, () => assertions.notThrowsAsync(() => {}, 'message'), {1082		assertion: 'notThrowsAsync',1083		message: 'message',1084		values: [1085			{label: 'Function did not return a promise. Use `t.notThrows()` instead:', formatted: /undefined/},1086		],1087	});1088	notThrowsAsyncFails(t, () => assertions.notThrowsAsync(Promise.resolve(), null), {1089		assertion: 'notThrowsAsync',1090		improperUsage: true,1091		message: 'The assertion message must be a string',1092		values: [{1093			label: 'Called with:',1094			formatted: /null/,1095		}],1096	});1097}));1098test('.notThrowsAsync() returns undefined for a fulfilled promise', t => assertions.notThrowsAsync(Promise.resolve(Symbol(''))).then(actual => {1099	t.equal(actual, undefined);1100}));1101test('.notThrowsAsync() returns undefined for a fulfilled promise returned by the function', t => assertions.notThrowsAsync(() => Promise.resolve(Symbol(''))).then(actual => {1102	t.equal(actual, undefined);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const test = require('ava');2const delay = require('delay');3test('foo', async t => {4    await delay(200);5    t.pass();6});7test('bar', async t => {8    const bar = Promise.resolve('bar');9    t.is(await bar, 'bar');10});11const test = require('ava');12const delay = require('delay');13test('foo', async t => {14    await delay(200);15    t.pass();16});17test('bar', async t => {18    const bar = Promise.reject(new Error('bar'));19    t.is(await bar, 'bar');20});21const test = require('ava');22const delay = require('delay');23test('foo', async t => {24    await delay(200);25    t.pass();26});27test('bar', async t => {28    const bar = Promise.resolve('bar');29    t.is(await bar, 'bar');30});31const test = require('ava');32const delay = require('delay');33test('foo', async t => {34    await delay(200);35    t.pass();36});37test('bar', async t => {38    const bar = Promise.reject(new Error('bar'));39    t.is(await bar, 'bar');40});41const test = require('ava');42const delay = require('delay');43test('foo', async t => {44    await delay(200);45    t.pass();46});47test('bar', async t => {48    const bar = Promise.resolve('bar');49    t.is(await bar, 'bar');50});51const test = require('ava');52const delay = require('delay');53test('foo', async t => {54    await delay(200);55    t.pass();56});57test('bar', async t => {58    const bar = Promise.reject(new Error('bar'));59    t.is(await bar, 'bar');60});61const test = require('ava');62const delay = require('delay');63test('foo', async t => {64    await delay(200);65    t.pass();66});67test('bar', async t => {68    const bar = Promise.resolve('bar');69    t.is(await bar, 'bar');70});

Full Screen

Using AI Code Generation

copy

Full Screen

1import test from 'ava';2import {notThrowsAsyncFails} from 'not-throws-async-fails';3test('notThrowsAsyncFails', async t => {4	await notThrowsAsyncFails(async () => {5		throw new Error('foo');6	}, 'foo');7});8### notThrowsAsyncFails(asyncFn, [expected], [message])9- [not-throws](

Full Screen

Using AI Code Generation

copy

Full Screen

1const test = require('ava');2const {notThrowsAsyncFails} = require('not-throws-async-fails');3test('notThrowsAsyncFails', async t => {4	await notThrowsAsyncFails(t, Promise.resolve(), 'Expected to fail');5});6- [not-throws](

Full Screen

Using AI Code Generation

copy

Full Screen

1import test from 'ava';2import { notThrowsAsyncFails } from 'ava-assert';3test('notThrowsAsyncFails', async t => {4    await notThrowsAsyncFails(async () => {5        throw new Error('error');6    }, 'error message');7});

Full Screen

Using AI Code Generation

copy

Full Screen

1const test = require('ava');2test('notThrowsAsync fails', async t => {3  await t.notThrowsAsync(Promise.reject(new Error('foo')));4});5   3: test('notThrowsAsync fails', async t => {6   4:   await t.notThrowsAsync(Promise.reject(new Error('foo')));7   5: });8  Error {9  }10const test = require('ava');11test('notThrowsAsync', async t => {12  await t.notThrowsAsync(Promise.resolve());13});14const test = require('ava');15test('notThrows', t => {16  t.notThrows(() => {17  });18});19const test = require('ava');20test('notThrows fails', t => {21  t.notThrows(() => {22    throw new Error('foo');23  });24});25   3: test('notThrows fails', t => {26   4:   t.notThrows(() => {27   5:     throw new Error('foo');28  Error {29  }30const test = require('ava');31test('notRegex', t => {32  t.notRegex('I love unicorns', /rainbows/);33});

Full Screen

Using AI Code Generation

copy

Full Screen

1const test = require('ava');2const {notThrowsAsyncFails} = require('not-throws-async-fails');3test('should not throw', async t => {4    await notThrowsAsyncFails(async () => {5        await Promise.resolve();6    }, 'error message');7});8test('should throw', async t => {9    await t.throwsAsync(notThrowsAsyncFails(async () => {10        await Promise.reject(new Error('error message'));11    }, 'error message'));12});13MIT © [Nikolay Solodovnik](

Full Screen

Using AI Code Generation

copy

Full Screen

1const test = require('ava');2const notThrowsAsyncFails = require('../lib/not-throws-async-fails');3test('notThrowsAsyncFails', async t => {4	await notThrowsAsyncFails(t, Promise.resolve(10), 'foo');5});6const test = require('ava');7const notThrowsAsyncFailsRegex = require('../lib/not-throws-async-fails-regex');8test('notThrowsAsyncFailsRegex', async t => {9	await notThrowsAsyncFailsRegex(t, Promise.resolve(10), /foo/);10});11const test = require('ava');12const notThrowsAsyncFailsSnapshot = require('../lib/not-throws-async-fails-snapshot');13test('notThrowsAsyncFailsSnapshot', async t => {14	await notThrowsAsyncFailsSnapshot(t, Promise.resolve(10));15});16const test = require('ava');17const notThrowsAsyncFailsRegexSnapshot = require('../lib/not-throws-async-fails-regex-snapshot');18test('notThrowsAsyncFailsRegexSnapshot', async t => {19	await notThrowsAsyncFailsRegexSnapshot(t, Promise.resolve(10), /foo/);20});21const test = require('ava');22const notThrowsAsyncFailsThrows = require('../lib/not-throws-async-fails-throws');23test('notThrowsAsyncFailsThrows', async t => {24	await notThrowsAsyncFailsThrows(t, Promise.resolve(10), TypeError);25});

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