Best JavaScript code snippet using ava
assert.js
Source:assert.js
...582});583test('.throws()', gather(t => {584 // Fails because function doesn't throw.585 failsWith(t, () => {586 assertions.throws(() => {});587 }, {588 assertion: 'throws',589 message: '',590 values: [{label: 'Function returned:', formatted: /undefined/}]591 });592 // Fails because function doesn't throw. Asserts that 'my message' is used593 // as the assertion message (*not* compared against the error).594 failsWith(t, () => {595 assertions.throws(() => {}, null, 'my message');596 }, {597 assertion: 'throws',598 message: 'my message',599 values: [{label: 'Function returned:', formatted: /undefined/}]600 });601 // Fails because the function returned a promise.602 failsWith(t, () => {603 assertions.throws(() => Promise.resolve());604 }, {605 assertion: 'throws',606 message: '',607 values: [{label: 'Function returned a promise. Use `t.throwsAsync()` instead:', formatted: /Promise/}]608 });609 // Fails because thrown exception is not an error610 failsWith(t, () => {611 assertions.throws(() => {612 const err = 'foo';613 throw err;614 });615 }, {616 assertion: 'throws',617 message: '',618 values: [619 {label: 'Function threw exception that is not an error:', formatted: /'foo'/}620 ]621 });622 // Fails because thrown error's message is not equal to 'bar'623 failsWith(t, () => {624 const err = new Error('foo');625 assertions.throws(() => {626 throw err;627 }, 'bar');628 }, {629 assertion: 'throws',630 message: '',631 values: [632 {label: 'Function threw unexpected exception:', formatted: /foo/},633 {label: 'Expected message to equal:', formatted: /bar/}634 ]635 });636 // Fails because thrown error is not the right instance637 failsWith(t, () => {638 const err = new Error('foo');639 assertions.throws(() => {640 throw err;641 }, class Foo {});642 }, {643 assertion: 'throws',644 message: '',645 values: [646 {label: 'Function threw unexpected exception:', formatted: /foo/},647 {label: 'Expected instance of:', formatted: /Foo/}648 ]649 });650 // Passes because thrown error's message is equal to 'bar'651 passes(t, () => {652 const err = new Error('foo');653 assertions.throws(() => {654 throw err;655 }, 'foo');656 });657 // Passes because an error is thrown.658 passes(t, () => {659 assertions.throws(() => {660 throw new Error('foo');661 });662 });663 // Passes because the correct error is thrown.664 passes(t, () => {665 const err = new Error('foo');666 assertions.throws(() => {667 throw err;668 }, {is: err});669 });670 // Fails because the thrown value is not an error671 fails(t, () => {672 const obj = {};673 assertions.throws(() => {674 throw obj;675 }, {is: obj});676 });677 // Fails because the thrown value is not the right one678 fails(t, () => {679 const err = new Error('foo');680 assertions.throws(() => {681 throw err;682 }, {is: {}});683 });684 // Passes because the correct error is thrown.685 passes(t, () => {686 assertions.throws(() => {687 throw new TypeError(); // eslint-disable-line unicorn/error-message688 }, {name: 'TypeError'});689 });690 // Fails because the thrown value is not an error691 fails(t, () => {692 assertions.throws(() => {693 const err = {name: 'Bob'};694 throw err;695 }, {name: 'Bob'});696 });697 // Fails because the thrown value is not the right one698 fails(t, () => {699 assertions.throws(() => {700 throw new Error('foo');701 }, {name: 'TypeError'});702 });703 // Passes because the correct error is thrown.704 passes(t, () => {705 assertions.throws(() => {706 const err = new TypeError(); // eslint-disable-line unicorn/error-message707 err.code = 'ERR_TEST';708 throw err;709 }, {code: 'ERR_TEST'});710 });711 // Passes because the correct error is thrown.712 passes(t, () => {713 assertions.throws(() => {714 const err = new TypeError(); // eslint-disable-line unicorn/error-message715 err.code = 42;716 throw err;717 }, {code: 42});718 });719 // Fails because the thrown value is not the right one720 fails(t, () => {721 assertions.throws(() => {722 const err = new TypeError(); // eslint-disable-line unicorn/error-message723 err.code = 'ERR_NOPE';724 throw err;725 }, {code: 'ERR_TEST'});726 });727}));728test('.throws() returns the thrown error', t => {729 const expected = new Error();730 const actual = assertions.throws(() => {731 throw expected;732 });733 t.is(actual, expected);734 t.end();735});736test('.throwsAsync()', gather(t => {737 // Fails because the promise is resolved, not rejected.738 eventuallyFailsWith(t, () => assertions.throwsAsync(Promise.resolve('foo')), {739 assertion: 'throwsAsync',740 message: '',741 values: [{label: 'Promise resolved with:', formatted: /'foo'/}]742 });743 // Fails because the promise is resolved with an Error744 eventuallyFailsWith(t, () => assertions.throwsAsync(Promise.resolve(new Error())), {745 assertion: 'throwsAsync',746 message: '',747 values: [{label: 'Promise resolved with:', formatted: /Error/}]748 });749 // Fails because the function returned a promise that resolved, not rejected.750 eventuallyFailsWith(t, () => assertions.throwsAsync(() => Promise.resolve('foo')), {751 assertion: 'throwsAsync',752 message: '',753 values: [{label: 'Returned promise resolved with:', formatted: /'foo'/}]754 });755 // Passes because the promise was rejected with an error.756 eventuallyPasses(t, () => assertions.throwsAsync(Promise.reject(new Error())));757 // Passes because the function returned a promise rejected with an error.758 eventuallyPasses(t, () => assertions.throwsAsync(() => Promise.reject(new Error())));759 // Passes because the error's message matches the regex760 eventuallyPasses(t, () => assertions.throwsAsync(Promise.reject(new Error('abc')), /abc/));761 // Fails because the error's message does not match the regex762 eventuallyFailsWith(t, () => assertions.throwsAsync(Promise.reject(new Error('abc')), /def/), {763 assertion: 'throwsAsync',764 message: '',765 values: [766 {label: 'Promise rejected with unexpected exception:', formatted: /Error/},767 {label: 'Expected message to match:', formatted: /\/def\//}768 ]769 });770 // Fails because the function throws synchronously771 eventuallyFailsWith(t, () => assertions.throwsAsync(() => {772 throw new Error('sync');773 }, null, 'message'), {774 assertion: 'throwsAsync',775 message: 'message',776 values: [777 {label: 'Function threw synchronously. Use `t.throws()` instead:', formatted: /Error/}778 ]779 });780 // Fails because the function did not return a promise781 eventuallyFailsWith(t, () => assertions.throwsAsync(() => {}, null, 'message'), {782 assertion: 'throwsAsync',783 message: 'message',784 values: [785 {label: 'Function returned:', formatted: /undefined/}786 ]787 });788}));789test('.throwsAsync() returns the rejection reason of promise', t => {790 const expected = new Error();791 return assertions.throwsAsync(Promise.reject(expected)).then(actual => {792 t.is(actual, expected);793 t.end();794 });795});796test('.throwsAsync() returns the rejection reason of a promise returned by the function', t => {797 const expected = new Error();798 return assertions.throwsAsync(() => {799 return Promise.reject(expected);800 }).then(actual => {801 t.is(actual, expected);802 t.end();803 });804});805test('.throws() fails if passed a bad value', t => {806 failsWith(t, () => {807 assertions.throws('not a function');808 }, {809 assertion: 'throws',810 message: '`t.throws()` must be called with a function',811 values: [{label: 'Called with:', formatted: /not a function/}]812 });813 t.end();814});815test('.throwsAsync() fails if passed a bad value', t => {816 failsWith(t, () => {817 assertions.throwsAsync('not a function');818 }, {819 assertion: 'throwsAsync',820 message: '`t.throwsAsync()` must be called with a function or promise',821 values: [{label: 'Called with:', formatted: /not a function/}]822 });823 t.end();824});825test('.throws() fails if passed a bad expectation', t => {826 failsWith(t, () => {827 assertions.throws(() => {}, true);828 }, {829 assertion: 'throws',830 message: 'The second argument to `t.throws()` must be a function, string, regular expression, expectation object or `null`',831 values: [{label: 'Called with:', formatted: /true/}]832 });833 failsWith(t, () => {834 assertions.throws(() => {}, {});835 }, {836 assertion: 'throws',837 message: 'The second argument to `t.throws()` must be a function, string, regular expression, expectation object or `null`',838 values: [{label: 'Called with:', formatted: /\{\}/}]839 });840 failsWith(t, () => {841 assertions.throws(() => {}, []);842 }, {843 assertion: 'throws',844 message: 'The second argument to `t.throws()` must be a function, string, regular expression, expectation object or `null`',845 values: [{label: 'Called with:', formatted: /\[\]/}]846 });847 failsWith(t, () => {848 assertions.throws(() => {}, {code: {}});849 }, {850 assertion: 'throws',851 message: 'The `code` property of the second argument to `t.throws()` must be a string or number',852 values: [{label: 'Called with:', formatted: /code: {}/}]853 });854 failsWith(t, () => {855 assertions.throws(() => {}, {instanceOf: null});856 }, {857 assertion: 'throws',858 message: 'The `instanceOf` property of the second argument to `t.throws()` must be a function',859 values: [{label: 'Called with:', formatted: /instanceOf: null/}]860 });861 failsWith(t, () => {862 assertions.throws(() => {}, {message: null});863 }, {864 assertion: 'throws',865 message: 'The `message` property of the second argument to `t.throws()` must be a string or regular expression',866 values: [{label: 'Called with:', formatted: /message: null/}]867 });868 failsWith(t, () => {869 assertions.throws(() => {}, {name: null});870 }, {871 assertion: 'throws',872 message: 'The `name` property of the second argument to `t.throws()` must be a string',873 values: [{label: 'Called with:', formatted: /name: null/}]874 });875 failsWith(t, () => {876 assertions.throws(() => {}, {is: {}, message: '', name: '', of() {}, foo: null});877 }, {878 assertion: 'throws',879 message: 'The second argument to `t.throws()` contains unexpected properties',880 values: [{label: 'Called with:', formatted: /foo: null/}]881 });882 t.end();883});884test('.throwsAsync() fails if passed a bad expectation', t => {885 failsWith(t, () => {886 assertions.throwsAsync(() => {}, true);887 }, {888 assertion: 'throwsAsync',889 message: 'The second argument to `t.throwsAsync()` must be a function, string, regular expression, expectation object or `null`',890 values: [{label: 'Called with:', formatted: /true/}]...
throws_test.js
Source:throws_test.js
1import jutest from 'jutest';2import assertions from "assertions/all";3jutest('assertions.throws()', s => {4 s.describe('basic behaviour', s => {5 s.test('passes if provided expression throws', t => {6 let result = assertions.throws(() => { throw 'foobar'; }, 'foobar');7 t.equal(result.passed, true);8 t.equal(result.actual, 'foobar');9 t.equal(result.expected, 'foobar');10 t.equal(result.operator, 'throws');11 });12 s.test('fails if provided expression doesnt throw', t => {13 let result = assertions.throws(() => { }, Error);14 t.equal(result.passed, false);15 t.equal(result.actual, undefined);16 t.equal(result.expected, Error);17 t.equal(result.operator, 'throws');18 t.match(result.failureMessage, /no errors/);19 });20 s.test("fails if error doesn't match the expectation", t => {21 let result = assertions.throws(() => { throw Error('baz'); }, 'bar');22 t.match(result.failureMessage, /baz/);23 });24 });...
Using AI Code Generation
1import test from 'ava';2test(t => {3 const err = t.throws(() => {4 throw new Error('foo');5 }, Error);6 t.is(err.message, 'foo');7});8test(t => {9 const err = t.throws(() => {10 throw new TypeError('bar');11 }, TypeError);12 t.is(err.message, 'bar');13});14test(t => {15 t.throws(() => {16 throw new TypeError('bar');17 }, 'bar');18});19test(t => {20 t.throws(() => {21 throw new TypeError('bar');22 }, TypeError, 'bar');23});24test(t => {25 const err = t.throws(() => {26 throw new TypeError('bar');27 }, TypeError);28 t.is(err.message, 'bar');29});30test(t => {31 const err = t.throws(() => {32 throw new TypeError('bar');33 }, TypeError);34 t.is(err.message, 'bar');35});36test(t => {37 t.throws(() => {38 throw new TypeError('bar');39 }, 'bar');40});41test(t => {42 t.throws(() => {43 throw new TypeError('bar');44 }, TypeError, 'bar');45});46test(t => {47 t.throws(() => {48 throw new TypeError('bar');49 }, /bar/);50});51test(t => {52 t.throws(() => {53 throw new TypeError('bar');54 }, TypeError, /bar/);55});56test(t => {57 t.throws(() => {58 throw new TypeError('bar');59 }, TypeError, 'bar');60});61test(t => {62 t.throws(() => {63 throw new TypeError('bar');64 }, TypeError, /bar/);65});66test(t => {67 t.throws(() => {68 throw new TypeError('bar');69 }, TypeError, 'bar');70});71test(t => {72 t.throws(() => {73 throw new TypeError('bar');74 }, TypeError, /bar/);75});76test(t => {77 t.throws(() => {78 throw new TypeError('bar');79 }, TypeError, 'bar');80});81test(t => {82 t.throws(() => {83 throw new TypeError('bar');84 }, TypeError, /bar/);85});
Using AI Code Generation
1const test = require('ava');2const { throws } = require('assert');3function fn() {4 throw new TypeError('Wrong value');5}6test('throws', t => {7 const error = t.throws(fn, TypeError);8 t.is(error.message, 'Wrong value');9});10test('throws with promise', async t => {11 await t.throwsAsync(fn, TypeError);12});13test('throws with async function', async t => {14 const error = await t.throwsAsync(async () => {15 await Promise.resolve(true);16 throw new TypeError('Wrong value');17 }, TypeError);18 t.is(error.message, 'Wrong value');19});20const test = require('ava');21const { notThrows } = require('assert');22function fn() {23 return true;24}25test('notThrows', t => {26 t.notThrows(fn);27});28test('notThrows with promise', async t => {29 await t.notThrowsAsync(Promise.resolve(true));30});31test('notThrows with async function', async t => {32 await t.notThrowsAsync(async () => {33 await Promise.resolve(true);34 });35});36const test = require('ava');37const { regex } = require('assert');38test('regex', t => {39 t.regex('I love AVA', /AVA/);40 t.notRegex('I love AVA', /ava/);41});42const test = require('ava');43const { ifError } = require('assert');44test('ifError', t => {45 t.ifError(false);46 t.ifError(0);47 t.ifError(null);48 t.ifError(undefined);49 t.ifError('');50 t.ifError(NaN);51 t.ifError(new Error());52});53const test = require('ava');54const { snapshot } = require('assert');55test('snapshot', t => {56 t.snapshot({ foo: 'bar' });57});58const test = require('ava');59const { log } = require('assert');60test('log', t => {61 t.log('Hello
Using AI Code Generation
1const test = require('ava');2const myMath = require('../src/myMath.js');3test('throws on negative numbers', t => {4 const error = t.throws(() => {5 myMath.add(-1, 2);6 }, Error);7 t.is(error.message, 'Negative numbers are not supported');8});9test('throws on negative numbers', t => {10 const error = t.throws(() => {11 myMath.add(-1, 2);12 }, Error);13 t.is(error.message, 'Negative numbers are not supported');14});15const test = require('ava');16const myMath = require('../src/myMath.js');17test('throws on negative numbers', async t => {18 const error = await t.throwsAsync(() => {19 myMath.addAsync(-1, 2);20 }, Error);21 t.is(error.message, 'Negative numbers are not supported');22});23test('throws on negative numbers', async t => {24 const error = await t.throwsAsync(() => {25 myMath.addAsync(-1, 2);26 }, Error);27 t.is(error.message, 'Negative numbers are not supported');28});29const test = require('ava');30const myMath = require('../src/myMath.js');31test('does not throw on positive numbers', t => {32 t.notThrows(() => {33 myMath.add(1, 2);34 });35});36test('does not throw on positive numbers', t => {37 t.notThrows(() => {38 myMath.add(1, 2);39 });40});41const test = require('ava');42const myMath = require('../src/myMath.js');43test('does not throw on positive numbers', async t => {44 await t.notThrowsAsync(() => {45 myMath.addAsync(1, 2);46 });47});48test('does not throw on positive numbers', async t => {49 await t.notThrowsAsync(() => {50 myMath.addAsync(1, 2);51 });52});53const test = require('ava');54const myMath = require('../src/myMath.js');55test('
Using AI Code Generation
1const test = require('ava');2const assert = require('assert');3const { throws } = require('assert/strict');4test('throws', t => {5 assert.throws(6 () => {7 throw new TypeError('Wrong value');8 },9 {10 }11 );12});
Using AI Code Generation
1const test = require('ava');2const fn = require('./');3test(t => {4 const err = t.throws(() => fn(100), TypeError);5 t.is(err.message, 'Expected a string, got number');6});
Using AI Code Generation
1import test from 'ava';2import {throws} from 'assert';3import {parse} from 'babylon';4import {generate} from 'babel-generator';5import {transformFromAst} from 'babel-core';6import {transform} from 'babel-core';7import {readFileSync} from 'fs';8import {join} from 'path';9import {Plugin} from 'broccoli-plugin';10import {build} from 'broccoli-test-helper';11import {Promise} from 'rsvp';12import broccoliBabelTranspiler from '../lib/index';13const fixture = (file) => readFileSync(join(__dirname, 'fixtures', file), 'utf8');14test('should compile ES6 to ES5', async (t) => {15 const node = new broccoliBabelTranspiler('test/fixtures/basic');16 const result = await build(node);17 t.is(result.read().toString(), fixture('basic.expected.js'));18});19test('should compile ES6 to ES5 with options', async (t) => {20 const node = new broccoliBabelTranspiler('test/fixtures/options', {21 getModuleId: function (name) {22 return name.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); });23 }24 });25 const result = await build(node);26 t.is(result.read().toString(), fixture('options.expected.js'));27});28test('should compile ES6 to ES5 with a custom parser', async (t) => {29 const node = new broccoliBabelTranspiler('test/fixtures/parser', {30 parserOpts: {31 }32 });33 const result = await build(node);34 t.is(result.read().toString(), fixture('parser.expected.js'));35});36test('should compile ES6 to ES5 with a custom generator', async (t) => {37 const node = new broccoliBabelTranspiler('test/fixtures/generator', {38 generatorOpts: {39 }40 });41 const result = await build(node);42 t.is(result.read().toString(), fixture('generator.expected.js'));43});44test('should compile ES6 to ES5 with a custom babel', async (t) => {45 const node = new broccoliBabelTranspiler('test/fixtures/babel', {46 babel: {
Using AI Code Generation
1const test = require('ava');2test('throws if no arguments', t => {3 t.throws(() => {4 });5});6const test = require('ava');7test('throws if no arguments', t => {8 t.throws(() => {9 throw new TypeError('Missing argument');10 });11});12const test = require('ava');13test('throws if no arguments', t => {14 t.throws(() => {15 throw new TypeError('Missing argument');16 }, TypeError);17});18const test = require('ava');19test('throws if no arguments', t => {20 t.throws(() => {21 throw new TypeError('Missing argument');22 }, TypeError, 'Missing argument');23});24const test = require('ava');25test('throws if no arguments', t => {26 t.throws(() => {27 throw new TypeError('Missing argument');28 }, {29 });30});31const test = require('ava');32test('throws if no arguments', t => {33 t.throws(() => {34 throw new TypeError('Missing argument');35 }, TypeError);36});37const test = require('ava');38test('throws if no arguments', t => {39 t.throws(() => {40 throw new TypeError('Missing argument');41 }, TypeError, 'Missing argument');42});43const test = require('ava');44test('throws if no arguments', t => {45 t.throws(() => {46 throw new TypeError('Missing argument');47 }, {48 });49});50const test = require('ava');51test('throws if no arguments', t => {52 t.throws(() => {53 throw new TypeError('Missing argument');54 }, TypeError);55});56const test = require('ava');57test('throws if no arguments', t => {58 t.throws(() => {59 throw new TypeError('Missing argument');60 }, TypeError, 'Missing argument');61});62const test = require('ava');63test('throws if no arguments', t => {64 t.throws(() => {65 throw new TypeError('Missing argument');66 }, {67 });68});
Using AI Code Generation
1const test = require('ava');2const { throws } = require('assert');3const { throwSync } = require('assert/strict');4const { strict } = require('assert');5const { strictEqual } = require('assert');6function sum(a,b){7 return a + b;8}9test('sum of 2 and 2 is 4', t => {10 t.is(sum(2,2), 4);11});12test('sum of 2 and 3 is 5', t => {13 t.is(sum(2,3), 5);14});15test('sum of 2 and 3 is not 4', t => {16 t.not(sum(2,3), 4);17});18test('sum of 2 and 3 is not 4', t => {19 t.not(sum(2,3), 4);20});21test('throws an error', t => {22 const error = t.throws(() => {23 throw new Error('error');24 });25 t.is(error.message, 'error');26});27test('throws an error', t => {28 const error = t.throws(() => {29 throw new Error('error');30 });31 t.is(error.message, 'error');32});33test('does not throw an error', t => {34 t.notThrows(() => {35 sum(2,3);36 });37});38test('deep equality', t => {39 t.deepEqual({a: 1}, {a: 1});40});41test('deep equality', t => {42 t.deepEqual({a: 1}, {a: 1});43});44test('not deep equality', t => {45 t.notDeepEqual({a: 1}, {a: 2});46});47test('not deep equality', t => {48 t.notDeepEqual({a: 1}, {a: 2});49});50test('truthy value', t => {51 t.truthy('hello');52});53test('truthy value', t => {54 t.truthy('hello');55});56test('falsy value', t => {57 t.falsy('');58});
Using AI Code Generation
1const test = require('ava')2const fn = require('./')3test('throws on non-string', t => {4 t.throws(() => {5 fn(1)6 })7})8module.exports = function (str) {9 if (typeof str !== 'string') {10 throw new Error('Expected a string')11 }12}13### `t.throwsAsync(fn, [error, [message]])`14const test = require('ava')15const fn = require('./')16test('rejects with a TypeError', async t => {17 await t.throwsAsync(fn(1), TypeError)18})19test('rejects with a TypeError containing message', async t => {20 const error = await t.throwsAsync(fn(1), TypeError)21 t.is(error.message, 'Expected a string')22})23test('rejects with a TypeError matching message', async t => {24 await t.throwsAsync(fn(1), TypeError, 'Expected a string')25})26test('rejects with a TypeError matching message using regex', async t => {27 await t.throwsAsync(fn(1), TypeError, /string/)28})29test('rejects with a TypeError matching message using function', async t => {30 await t.throwsAsync(fn(1), TypeError, error => error.message === 'Expected a string')31})
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!