Best JavaScript code snippet using ava
test.js
Source: test.js
...486 this.finishing = true;487 this.clearTimeout();488 this.verifyPlan();489 this.verifyAssertions();490 await this.runTeardowns();491 this.duration = nowAndTimers.now() - this.startedAt;492 let error = this.assertError;493 let passed = !error;494 if (this.metadata.failing) {495 passed = !passed;496 error = passed ? null : new Error('Test was expected to fail, but succeeded, you should stop marking the test as failing');497 }498 return {499 deferredSnapshotRecordings: this.deferredSnapshotRecordings,500 duration: this.duration,501 error,502 logs: this.logs,503 metadata: this.metadata,504 passed,...
ioexample.js
Source: ioexample.js
1import assert from 'assert';2import Promise from 'bluebird';3import SetupTeardown from './setup-teardown';4import Call from './call';5import Stub from './stub';6import {completeAll, deepEqual, VerifierFailure, InterfaceConsistencyFailure, ImproperAntiExample} from './utils';7// an ioexample represents an example invocation8// unlike an `Example`, it is specifically geared to describe the inputs and output of a hypothetical function9// an ioexample can be used to verify a pure function's behavior, e.g. in a unit test10// it can also be used in "auto stubbing" to generate a fake11// that fake function (stub), if it receives the ioexample's inputs will simply output the ioexample's output12class IOExample extends SetupTeardown {13 constructor (title, isAntiExample = false, target = null) {14 this.title = title;15 this.isAntiExample = isAntiExample;16 // internally, the `.target` will only ever be an interface instance17 // externally, the `.target` could be used to attach the example to whatever really (it's just metadata after all)18 this.target = target;19 this.outputsPromise = false;20 this.returns = undefined;21 this.setups = [];22 this.teardowns = [];23 }24 // `.boundTo` sets an ioexample's context, i.e. its hypothetical `this`25 boundTo (context) {26 this.context = context;27 return this;28 }29 // `.given` set an ioexample's inputs30 given (...inputs) {31 this.inputs = inputs;32 return this;33 }34 // `.return` sets an ioexample's *successful* output35 return (output) {36 this.returns = output;37 delete this.throws;38 return this;39 }40 // `.throw` sets an ioexample's *failing* output41 throw (output) {42 this.throws = output;43 delete this.returns;44 return this;45 }46 // `.eventually` sets an ioexample to represent an asynchronous (promise) result47 get eventually () {48 this.outputsPromise = true;49 return this;50 }51 // `.immediately` sets an ioexample to represent a synchronous result (which is the default)52 get immediately () {53 this.outputsPromise = false;54 return this;55 }56 // `.not` sets the ioexample to be an anti-example57 // an anti-example provides an example of what should *not* be58 get not () {59 this.isAntiExample = !this.isAntiExample;60 return this;61 }62 // `.does` actually does nothing, it's simply around for the sake of chaining with more-natural-language63 // e.g. `new IOExample('addition').given(2,2).does.not.return(5);`64 get does () {65 return this;66 }67 get succeeds () {68 return this.hasOwnProperty('returns');69 }70 get fails () {71 return this.hasOwnProperty('throws');72 }73 get output () {74 if (this.hasOwnProperty('returns')) return this.returns;75 if (this.hasOwnProperty('throws')) return this.throws;76 throw new Error('This ioexample has neither an expected return value nor an expected thrown error.');77 }78 // `.verifyWithoutSetupOrTeardown` does the work of verifying a candidate function against this ioexample79 verifyWithoutSetupOrTeardown (candidateFn) {80 if (typeof candidateFn !== 'function') return Promise.reject(new Error('Should have been a function'));81 const expected = this.output;82 const isAntiExample = this.isAntiExample;83 return new Call(candidateFn, this.context, this.inputs)84 .verifyOutputStyle(this.outputsPromise, this.throws)85 .then(function (call) {86 assert.deepEqual(call.ultimateOutput, expected); // TODO: format error87 return call.ultimateOutput;88 })89 .then(90 function (value) {91 if (isAntiExample) throw ImproperAntiExample(this, candidateFn);92 return value;93 },94 function (err) {95 if (isAntiExample) return;96 throw err;97 }98 )99 .catch(err => {100 throw VerifierFailure(this, candidateFn, err);101 });102 }103 // `.verify` confirms whether a candidate function fits this ioexample104 // verification passes if the candidate, invoked with the ioexample's inputs, outputs something deeply equal to the ioexample's output105 verify (candidateFn) {106 return Promise.bind(this)107 .then(this.runSetups)108 .then(() => this.verifyWithoutSetupOrTeardown(candidateFn))109 .then(this.runTeardowns);110 }111 consistentWith (interf, breadcrumbs = [interf.name]) {112 const signatures = values(interf.signatures);113 const candidateFn = this.fake();114 if (signatures.length === 0) return Promise.resolve(candidateFn);115 const isAntiExample = this.isAntiExample;116 return Promise.any(signatures, signature => signature.verify(candidateFn, ...this.inputs))117 .then(value => {118 if (isAntiExample) throw ImproperAntiExample(this, candidateFn);119 return value;120 }, err => {121 if (isAntiExample) return;122 throw err;123 })124 .catch(err => {125 throw InterfaceConsistencyFailure(this, breadcrumbs, err);126 });127 }128 fake () {129 return new Stub([this]);130 }131}...
constraint.js
Source: constraint.js
1import Promise from 'bluebird';2import SetupTeardown from './setup-teardown';3import {VerifierFailure} from './utils';4// a constraint is the most open-ended verifier (a thing with a `.verify` method)5// it simply accepts a validator function that should either error to deny verification or not error to affirm it6class Constraint extends SetupTeardown {7 constructor (title, validator, target = null) {8 super();9 this.title = title;10 this.validator = validator;11 // internally, the `.target` will only ever be an interface instance12 // externally, the `.target` could be used to attach the constraint to whatever really (it's just metadata after all)13 this.target = target;14 }15 verifyWithoutSetupOrTeardown (candidate) {16 // TODO: error if there's no validator to use17 return Promise.try(Function.prototype.bind.call(this.validator, candidate))18 .catch(err => {19 throw VerifierFailure(this, candidate, err);20 });21 }22 // `.verify` confirms whether a candidate fits this contraint23 // if the validator throws an error or results in a rejected promise, the constraint fails24 verify (candidate) {25 return Promise.bind(this)26 .then(this.runSetups)27 .then(() => this.verifyWithoutSetupOrTeardown(candidate))28 .then(this.runTeardowns);29 }30 verifiesThat (validator) {31 this.validator = validator;32 return this;33 }34}...
Using AI Code Generation
1import test from 'ava';2import { teardown } from 'ava-teardown';3test('test 1', t => {4 t.pass();5});6test('test 2', t => {7 t.pass();8});9teardown(() => {10 console.log('teardown');11 this.runTeardowns();12});13import test from 'ava';14import { teardown } from 'ava-teardown';15test('test 1', t => {16 t.pass();17});18test('test 2', t => {19 t.pass();20});21teardown(() => {22 console.log('teardown');23});
Using AI Code Generation
1test.beforeEach(t => {2 t.context.runTeardowns = runTeardowns;3});4test.afterEach.always(async t => {5 await t.context.runTeardowns();6});7const teardowns = [];8function addTeardown(fn) {9 teardowns.push(fn);10}11function runTeardowns() {12 return Promise.all(teardowns.map(fn => fn()));13}14module.exports = { addTeardown, runTeardowns };
Using AI Code Generation
1const test = require('ava');2test.beforeEach(t => {3 t.context.runTeardowns = runTeardowns;4});5test.afterEach(t => {6 return t.context.runTeardowns();7});8const test = require('ava');9test('ava test', t => {10 t.pass();11});12const test = require('ava');13test('ava test', t => {14 t.pass();15});16function runTeardowns() {17 return new Promise(resolve => {18 setTimeout(() => {19 resolve();20 }, 1000);21 });22}23const test = require('ava');24test('ava test', t => {25 t.pass();26});27function runTeardowns() {28 return new Promise(resolve => {29 setTimeout(() => {30 resolve();31 }, 1000);32 });33}34const test = require('ava');35test('ava test', t => {36 t.pass();37});38function runTeardowns() {39 return new Promise(resolve => {40 setTimeout(() => {41 resolve();42 }, 1000);43 });44}45const test = require('ava');46test('ava test', t => {47 t.pass();48});49function runTeardowns() {50 return new Promise(resolve => {51 setTimeout(() => {52 resolve();53 }, 1000);54 });55}56const test = require('ava');57test('ava test', t => {58 t.pass();59});60function runTeardowns() {61 return new Promise(resolve => {62 setTimeout(() => {63 resolve();64 }, 1000);65 });66}67const test = require('ava');68test('ava test', t => {69 t.pass();70});71function runTeardowns() {72 return new Promise(resolve => {73 setTimeout(() => {74 resolve();75 }, 1000);76 });77}78const test = require('ava');79test('ava test', t => {80 t.pass();81});82function runTeardowns() {83 return new Promise(resolve => {84 setTimeout(() => {85 resolve();86 }, 1000);87 });88}
Using AI Code Generation
1test.afterEach.always(t => {2 return new Promise(resolve => {3 this.runTeardowns(err => {4 if (err) {5 t.fail(err);6 }7 resolve();8 });9 });10});11test.beforeEach(t => {12 t.context = {};13 t.context.teardowns = [];14 t.context.addTeardown = teardown => {15 t.context.teardowns.push(teardown);16 };17 t.context.runTeardowns = cb => {18 const teardowns = t.context.teardowns.slice().reverse();19 const run = () => {20 if (teardowns.length === 0) {21 cb();22 return;23 }24 const teardown = teardowns.pop();25 teardown(run);26 };27 run();28 };29});30test.beforeEach(t => {31 t.context.addTeardown(cb => {32 cb();33 });34});35test('some test', t => {36 t.context.addTeardown(cb => {37 cb();38 });39});40test.beforeEach(t => {41 t.context.addTeardown(cb => {42 cb();43 });44});45test.beforeEach(t => {46 t.context.addTeardown(cb => {47 cb();48 });49});50test.afterEach.always(t => {51 return new Promise(resolve => {52 t.context.runTeardowns(err => {53 if (err) {54 t.fail(err);55 }56 resolve();57 });58 });59});60test.beforeEach(t => {61 t.context.addTeardown(cb => {62 cb();63 });64});65test.afterEach.always(t => {66 return new Promise(resolve => {67 t.context.runTeardowns(err => {68 if (err) {69 t.fail(err);70 }71 resolve();72 });73 });74});75test.beforeEach(t => {76 t.context.addTeardown(cb => {77 cb();78 });79});80test.afterEach.always(t => {81 return new Promise(resolve => {82 t.context.runTeardowns(err => {83 if (err) {84 t.fail(err);85 }86 resolve();87 });88 });89});
Using AI Code Generation
1test.beforeEach(t => {2 t.context.test = 'test';3});4test.afterEach(t => {5 t.context.test = 'test2';6});7test('test', t => {8 t.is(t.context.test, 'test');9});10test('test2', t => {11 t.is(t.context.test, 'test2');12});13test.beforeEach(t => {14 t.context.test = 'test3';15});16test.afterEach(t => {17 t.context.test = 'test4';18});19test('test3', t => {20 t.is(t.context.test, 'test3');21});22test('test4', t => {23 t.is(t.context.test, 'test4');24});25test.beforeEach(t => {26 t.context.test = 'test5';27});28test.afterEach(t => {29 t.context.test = 'test6';30});31test('test5', t => {32 t.is(t.context.test, 'test5');33});34test('test6', t => {35 t.is(t.context.test, 'test6');36});37test.beforeEach(t => {38 t.context.test = 'test7';39});40test.afterEach(t => {41 t.context.test = 'test8';42});43test('test7', t => {44 t.is(t.context.test, 'test7');45});46test('test8', t => {47 t.is(t.context.test, 'test8');48});
Using AI Code Generation
1const ava = require('ava');2const test = ava.default;3test.beforeEach(t => {4 t.context.data = { foo: 'bar' };5});6test('foo', t => {7 t.is(t.context.data.foo, 'bar');8});9test.afterEach.always(t => {10});11test('bar', t => {12 t.pass();13});14test.afterEach.always(t => {15});16test('baz', t => {17 t.pass();18});
Using AI Code Generation
1import test from 'ava';2import { runTeardowns } from 'ava/lib/runner';3test('test', t => {4 t.pass();5});6runTeardowns();7#### runTeardowns()8- [ava-teardowns](
Using AI Code Generation
1(async () => {2 const test = require('ava');3 test.beforeEach(t => {4 t.context.data = 'foo';5 });6 test.afterEach(async t => {7 await t.context.data;8 });9 test.after.always(async t => {10 await t.context.data;11 });12 test('test', t => {13 t.is(t.context.data, 'foo');14 });15 await test.runTeardowns();16})();17test.todo('will think about writing this later');18test.failing('will fix this later', t => {19 t.fail();20});21test.cb(t => {22 setTimeout(() => {23 t.pass();24 t.end();25 }, 100);26});27#### `.pass([message])`28#### `.fail([message])`29#### `.truthy(value, [message])`30#### `.falsy(value, [message])`31#### `.true(value, [message])`
Using AI Code Generation
1const test = require('ava');2const avaTeardown = require('ava-teardown');3const myTeardown = avaTeardown(test);4const myTest = myTeardown.teardown(() => {5});6myTest('test', t => {7});8myTeardown.runTeardowns();9const test = require('ava');10const avaTeardown = require('ava-teardown');11const myTeardown = avaTeardown(test);12const myTest = myTeardown.teardown(() => {13});14myTest('test', t => {15});16myTeardown.runTeardowns();17const test = require('ava');18const avaTeardown = require('ava-teardown');19const myTeardown = avaTeardown(test);20const myTest = myTeardown.teardown(() => {21});22myTest('test', t => {23});24myTeardown.runTeardowns();25const test = require('ava');26const avaTeardown = require('ava-teardown');27const myTeardown = avaTeardown(test);28const myTest = myTeardown.teardown(() => {29});30myTest('test', t => {31});32myTeardown.runTeardowns();33const test = require('ava');
Check out the latest blogs from LambdaTest on this topic:
Connectivity is so daunting. By far, we are all used to instant connectivity that puts world at our fingertips. We can purchase, post & pick anything, anywhere with the aid of desktops & devices.
If a decade ago, someone would have given you software and asked you to find out if it is working properly or not on all the operating systems and browsers available then you would have used the only one available method. Set up hundreds of computers with every possible combination of operating systems, browser, and browser versions, and then perform the testing of the software. But with the advancements in technology and software, this task has been simplified to leaps and bounds. One such technology that allows you to test software on a localized platform is Virtualization.
When it comes to a web application, before it goes into production, the developer must make sure that it works properly in all browsers. The end user should be able to experience a fully functional site that is able to handle all critical functionalities irrespective of the browser or device used by the end user. The behavior of an application is different in different operating systems, browsers and even devices based on their resolution. Most developers usually a prefers to work on a single browser, even if multiple browsers are installed in the workstation.
Developers have been trying to fully implement pure web based apps for mobile devices since the launch of iPhone in 2007, but its only from last 1-2 years that we have seen a headway in this direction. Progressive Web Applications are pure web-based that acts and feels like native apps. They can be added as icons to home and app tray, open in full screen (without browser), have pure native app kind of user experience, and generates notifications.
Automation is the need of the hour and is probably going to help you in the long run! Considering the huge number of competitors for every product, widespread adoption of Agile development is demanding automation everywhere in the IT world – in order to reach the pinnacle stage. With everyone planning on deploying automation into their organization, I thought of addressing the challenges faced while Website Automated Testing!
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!!