Best JavaScript code snippet using ava
assert.js
Source:assert.js
...52 }53 }54}55exports.AssertionError = AssertionError;56function checkAssertionMessage(assertion, message) {57 if (typeof message === 'undefined' || typeof message === 'string') {58 return true;59 }60 return new AssertionError({61 assertion,62 improperUsage: true,63 message: 'The assertion message must be a string',64 values: [formatWithLabel('Called with:', message)]65 });66}67exports.checkAssertionMessage = checkAssertionMessage;68function getErrorWithLongStackTrace() {69 const limitBefore = Error.stackTraceLimit;70 Error.stackTraceLimit = Number.POSITIVE_INFINITY;71 const error = new Error(); // eslint-disable-line unicorn/error-message72 Error.stackTraceLimit = limitBefore;73 return error;74}75function validateExpectations(assertion, expectations, numberArgs) { // eslint-disable-line complexity76 if (numberArgs === 1 || expectations === null || expectations === undefined) {77 if (expectations === null) {78 throw new AssertionError({79 assertion,80 message: `The second argument to \`t.${assertion}()\` must be an expectation object or \`undefined\``,81 values: [formatWithLabel('Called with:', expectations)]82 });83 }84 expectations = {};85 } else if (86 typeof expectations === 'function' ||87 typeof expectations === 'string' ||88 expectations instanceof RegExp ||89 typeof expectations !== 'object' ||90 Array.isArray(expectations) ||91 Object.keys(expectations).length === 092 ) {93 throw new AssertionError({94 assertion,95 message: `The second argument to \`t.${assertion}()\` must be an expectation object, \`null\` or \`undefined\``,96 values: [formatWithLabel('Called with:', expectations)]97 });98 } else {99 if (hasOwnProperty(expectations, 'instanceOf') && typeof expectations.instanceOf !== 'function') {100 throw new AssertionError({101 assertion,102 message: `The \`instanceOf\` property of the second argument to \`t.${assertion}()\` must be a function`,103 values: [formatWithLabel('Called with:', expectations)]104 });105 }106 if (hasOwnProperty(expectations, 'message') && typeof expectations.message !== 'string' && !(expectations.message instanceof RegExp)) {107 throw new AssertionError({108 assertion,109 message: `The \`message\` property of the second argument to \`t.${assertion}()\` must be a string or regular expression`,110 values: [formatWithLabel('Called with:', expectations)]111 });112 }113 if (hasOwnProperty(expectations, 'name') && typeof expectations.name !== 'string') {114 throw new AssertionError({115 assertion,116 message: `The \`name\` property of the second argument to \`t.${assertion}()\` must be a string`,117 values: [formatWithLabel('Called with:', expectations)]118 });119 }120 if (hasOwnProperty(expectations, 'code') && typeof expectations.code !== 'string' && typeof expectations.code !== 'number') {121 throw new AssertionError({122 assertion,123 message: `The \`code\` property of the second argument to \`t.${assertion}()\` must be a string or number`,124 values: [formatWithLabel('Called with:', expectations)]125 });126 }127 for (const key of Object.keys(expectations)) {128 switch (key) {129 case 'instanceOf':130 case 'is':131 case 'message':132 case 'name':133 case 'code':134 continue;135 default:136 throw new AssertionError({137 assertion,138 message: `The second argument to \`t.${assertion}()\` contains unexpected properties`,139 values: [formatWithLabel('Called with:', expectations)]140 });141 }142 }143 }144 return expectations;145}146// Note: this function *must* throw exceptions, since it can be used147// as part of a pending assertion for promises.148function assertExpectations({assertion, actual, expectations, message, prefix, savedError}) {149 if (!isError(actual)) {150 throw new AssertionError({151 assertion,152 message,153 savedError,154 values: [formatWithLabel(`${prefix} exception that is not an error:`, actual)]155 });156 }157 const actualStack = actual.stack;158 if (hasOwnProperty(expectations, 'is') && actual !== expectations.is) {159 throw new AssertionError({160 assertion,161 message,162 savedError,163 actualStack,164 values: [165 formatWithLabel(`${prefix} unexpected exception:`, actual),166 formatWithLabel('Expected to be strictly equal to:', expectations.is)167 ]168 });169 }170 if (expectations.instanceOf && !(actual instanceof expectations.instanceOf)) {171 throw new AssertionError({172 assertion,173 message,174 savedError,175 actualStack,176 values: [177 formatWithLabel(`${prefix} unexpected exception:`, actual),178 formatWithLabel('Expected instance of:', expectations.instanceOf)179 ]180 });181 }182 if (typeof expectations.name === 'string' && actual.name !== expectations.name) {183 throw new AssertionError({184 assertion,185 message,186 savedError,187 actualStack,188 values: [189 formatWithLabel(`${prefix} unexpected exception:`, actual),190 formatWithLabel('Expected name to equal:', expectations.name)191 ]192 });193 }194 if (typeof expectations.message === 'string' && actual.message !== expectations.message) {195 throw new AssertionError({196 assertion,197 message,198 savedError,199 actualStack,200 values: [201 formatWithLabel(`${prefix} unexpected exception:`, actual),202 formatWithLabel('Expected message to equal:', expectations.message)203 ]204 });205 }206 if (expectations.message instanceof RegExp && !expectations.message.test(actual.message)) {207 throw new AssertionError({208 assertion,209 message,210 savedError,211 actualStack,212 values: [213 formatWithLabel(`${prefix} unexpected exception:`, actual),214 formatWithLabel('Expected message to match:', expectations.message)215 ]216 });217 }218 if (typeof expectations.code !== 'undefined' && actual.code !== expectations.code) {219 throw new AssertionError({220 assertion,221 message,222 savedError,223 actualStack,224 values: [225 formatWithLabel(`${prefix} unexpected exception:`, actual),226 formatWithLabel('Expected code to equal:', expectations.code)227 ]228 });229 }230}231class Assertions {232 constructor({233 pass = notImplemented,234 pending = notImplemented,235 fail = notImplemented,236 skip = notImplemented,237 compareWithSnapshot = notImplemented,238 powerAssert,239 experiments = {},240 disableSnapshots = false241 } = {}) {242 const withSkip = assertionFn => {243 assertionFn.skip = skip;244 return assertionFn;245 };246 // When adding new enhanced functions with new patterns, don't forget to247 // enable the pattern in the power-assert compilation step in @ava/babel.248 const withPowerAssert = (pattern, assertionFn) => powerAssert.empower(assertionFn, {249 onError: event => {250 if (event.powerAssertContext) {251 event.error.statements = powerAssert.format(event.powerAssertContext, formatPowerAssertValue);252 }253 fail(event.error);254 },255 onSuccess: () => {256 pass();257 },258 bindReceiver: false,259 patterns: [pattern]260 });261 const checkMessage = (assertion, message, powerAssert = false) => {262 const result = checkAssertionMessage(assertion, message);263 if (result === true) {264 return this.true;265 }266 if (powerAssert) {267 throw result;268 }269 fail(result);270 return false;271 };272 this.pass = withSkip(() => {273 pass();274 });275 this.fail = withSkip(message => {276 if (!checkMessage('fail', message)) {...
test.js
Source:test.js
...327 // this function to throw with a useful stack.328 this.planError = planError;329 }330 timeout(ms, message) {331 const result = checkAssertionMessage('timeout', message);332 if (result !== true) {333 this.saveFirstError(result);334 // Allow the timeout to be set even when the message is invalid.335 message = '';336 }337 if (this.finishing) {338 return;339 }340 this.clearTimeout();341 this.timeoutMs = ms;342 this.timeoutTimer = nowAndTimers.setTimeout(() => {343 this.saveFirstError(new Error(message || 'Test timeout exceeded'));344 if (this.finishDueToTimeout) {345 this.finishDueToTimeout();...
Using AI Code Generation
1const test = require('ava');2test('foo', t => {3 t.is(1, 1);4});5test('bar', async t => {6 const bar = Promise.resolve('bar');7 t.is(await bar, 'bar');8});9const test = require('ava');10test('foo', t => {11 t.is(1, 1);12});13test('bar', async t => {14 const bar = Promise.resolve('bar');15 t.is(await bar, 'bar');16});17 t.is(await bar, 'bar')18 Expected value to be (===) 'bar'19 t.is(await bar, 'bar')20 Expected value to be (===) 'bar'
Using AI Code Generation
1import test from 'ava';2test('foo', t => {3 t.is(1, 1);4 t.is(2, 2);5});6test('bar', async t => {7 const bar = Promise.resolve('bar');8 t.is(await bar, 'bar');9});10import test from 'ava';11test('foo', t => {12 t.is(1, 1);13 t.is(2, 2);14});15test('bar', async t => {16 const bar = Promise.resolve('bar');17 t.is(await bar, 'bar');18});19Default: `import test from 'ava';`
Using AI Code Generation
1import test from 'ava';2test('my passing test', t => {3 t.pass();4});5import test from 'ava';6test('my passing test', t => {7 t.pass();8});9test('my failing test', t => {10 t.fail();11});12import test from 'ava';13test('my passing test', t => {14 t.pass();15});16test('my failing test', t => {17 t.fail();18});19test('my passing test', t => {20 t.pass();21});22test('my failing test', t => {23 t.fail();24});25import test from 'ava';26test('my passing test', t => {27 t.pass();28});29test('my failing test', t => {30 t.fail();31});32test('my passing test', t => {33 t.pass();34});35test('my failing test', t => {36 t.fail();37});38import test from 'ava';39test('my passing test', t => {40 t.pass();41});42test('my failing test', t => {43 t.fail();44});45test('my passing test', t => {46 t.pass();47});48test('my failing test', t => {49 t.fail();50});51test('my passing test', t => {52 t.pass();53});54test('my failing test', t => {55 t.fail();56});57import test from 'ava';58test('my passing test', t => {59 t.pass();60});61test('my failing test', t => {62 t.fail();63});64test('my passing test', t => {65 t.pass();66});67test('my failing test', t => {68 t.fail();69});70test('my passing test', t => {71 t.pass();72});73test('my failing test', t => {74 t.fail();75});76import test from 'ava';77test('my passing test', t => {78 t.pass();79});80test('my failing test', t => {81 t.fail();82});83test('my passing test', t => {84 t.pass();85});86test('my failing test', t => {87 t.fail();88});89test('my passing test', t => {90 t.pass();91});92test('my failing test', t => {93 t.fail();94});95import test from 'ava';96test('my passing test', t => {97 t.pass();98});99test('
Using AI Code Generation
1var assert = require('assert');2assert.doesNotThrow(3 function() {4 throw new Error('Wrong value');5 },6);7assert.doesNotThrow(8 function() {9 throw new Error('Wrong value');10 },11);12assert.doesNotThrow(13 function() {14 throw new Error('Wrong value');15 },16);17assert.doesNotThrow(18 function() {19 throw new Error('Wrong value');20 },21);22assert.doesNotThrow(23 function() {24 throw new Error('Wrong value');25 },26);27assert.doesNotThrow(28 function() {29 throw new Error('Wrong value');30 },31);32assert.doesNotThrow(33 function() {34 throw new Error('Wrong value');35 },36);37assert.doesNotThrow(38 function() {39 throw new Error('Wrong value');40 },41);42assert.doesNotThrow(43 function() {44 throw new Error('Wrong value');45 },46);
Using AI Code Generation
1test('foo', t => {2 t.throws(() => {3 throw new TypeError('foo bar');4 }, {message: 'foo bar'});5});6test('foo', () => {7 expect(() => {8 throw new TypeError('foo bar');9 }).toThrowErrorMatchingSnapshot();10});11it('foo', () => {12 assert.throws(() => {13 throw new TypeError('foo bar');14 }, {message: 'foo bar'});15});16test('foo', t => {17 t.throws(() => {18 throw new TypeError('foo bar');19 }, {message: 'foo bar'});20});
Using AI Code Generation
1import test from 'ava';2test('example', t => {3 t.is(1, 1);4 t.is(2, 2);5 t.is(3, 3);6});7import test from 'ava';8test('example', t => {9 t.is(1, 1);10 t.is(2, 2);11 t.is(3, 3, '3 is not equal to 3');12});13 Test.fn (test.js:5:5)
Using AI Code Generation
1import test from 'ava';2import { checkAssertionMessage } from 'ava/lib/assert.js';3test('test', t => {4 t.throws(() => {5 checkAssertionMessage('foo', 'bar');6 }, {7 });8});9### `checkAssertionMessage(actual, expected, message)`10- [ava](
Using AI Code Generation
1const test = require('ava');2const {checkAssertionMessage} = require('ava-assertion-message');3test('test1', t => {4 checkAssertionMessage(t, 'test1', () => {5 t.is(1, 2);6 });7});8 6: checkAssertionMessage(t, 'test1', () => {9 7: t.is(1, 2);10 8: });11 Expected value to be (using ===):12const test = require('ava');13const {checkAssertionMessage} = require('ava-assertion-message');14test('test1', t => {15 checkAssertionMessage(t, 'test1', () => {16 t.is(1, 2);17 });18});19 6: checkAssertionMessage(t, 'test1', () => {20 7: t.is(1, 2);21 8: });22 Expected value to be (using ===):23MIT © [Rajat S](
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!!