How to use runTeardowns method in ava

Best JavaScript code snippet using ava

test-case.js

Source:test-case.js Github

copy

Full Screen

1define((require, exports, module) => {2 'use strict';3 const koru = require('koru');4 const LinkedList = require('koru/linked-list');5 const Core = require('koru/test/core');6 const stubber = require('koru/test/stubber');7 const util = require('koru/util');8 const timeout$ = Symbol(), isDone$ = Symbol(),9 after$ = Symbol(), once$ = Symbol(), temp$ = Symbol(), before$ = Symbol();10 const MAX_TIME = 2000;11 let isOnce = false;12 let currentTC, tests;13 let lastTest, currTest, nextTest, common, nextFunc;14 let nt = 0, assertCount = 0;15 let asyncTimeout = 0;16 const {clearTimeout, setTimeout} = globalThis; // isolate from stubbing17 const assertIsPromise = (p, f) => {(p == null || typeof p.then !== 'function') && notPromise(f)};18 const notPromise = (f) => {19 assert.fail(`Expected return of undefined or a Promise ${Core.test.mode}:20${Core.test.name}` + (f ? ` Return is in code:\n ${f.toString()}` : ''));21 };22 const checkAssertionCount = (test, assertCount) => {23 if (assertCount !== Core.assertCount) {24 test.success = true;25 } else {26 test.success = false;27 const {name, line} = test.location;28 test.errors = [29 'Failure: No assertions\n at - ' +30 `test.body (${name}.js:${line}:1)`,31 ];32 }33 };34 const runListAndAsyncCallbacks = async (value, i, list, node) => {35 const {test} = Core;36 for (;i >= 0; --i) {37 const func = value[i];38 if (typeof func === 'function') {39 await func.call(test);40 } else {41 func.stop();42 }43 }44 await runAsyncCallbacks(list, node);45 };46 const runAsyncCallbacks = async (list, node) => {47 const {test} = Core;48 let prev = node;49 for (node = node.next; node !== void 0; node = node.next) {50 if (node[temp$] === true) {51 list.removeNode(node, prev);52 } else {53 prev = node;54 }55 const {value} = node;56 if (typeof value === 'function') {57 await value.call(test);58 } else if (Array.isArray(value)) {59 for (let i = value.length - 1; i >= 0; --i) {60 const func = value[i];61 if (typeof func === 'function') {62 await func.call(test);63 } else {64 func.stop();65 }66 }67 } else {68 value.stop();69 }70 }71 };72 const runCallbacks = (list) => {73 if (list === void 0) return;74 const {test} = Core;75 let prev;76 for (let node = list.front; node !== void 0; node = node.next) {77 if (node[temp$] === true) {78 list.removeNode(node, prev);79 } else {80 prev = node;81 }82 const {value} = node;83 if (typeof value.stop === 'function') {84 value.stop();85 } else if (typeof value === 'function') {86 const promise = value.call(test);87 if (promise !== void 0) {88 assertIsPromise(promise, value);89 return node.next === void 090 ? promise91 : promise.then(() => runAsyncCallbacks(list, node));92 }93 } else if (Array.isArray(value)) {94 for (let i = value.length - 1; i >= 0; --i) {95 const func = value[i];96 if (typeof func.stop === 'function') {97 func.stop();98 } else {99 const promise = func.call(test);100 if (promise !== void 0) {101 assertIsPromise(promise, func);102 return i > 0103 ? promise.then(() => {runListAndAsyncCallbacks(value, i - 1, list, node)})104 : promise;105 }106 }107 }108 }109 }110 };111 const runOnceCallbacks = (list) => {112 try {113 isOnce = true;114 return runCallbacks(list);115 } finally {116 isOnce = false;117 }118 };119 const runTearDowns = (tc, common) => {120 currentTC = tc;121 if (tc === void 0) return;122 const sameTC = tc === common;123 const once = tc[once$];124 const promise = runCallbacks(tc[after$]);125 if (promise !== void 0) {126 return promise.then(async () => {127 if (sameTC) {128 if (once !== void 0) return;129 }130 if (once !== void 0) {131 await runOnceCallbacks(once.after);132 }133 const pTc = tc.tc;134 if (pTc === void 0) {135 common === void 0 && reset(tc);136 } else {137 await runTearDowns(pTc, sameTC ? pTc : common);138 }139 });140 }141 if (sameTC) {142 if (once !== void 0) return;143 } else if (once !== void 0) {144 const promise = runOnceCallbacks(once.after);145 if (promise !== void 0) {146 return promise.then(() => {147 const pTc = tc.tc;148 if (pTc === void 0) {149 common === void 0 && reset(tc);150 } else {151 return runTearDowns(pTc, sameTC ? pTc : common);152 }153 });154 }155 }156 const pTc = tc.tc;157 if (pTc === void 0) {158 common === void 0 && reset(tc);159 } else {160 return runTearDowns(pTc, sameTC ? pTc : common);161 }162 };163 const runSetups = (tc, common) => {164 currentTC = tc;165 if (tc === void 0) return;166 const sameTC = tc === common;167 const once = tc[once$];168 if (! sameTC || once === void 0) {169 const pTc = tc.tc;170 const promise = runSetups(pTc, sameTC ? pTc : common);171 if (promise !== void 0) {172 return promise.then(async () => {173 currentTC = tc;174 if (once !== void 0) {175 await runOnceCallbacks(once.before);176 }177 await runCallbacks(tc[before$]);178 });179 }180 currentTC = tc;181 if (once !== void 0) {182 const promise = runOnceCallbacks(once.before);183 if (promise !== void 0) {184 return promise.then(() => runCallbacks(tc[before$]));185 }186 }187 }188 return runCallbacks(tc[before$]);189 };190 const commonTC = (ot, nt) => {191 if (ot === void 0 || nt === void 0) {192 return;193 }194 let otc = ot.tc, ntc = nt.tc;195 if (otc === void 0 || ntc === void 0) {196 return;197 }198 while (ntc.level > otc.level) ntc = ntc.tc;199 while (otc.level > ntc.level) otc = otc.tc;200 while (ntc !== otc) {201 ntc = ntc.tc; otc = otc.tc;202 }203 return ntc;204 };205 const once = (tc) => tc[once$] || (tc[once$] = {before: new LinkedList(), after: new LinkedList()});206 const before = (tc, func) => (tc[before$] || (tc[before$] = new LinkedList())).addBack(func);207 const after = (tc, func) => (tc[after$] || (tc[after$] = new LinkedList())).addFront(func);208 const reset = (tc) => {209 tc[before$] = tc[after$] = tc[once$] = void 0;210 };211 class TestCase {212 constructor(name, tc, body) {213 this.name = name;214 this.tc = tc;215 this.level = tc === void 0 ? 0 : tc.level + 1;216 this.body = body;217 }218 fullName(name) {219 const ret = this.tc ? this.tc.fullName(this.name) : this.name;220 return name ? ret + ' ' + name : ret;221 }222 topTestCase() {223 let top = this;224 while (top.tc != null) top = top.tc;225 return top;226 }227 before(func) {228 once(this).before.addBack(func);229 }230 after(func) {231 once(this).after.addFront(func);232 }233 beforeEach(func) {before(this, func)}234 afterEach(func) {after(this, func)}235 addTest(name, body, skipped=false) {236 if (typeof name === 'string' && name[0] === '/' && name[1] === '/') {237 skipped = true;238 name = name.slice(2);239 }240 name = 'test ' + name + '.';241 Object.defineProperty(body, 'name', {value: name});242 const fn = this.fullName(name);243 if (Core.runArg === void 0 || fn.indexOf(Core.runArg) !== -1) {244 ++Core.testCount;245 if (skipped) {246 ++Core.skipCount;247 } else {248 tests.push(new Test(fn, this, body));249 }250 }251 }252 get moduleId() {253 return this.tc ? this.tc.moduleId : this.name + '-test';254 }255 }256 const restorSpy = (spy) => () => {spy.restore && spy.restore()};257 Core.testCase = (name, body) => new TestCase(name, void 0, body);258 Object.defineProperty(Core, 'currentTestCase', {get: () => currentTC});259 class Test {260 constructor(name, tc, body) {261 this.name = name;262 this.tc = tc;263 this.topTC = tc.topTestCase();264 this.body = body;265 this.mode = 'init';266 }267 after(func) {268 const tc = currentTC || this.tc;269 (isOnce270 ? once(tc).after.addFront(func)271 : after(tc, func))[temp$] = true;272 }273 spy(...args) {274 const spy = stubber.spy.apply(stubber, args);275 this.after(restorSpy(spy));276 return spy;277 }278 stub(...args) {279 const spy = stubber.stub.apply(stubber, args);280 this.after(restorSpy(spy));281 return spy;282 }283 intercept(...args) {284 const spy = stubber.intercept.apply(stubber, args);285 this.after(restorSpy(spy));286 return spy;287 }288 get func() {return this.body}289 get location() {290 let line = 1;291 const name = this.moduleId;292 const mod = module.get(name);293 if (mod != null) {294 const tcbody = mod.body.toString();295 const testName = this.name.replace(/^.*?\btest /, '').slice(0, -1);296 const testbody = `test("${testName}", ${this.body.toString()}`;297 let idx = tcbody.indexOf(testbody);298 if (idx !== -1) {299 for (let ni = tcbody.indexOf('\n'); ni !== -1 && ni < idx;300 ni = tcbody.indexOf('\n', ni + 1)) {301 ++line;302 }303 }304 }305 return {name, line};306 }307 get moduleId() {return this.topTC.moduleId;}308 }309 Test.prototype.onEnd = Test.prototype.after;310 let skipped = false;311 const expandTestCase = (tc, skipped=false) => {312 const origTC = currentTC;313 currentTC = tc;314 builder.exec(tc.body);315 currentTC = origTC;316 };317 const builder = {318 // aroundEach: body => currentTC.add('setUpAround', body),319 beforeEach: (body) => currentTC.beforeEach(body),320 afterEach: (body) => currentTC.afterEach(body),321 before: (body) => currentTC.before(body),322 after: (body) => {323 const {test} = Core;324 if (test === void 0) {325 currentTC.after(body);326 } else {327 test.after(body);328 }329 },330 test: (name, body) => currentTC.addTest(name, body, skipped),331 group: (name, body) => {332 const otc = currentTC;333 const os = skipped;334 if (name[0] === '/' && name[1] === '/') {335 skipped = true;336 name = name.slice(2);337 }338 const ntc = new TestCase(name, otc);339 try {340 currentTC = ntc;341 ntc.body = body;342 body(ntc);343 return ntc;344 } finally {345 skipped = os;346 currentTC = otc;347 }348 },349 exec: (body) => {350 if (typeof body === 'function') {351 body(builder);352 } else {353 for (const name in body) {354 const value = body[name];355 if (typeof value === 'function') {356 switch (name) {357 case 'setUp': case 'beforeEach':358 currentTC.beforeEach(value);359 break;360 case 'tearDown': case 'afterEach':361 currentTC.afterEach(value);362 break;363 case 'setUpOnce': case 'before':364 currentTC.before(value);365 break;366 case 'tearDownOnce': case 'after':367 currentTC.after(value);368 break;369 default:370 if (! name.startsWith('test ')) {371 assert.fail('misnamed test ' + currentTC.fullName(name), 1);372 }373 builder.test(name.slice(5), value);374 }375 } else {376 expandTestCase(new TestCase(name, currentTC, value));377 }378 }379 }380 },381 };382 builder.it = builder.test;383 builder.describe = builder.group;384 const testStart = () => {385 currTest.mode = 'before';386 const promise = Core.runCallBacks('testStart', currTest);387 nextFunc = setup;388 return promise === void 0 ? setup() : promise;389 };390 const setup = () => {391 const promise = runSetups(currTest.tc, common);392 nextFunc = runTest;393 return promise === void 0 ? runTest() : promise;394 };395 const runDone = () => {396 let isDone = false, resolve, reject;397 const done = (err) => {398 isDone = true;399 if (resolve !== void 0) {400 err === void 0 ? resolve() : reject(err);401 }402 };403 currTest.body(done);404 if (isDone) return;405 return new Promise((res, rej) => {resolve = res; reject = rej});406 };407 const runTest = () => {408 nextFunc = tearDown;409 currTest.mode = 'running';410 assertCount = Core.assertCount;411 const promise = currTest.body.length === 1412 ? runDone()413 : currTest.body();414 return promise == void 0 ? tearDown() : promise;415 };416 const tearDown = () => {417 common = commonTC(currTest, nextTest);418 nextFunc = testEnd;419 currTest.errors === void 0 && checkAssertionCount(currTest, assertCount);420 currTest.mode = 'after';421 const promise = runTearDowns(currTest.tc, common);422 return promise === void 0 ? testEnd() : promise;423 };424 const testEnd = () => {425 nextFunc = testStart;426 return Core.runCallBacks('testEnd', currTest);427 };428 const handleAsyncError = (err) => {429 asyncTimeout == 0 || clearTimeout(asyncTimeout);430 if (handleError(err)) {431 runNext();432 }433 };434 const handleError = (err) => {435 const {test} = Core;436 if (test === void 0) return false;437 test.success = false;438 if (err === 'abortTests') {439 Core.abort(err);440 return false;441 }442 const isAssertionError = err instanceof Core.AssertionError;443 if (! isAssertionError) {444 (test.errors ??= []).push(445 (err instanceof Error)446 ? util.extractError(err)447 : (err === 'timeout'448 ? 'Test timed out'449 : (err === 'wrongReturn'450 ? 'Unexpected return value'451 : err.toString())));452 }453 if (Core.test.mode !== 'running') {454 Core.sendErrors(test);455 Core.abort(`\n**** Failure during ${Core.test.mode} ****`);456 return false;457 }458 return true;459 };460 const timeExpired = () => {461 asyncTimeout = 0;462 handleError('timeout');463 runNext();464 };465 const runAsyncNext = () => {466 if (asyncTimeout != 0) {467 clearTimeout(asyncTimeout);468 asyncTimeout = 0;469 }470 runNext();471 };472 const runNext = () => {473 while (true) {474 if (Core.abortMode !== void 0) {475 if (Core.abortMode === 'end') {476 nextFunc = (Core.test !== void 0 && Core.test.mode !== 'after') ? tearDown : testStart;477 nextTest = void 0;478 nt = tests.length;479 } else {480 return;481 }482 }483 if (nextFunc === testStart) {484 if (nt == tests.length) {485 Core.lastTest = Core.test = tests = void 0;486 Core.runCallBacks('end');487 return;488 }489 Core.lastTest = lastTest = currTest;490 currTest = Core.test = tests[nt];491 tests[nt++] = null;492 nextTest = nt < tests.length ? tests[nt] : void 0;493 }494 try {495 const promise = nextFunc();496 if (promise !== void 0) {497 assertIsPromise(promise);498 asyncTimeout = setTimeout(timeExpired, MAX_TIME);499 promise.then(runAsyncNext, handleAsyncError);500 return;501 }502 } catch (err) {503 handleError(err);504 }505 }506 };507 Core.start = (testCases, runNextWrapper) => {508 tests = [];509 nt = assertCount = 0;510 Core.test = Core.lastTest = void 0;511 lastTest = currTest = nextTest = common = nextFunc = void 0;512 nextFunc = testStart;513 for (let i = 0; i < testCases.length; ++i) {514 const tc = testCases[i];515 if (tc === void 0) continue;516 skipped = false;517 expandTestCase(tc);518 testCases[i] = null;519 }520 return ifPromise(Core.runCallBacks('start'), runNext);521 };522 return TestCase;...

Full Screen

Full Screen

test_setup_test.js

Source:test_setup_test.js Github

copy

Full Screen

...37 });38 s.describe("#runTeardowns", s => {39 s.test("does nothing if there are no teardown functions", async () => {40 let setup = new TestSetup();41 await setup.runTeardowns();42 });43 s.test("runs added teardown function", async t => {44 let wasRun = false;45 let setup = new TestSetup();46 setup.teardown(() => wasRun = true);47 await setup.runTeardowns();48 t.equal(wasRun, true);49 });50 s.test("runs teardown functions in order", async t => {51 let teardowns = [];52 let setup = new TestSetup();53 setup.teardown(async () => {54 await Promise.resolve();55 teardowns.push('slow');56 });57 setup.teardown(async () => teardowns.push('fast'));58 await setup.runTeardowns();59 t.same(teardowns, ['slow', 'fast']);60 });61 s.test("passes provided arguments to teardown functions", async t => {62 let calledWith;63 let setup = new TestSetup();64 setup.teardown(arg => calledWith = arg);65 await setup.runTeardowns('foobar');66 t.equal(calledWith, 'foobar');67 });68 });69 s.describe("#runBeforeTestEndCallbacks", s => {70 s.test("behaves like other callbacks", async t => {71 let setup = new TestSetup();72 await setup.runBeforeTestEndCallbacks();73 let calledWith;74 setup.beforeTestEnd(arg => calledWith = arg);75 await setup.runBeforeTestEndCallbacks('foobar');76 t.equal(calledWith, 'foobar');77 });78 });79 s.describe("#clone", s => {...

Full Screen

Full Screen

ErrorBoundary.jsx

Source:ErrorBoundary.jsx Github

copy

Full Screen

...11 static getDerivedStateFromError(error) {12 return { hasError: true, error };13 }14 recover() {15 runTeardowns();16 this.setState({ hasError: false, error: undefined });17 }18 render() {19 const { children } = this.props;20 const { hasError, error } = this.state;21 if (!hasError) {22 return children;23 }24 // eslint-disable-next-line no-console25 console.error("error boundary caught:", error);26 if (this.props.render) {27 const Render = this.props.render;28 return <Render error={error} />;29 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const test = require('ava');2const { runTeardowns } = require('ava/lib/worker/subprocess');3test('test', async t => {4 t.teardown(() => {5 console.log('teardown');6 });7 await runTeardowns();8});9const test = require('ava');10const { runTeardowns } = require('ava/lib/worker/subprocess');11test('test', async t => {12 t.teardown(() => {13 console.log('teardown');14 });15 await runTeardowns();16});17const test = require('ava');18const { runTeardowns } = require('ava/lib/worker/subprocess');19test('test', async t => {20 t.teardown(() => {21 console.log('teardown');22 });23 await runTeardowns();24});25const test = require('ava');26const { runTeardowns } = require('ava/lib/worker/subprocess');27test('test', async t => {28 t.teardown(() => {29 console.log('teardown');30 });31 await runTeardowns();32});33const test = require('ava');34const { runTeardowns } = require('ava/lib/worker/subprocess');35test('test', async t => {36 t.teardown(() => {37 console.log('teardown');38 });39 await runTeardowns();40});41const test = require('ava');42const { runTeardowns } = require('ava/lib/worker/subprocess');43test('test', async t => {44 t.teardown(() => {45 console.log('teardown');46 });47 await runTeardowns();48});49const test = require('ava');50const { runTeardowns } = require('ava

Full Screen

Using AI Code Generation

copy

Full Screen

1import test from 'ava';2import { runTeardowns } from 'ava/lib/worker/teardown';3import { teardown } from './teardown';4test.after.always(async () => {5 await runTeardowns([teardown]);6});

Full Screen

Using AI Code Generation

copy

Full Screen

1import test from 'ava';2test.afterEach.always((t) => {3 t.context.runTeardowns();4});5test('test', async (t) => {6 const teardown = () => {7 console.log('teardown');8 };9 t.context.addTeardown(teardown);10 t.pass();11});12### after.always()13### afterEach.always()14### before()15### beforeEach()16### before.always()17### beforeEach.always()18### addTeardown()19### runTeardowns()

Full Screen

Using AI Code Generation

copy

Full Screen

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

Full Screen

Using AI Code Generation

copy

Full Screen

1import test from 'ava';2import { runTeardowns } from 'ava/lib/worker/child';3test.afterEach.always(() => {4 runTeardowns();5});6MIT © [Rajasegar](

Full Screen

Using AI Code Generation

copy

Full Screen

1test('my test', async t => {2 t.context.teardowns.push(() => {3 });4 await t.context.runTeardowns();5});6test('my test', async t => {7 t.context.teardowns.push(() => {8 });9 await t.context.runTeardowns();10});11test('my test', async t => {12 t.context.teardowns.push(() => {13 });14 await t.context.runTeardowns();15});16test('my test', async t => {17 t.context.teardowns.push(() => {18 });19 await t.context.runTeardowns();20});21test('my test', async t => {22 t.context.teardowns.push(() => {23 });24 await t.context.runTeardowns();25});26test('my test', async t => {27 t.context.teardowns.push(() => {28 });29 await t.context.runTeardowns();30});31test('my test', async t => {32 t.context.teardowns.push(() => {33 });34 await t.context.runTeardowns();35});36test('my test', async t => {37 t.context.teardowns.push(() => {38 });39 await t.context.runTeardowns();40});41test('my test', async t => {42 t.context.teardowns.push(() => {43 });44 await t.context.runTeardowns();45});46test('my test', async t => {47 t.context.teardowns.push(() =>

Full Screen

Using AI Code Generation

copy

Full Screen

1import test from 'ava';2import { runTeardowns } from 'ava/lib/worker/subprocess';3test.after.always(() => {4 runTeardowns();5});6import test from 'ava';7test.after.always(() => {8});9import test from 'ava';10test.afterEach.always(t => {11 if (t.failed) {12 }13});14import test from 'ava';15test.afterEach.always(() => {16});17import test from 'ava';18test.afterEach.always(t => {19 if (t.failed) {20 }21});22import test from 'ava';23test.afterEach.always('test.js', () => {24});25import test from 'ava';26test.afterEach.always('test.js', t => {27 if (t.failed) {28 }29});

Full Screen

Using AI Code Generation

copy

Full Screen

1import ava from 'ava';2ava.runTeardowns();3### `ava.serial([title], implementation)`4### `ava.only([title], implementation)`5### `ava.skip([title], implementation)`6### `ava.failing([title], implementation)`7### `ava.todo([title])`8### `ava.cb([title], implementation)`9### `ava.before([title], implementation)`10### `ava.beforeEach([title], implementation)`11### `ava.after([title], implementation)`12### `ava.afterEach([title], implementation)`13### `ava.context([title], implementation)`14### `ava.only([title], implementation)`15### `ava.skip([title], implementation)`16### `ava.failing([title], implementation)`

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