Best JavaScript code snippet using ava
expectations.js
Source:expectations.js
...194 stack.pop();195 return result;196 }197 if(!eq(this.value, val, [])){198 return this.assertions.fail(message);199 }200 this.assertions.pass(message);201 };202 Expect.prototype.toNotEqual = function(value){203 return this.not.toEqual(value);204 };205 Expect.prototype.toBe = function(val){206 var message = this.generateMessage(this.value, this.expr, 'to equal', val);207 if(this.value !== val){208 return this.assertions.fail(message);209 }210 this.assertions.pass(message);211 };212 Expect.prototype.toBeTruthy = function(val){213 var message = this.generateMessage(this.value, this.expr, 'to be truthy');214 if(!!this.value){215 return this.assertions.pass(message);216 }217 this.assertions.fail(message);218 };219 Expect.prototype.toBeFalsey = Expect.prototype.toBeFalsy = function(val){220 var message = this.generateMessage(this.value, this.expr, 'to be falsey');221 if(!this.value){222 return this.assertions.pass(message);223 }224 this.assertions.fail(message);225 };226 Expect.prototype.toBeGreaterThan = function(val){227 var message = this.generateMessage(this.value, this.expr, 'to be greater than', val);228 if(this.value > val){229 return this.assertions.pass(message);230 }231 this.assertions.fail(message);232 };233 Expect.prototype.toBeLessThan = function(val){234 var message = this.generateMessage(this.value, this.expr, 'to be less than', val);235 if(this.value < val){236 return this.assertions.pass(message);237 }238 this.assertions.fail(message);239 };240 Expect.prototype.toContain = function(val){241 var message = this.generateMessage(this.value, this.expr, 'to contain', val);242 if(this.value.indexOf(val) > -1){243 return this.assertions.pass(message);244 }245 this.assertions.fail(message);246 };247 Expect.prototype.toMatch = function(regex){248 var message = this.generateMessage(this.value, this.expr, 'to match', regex);249 if(regex.test(this.value)){250 return this.assertions.pass(message);251 }252 return this.assertions.fail(message);253 };254 Expect.prototype.toBeDefined = function(){255 var message = this.generateMessage(this.value, this.expr, 'to be defined');256 if(typeof this.value !== 'undefined'){257 return this.assertions.pass(message);258 }259 this.assertions.fail(message);260 };261 Expect.prototype.toBeUndefined = function(){262 var message = this.generateMessage(this.value, this.expr, 'to be undefined');263 if(typeof this.value === 'undefined'){264 return this.assertions.pass(message);265 }266 this.assertions.fail(message);267 };268 Expect.prototype.toBeNull = function(){269 if(this.value === null){270 return this.assertions.pass();271 }272 this.assertions.fail('to be null');273 };274 Expect.prototype.toThrow = function(){275 try{276 this.value();277 this.fail('to throw an exception');278 }catch(e){279 this.assertions.pass();280 }281 };282 Expect.prototype.pass = function(){283 this.assertions.pass();284 };285 Expect.prototype.fail = function(why, what){286 var message = this.generateMessage(this.value, this.expr, why || '', what);287 this.assertions.fail(message);288 };289 root.expect = function(value){290 return new Expect(value);291 };292 root.expect.addAssertion = function(name, matcher){293 Expect.prototype[name] = matcher;294 };...
apiv1.js
Source:apiv1.js
1/**2 * These assert functions can be used to ensure that the response objects are as expected.3 * @module test/router/api/assert4 * @author Marshall Asch <masch@uoguelph.ca>5 */6"use strict";7const chai = require("chai");8const validator = require("validator");9const asPromised = require("chai-as-promised");10const assert = chai.assert;11chai.use(asPromised);12function isDateString(s, propName) {13 if (propName == null) {14 propName = "date string";15 }16 assert.isString(s, `${propName} is not a string`);17 assert.equal(new Date(s).toISOString(), s, `${propName} is not a valid date`);18}19/**20 * isSaleObject() asserts that the supplied object conforms to the definition of21 * a the sale for a product object.22 *23 * @param {Object} obj - the object to check24 * @throws AssertionError if the assertions fail25 */26function isSaleObject(obj) {27 let hasAtt = false;28 for (const key in Object.keys(obj)) {29 switch (key) {30 case "percent":31 assert.isFalse(hasAtt, "Can not contain both 'percent' and 'discount' attributes");32 assert.isString(obj.id, "id is a not a string");33 assert.isTrue(validator.isMongoId(obj.id), "id is not a valid mongoId");34 hasAtt = true;35 break;36 case "discount":37 assert.isFalse(hasAtt, "Can not contain both 'percent' and 'discount' attributes");38 assert.isNumeric(obj.discount, "discount is not a number");39 assert.isAtLeast(obj.discount, 0, "discount can not be negative");40 assert.isTrue(validator.isDecimal(obj.discount.toString(), {force_decimal: true, decimal_digits: "2", }), "discount is not a valid value");41 hasAtt = true;42 break;43 default:44 assert.isUndefined(obj[key]);45 break;46 }47 }48 assert.isTrue(hasAtt, "must contain either 'percent' or 'discount' attributes");49}50/**51 * isGenericResponse() asserts that the body of the response conforms to the52 * definition of the Generic Response Object.53 *54 * @param {number} code - the response code to check55 * @param {Object} obj - the object to check56 * @param {boolean} [any] - if true, then any code will be accepted57 * @throws Error if the assertions fail58 */59function isGenericResponse(code, obj, any) {60 if (any !== true) {61 assert.isOk(Number.isInteger(code));62 assert.equal(obj.status, code);63 }64 assert.isString(obj.message);65}66/**67 * isExecObject() asserts that the body of the response conforms to the68 * definition of the Exec Object.69 *70 * @param {Object} obj - the object to check71 * @throws AssertionError if the assertions fail72 */73function isExecObject(obj) {74 for (const key in Object.keys(obj)) {75 switch (key) {76 case "id":77 assert.isString(obj.id, "id is a not a string");78 assert.isTrue(validator.isMongoId(obj.id), "id is not a valid mongoId");79 break;80 case "name":81 assert.isString(obj.name, "name is not a string");82 break;83 case "email":84 assert.isString(obj.email, "email is not a string");85 assert.isTrue(validator.isEmail(obj.email), "email not a valid email");86 break;87 case "year":88 assert.isOk(Number.isInteger(obj.year));89 break;90 case "role":91 assert.isString(obj.role, "role is not a string");92 break;93 case "order":94 assert.isOk(Number.isInteger(obj.order));95 break;96 default:97 assert.isUndefined(obj[key]);98 break;99 }100 }101}102/**103 * isChallengeObject() asserts that supplied object conforms to the definition of104 * the Roboticon Challenge Object.105 *106 * @param {Object} obj - the object to check107 * @throws AssertionError if the assertions fail108 */109function isChallengeObject(obj) {110 for (const key in Object.keys(obj)) {111 switch (key) {112 case "id":113 assert.isString(obj.id, "id is a not a string");114 assert.isTrue(validator.isMongoId(obj.id), "id is not a valid mongoId");115 break;116 case "year":117 assert.isOk(Number.isInteger(obj.year));118 break;119 case "challenge_number":120 assert.isOk(Number.isInteger(obj.challenge_number));121 break;122 case "description":123 assert.isString(obj.description, "description is not a string");124 break;125 case "goal":126 assert.isString(obj.goal, "goal is not a string");127 break;128 case "parameters":129 assert.isString(obj.parameters, "parameters is not a string");130 break;131 case "points":132 assert.isString(obj.points, "points is not a string");133 break;134 case "hidden":135 assert.isBooleanObject(obj.hidden);136 break;137 case "image":138 assert.isString(obj.image, "image url is not a string");139 break;140 case "map":141 assert.isString(obj.map, "map url is not a string");142 break;143 default:144 assert.isUndefined(obj[key]);145 break;146 }147 }148}149/**150 * isEvent() asserts that the supplied object conforms to the definition of an Event object.151 *152 * @param {Object} obj - the object to check153 * @throws AssertionError if the assertions fail154 */155function isEvent(obj) {156 for (const key in Object.keys(obj)) {157 switch (key) {158 case "id":159 assert.isString(obj.id, "id is a not a string");160 assert.isTrue(validator.isMongoId(obj.id), "id is not a valid mongoId");161 break;162 case "start_time":163 isDateString(obj.start_time, "start_time");164 break;165 case "end_time":166 isDateString(obj.end_time, "end_time");167 break;168 case "title":169 assert.isString(obj.title, "title is not a string");170 break;171 case "description":172 assert.isString(obj.description, "description is not a string");173 break;174 case "location":175 assert.isString(obj.location, "location is not a string");176 break;177 case "tags":178 assert.isArray(obj.tags, "tags is not an array");179 obj.tags.forEach((tag) => {180 assert.isString(tag, "tag is not a string");181 });182 break;183 default:184 assert.isUndefined(obj[key]);185 break;186 }187 }188}189/**190 * isProduct() asserts that the supplied object conforms to the definition of191 * a Product object.192 *193 * @param {Object} obj - the object to check194 * @throws AssertionError if the assertions fail195 */196function isProduct(obj) {197 for (const key in Object.keys(obj)) {198 switch (key) {199 case "id":200 assert.isString(obj.id, "id is a not a string");201 assert.isTrue(validator.isMongoId(obj.id), "id is not a valid mongoId");202 break;203 case "name":204 assert.isString(obj.name, "name is not a string");205 break;206 case "type":207 assert.isString(obj.type, "type is not a string");208 break;209 case "price":210 assert.isNumeric(obj.price, "price is not a number");211 assert.isAtLeast(obj.price, 0, "price can not be negative");212 assert.isTrue(validator.isDecimal(obj.price.toString(), {force_decimal: true, decimal_digits: "2", }), "Price is not a valid value");213 break;214 case "description":215 assert.isString(obj.description, "description is not a string");216 break;217 case "tags":218 assert.isArray(obj.tags, "tags is not an array");219 obj.tags.forEach((tag) => {220 assert.isString(tag, "tag is not a string");221 });222 break;223 case "images":224 assert.isArray(obj.images, "images is not an array");225 obj.images.forEach((img) => {226 assert.isString(img, "img is not a string");227 });228 break;229 case "available":230 assert.isBooleanObject(obj.available);231 break;232 case "sale":233 if (obj.sale !== null) {234 isSaleObject(obj.sale);235 }236 break;237 default:238 assert.isUndefined(obj[key]);239 break;240 }241 }242}243/**244 * isPagingObject() asserts that the supplied response conforms to the definition of a Post object.245 *246 * @param {Object} obj - the response body247 * @throws AssertionError if the assertions fail248 */249function isPagingObject(obj) {250 Object.keys(obj).forEach((key) => {251 switch (key) {252 case "href":253 assert.isString(obj.href, "href is not a string");254 break;255 case "next":256 if (obj.next !== null) {257 assert.isString(obj.next, "next is not a string or null");258 }259 break;260 case "previous":261 if (obj.previous !== null) {262 assert.isString(obj.previous, "previous is not a string or null");263 }264 break;265 case "items": 266 assert.isArray(obj.items, "items is not an array");267 break;268 case "limit":269 assert.isNumber(obj.limit, "limit is not a number");270 break;271 case "offset":272 assert.isNumber(obj.offset, "offset is not a number");273 break;274 case "total":275 assert.isNumber(obj.total, "total is not a number");276 break;277 default:278 assert.isUndefined(obj[key]);279 break;280 }281 });282}283module.exports = {284 isGenericResponse: isGenericResponse,285 isProduct: isProduct,286 isEvent: isEvent,287 isChallengeObject: isChallengeObject,288 isExecObject: isExecObject,289 isPagingObject: isPagingObject,...
Using AI Code Generation
1const test = require('ava')2test('foo', t => {3 t.fail()4})5test('bar', async t => {6 const bar = Promise.resolve('bar')7 t.is(await bar, 'bar')8})
Using AI Code Generation
1const test = require('ava');2test('foo', t => {3 t.fail();4});5test('bar', async t => {6 const bar = Promise.resolve('bar');7 t.is(await bar, 'bar');8});9- [Assertions](
Using AI Code Generation
1const test = require('ava');2test('my passing test', t => {3 t.pass();4});5test('my failing test', t => {6 t.fail();7});
Using AI Code Generation
1import test from 'ava';2test('my passing test', t => {3 t.pass();4});5test('my failing test', t => {6 t.fail();7});8import test from 'ava';9test('my passing test', t => {10 t.pass();11});12test('my failing test', t => {13 t.fail();14});15import test from 'ava';16test('my passing test', t => {17 t.truthy('unicorn');18});19test('my failing test', t => {20 t.truthy('');21});22import test from 'ava';23test('my passing test', t => {24 t.falsy('');25});26test('my failing test', t => {27 t.falsy('unicorn');28});29import test from 'ava';30test('my passing test', t => {31 t.true(true);32});33test('my failing test', t => {34 t.true(false);35});36import test from 'ava';37test('my passing test', t => {38 t.false(false);39});40test('my failing test', t => {41 t.false(true);42});43import test from 'ava';44test('my passing test', t => {45 t.is('foo', 'foo');46});47test('my failing test', t => {48 t.is('foo', 'bar');49});50import test from 'ava';51test('my passing test', t => {52 t.not('foo', 'bar');53});54test('my failing test', t => {55 t.not('foo', 'foo');56});57import test from 'ava';58test('my passing test', t => {59 t.deepEqual({a: 1}, {a: 1});60});61test('my failing test', t => {62 t.deepEqual({a: 1}, {a: 2});63});64import test from 'ava';65test('my passing test', t => {66 t.notDeepEqual({a:
Using AI Code Generation
1const test = require('ava');2test('this will fail', t => {3 t.fail();4});5test('this will pass', t => {6 t.pass();7});8test('this will pass', t => {9 t.pass();10});11test('this will pass', t => {12 t.is(1, 1);13});14test('this will pass', t => {15 t.not(1, 2);16});17test('this will pass', t => {18 t.true(true);19});20test('this will pass', t => {21 t.false(false);22});23test('this will pass', t => {24 t.is(1, 1);25});26test('this will pass', t => {27 t.not(1, 2);28});29test('this will pass', t => {30 t.true(true);31});32test('this will pass', t => {33 t.false(false);34});35test('this will pass', t => {36 t.deepEqual({a: 1}, {a: 1});37});38test('this will pass', t => {39 t.notDeepEqual({a: 1}, {a: 2});40});41test('this will pass', t => {42 t.regex('abc', /a/);43});44test('this will pass', t => {45 t.notRegex('abc', /d/);46});47test('this will pass', t => {48 t.throws(() => {49 throw new TypeError('foo');50 });51});
Using AI Code Generation
1const test = require('ava')2test('my passing test', t => {3 t.pass()4})5test('my failing test', t => {6 t.fail()7})8test('my failing test with error message', t => {9 t.fail('This will fail')10})11const test = require('ava')12test('my passing test', t => {13 t.truthy('hello')14})15test('my failing test', t => {16 t.truthy(false)17})18const test = require('ava')19test('my passing test', t => {20 t.falsy(false)21})22test('my failing test', t => {23 t.falsy('hello')24})25const test = require('ava')26test('my passing test', t => {27 t.true(true)28})29test('my failing test', t => {30 t.true(false)31})32const test = require('ava')33test('my passing test', t => {34 t.false(false)35})36test('my failing test', t => {37 t.false(true)38})39const test = require('ava')40test('my passing test', t => {41 t.is(1, 1)42})43test('my failing test', t => {44 t.is(1, 2)45})46const test = require('ava')47test('my passing test', t => {48 t.not(1, 2)49})50test('my failing test', t => {51 t.not(1, 1)52})53const test = require('ava')54test('my passing test', t => {55 const obj = { foo: 'bar' }56 t.deepEqual(obj, { foo: 'bar' })57})58test('my failing test', t => {59 const obj = { foo: 'bar' }60 t.deepEqual(obj, { foo:
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!!