Best JavaScript code snippet using ava
runner.js
Source:runner.js
...11 }).then(() => runner);12};13test('nested tests and hooks arenât allowed', t => {14 t.plan(1);15 return promiseEnd(new Runner(), runner => {16 runner.chain('test', a => {17 t.throws(() => {18 runner.chain(noop);19 }, {message: 'All tests and hooks must be declared synchronously in your test file, and cannot be nested within other tests or hooks.'});20 a.pass();21 });22 });23});24test('tests must be declared synchronously', t => {25 t.plan(1);26 return promiseEnd(new Runner(), runner => {27 runner.chain('test', a => {28 a.pass();29 return Promise.resolve();30 });31 }).then(runner => {32 t.throws(() => {33 runner.chain(noop);34 }, {message: 'All tests and hooks must be declared synchronously in your test file, and cannot be nested within other tests or hooks.'});35 });36});37test('runner emits "stateChange" events', t => {38 const runner = new Runner();39 runner.on('stateChange', evt => {40 if (evt.type === 'declared-test') {41 t.deepEqual(evt, {42 type: 'declared-test',43 title: 'foo',44 knownFailing: false,45 todo: false46 });47 t.end();48 }49 });50 runner.chain('foo', a => {51 a.pass();52 });53});54test('run serial tests before concurrent ones', t => {55 const array = [];56 return promiseEnd(new Runner(), runner => {57 runner.chain('test', a => {58 array.push('c');59 a.end();60 });61 runner.chain.serial('serial', a => {62 array.push('a');63 a.end();64 });65 runner.chain.serial('serial 2', a => {66 array.push('b');67 a.end();68 });69 }).then(() => {70 t.strictDeepEqual(array, ['a', 'b', 'c']);71 });72});73test('anything can be skipped', t => {74 const array = [];75 function pusher(title) {76 return a => {77 array.push(title);78 a.pass();79 };80 }81 return promiseEnd(new Runner(), runner => {82 runner.chain.after(pusher('after'));83 runner.chain.after.skip(pusher('after.skip'));84 runner.chain.afterEach(pusher('afterEach'));85 runner.chain.afterEach.skip(pusher('afterEach.skip'));86 runner.chain.before(pusher('before'));87 runner.chain.before.skip(pusher('before.skip'));88 runner.chain.beforeEach(pusher('beforeEach'));89 runner.chain.beforeEach.skip(pusher('beforeEach.skip'));90 runner.chain('concurrent', pusher('concurrent'));91 runner.chain.skip('concurrent.skip', pusher('concurrent.skip'));92 runner.chain.serial('serial', pusher('serial'));93 runner.chain.serial.skip('serial.skip', pusher('serial.skip'));94 }).then(() => {95 // Note that afterEach and beforeEach run twice because there are two actual tests - "serial" and "concurrent"96 t.strictDeepEqual(array, [97 'before',98 'beforeEach',99 'serial',100 'afterEach',101 'beforeEach',102 'concurrent',103 'afterEach',104 'after'105 ]);106 });107});108test('test types and titles', t => {109 t.plan(10);110 const fail = a => a.fail();111 const pass = a => a.pass();112 const check = (setup, expect) => {113 const runner = new Runner();114 runner.on('stateChange', evt => {115 if (evt.type === 'hook-failed' || evt.type === 'test-failed' || evt.type === 'test-passed') {116 const expected = expect.shift();117 t.is(evt.title, expected.title);118 }119 });120 return promiseEnd(runner, () => setup(runner.chain));121 };122 return Promise.all([123 check(chain => {124 chain.before(fail);125 chain('test', pass);126 }, [127 {type: 'before', title: 'before hook'}128 ]),129 check(chain => {130 chain('test', pass);131 chain.after(fail);132 }, [133 {type: 'test', title: 'test'},134 {type: 'after', title: 'after hook'}135 ]),136 check(chain => {137 chain('test', pass);138 chain.after.always(fail);139 }, [140 {type: 'test', title: 'test'},141 {type: 'after', title: 'after.always hook'}142 ]),143 check(chain => {144 chain.beforeEach(fail);145 chain('test', fail);146 }, [147 {type: 'beforeEach', title: 'beforeEach hook for test'}148 ]),149 check(chain => {150 chain('test', pass);151 chain.afterEach(fail);152 }, [153 {type: 'test', title: 'test'},154 {type: 'afterEach', title: 'afterEach hook for test'}155 ]),156 check(chain => {157 chain('test', pass);158 chain.afterEach.always(fail);159 }, [160 {type: 'test', title: 'test'},161 {type: 'afterEach', title: 'afterEach.always hook for test'}162 ])163 ]);164});165test('skip test', t => {166 t.plan(3);167 const array = [];168 return promiseEnd(new Runner(), runner => {169 runner.on('stateChange', evt => {170 if (evt.type === 'selected-test' && evt.skip) {171 t.pass();172 }173 if (evt.type === 'test-passed') {174 t.pass();175 }176 });177 runner.chain('test', a => {178 array.push('a');179 a.pass();180 });181 runner.chain.skip('skip', () => {182 array.push('b');183 });184 }).then(() => {185 t.strictDeepEqual(array, ['a']);186 });187});188test('tests must have a non-empty title)', t => {189 t.plan(1);190 return promiseEnd(new Runner(), runner => {191 t.throws(() => {192 runner.chain('', t => t.pass());193 }, new TypeError('Tests must have a title'));194 });195});196test('test titles must be unique', t => {197 t.plan(1);198 return promiseEnd(new Runner(), runner => {199 runner.chain('title', t => t.pass());200 t.throws(() => {201 runner.chain('title', t => t.pass());202 }, new Error('Duplicate test title: title'));203 });204});205test('tests must have an implementation', t => {206 t.plan(1);207 const runner = new Runner();208 t.throws(() => {209 runner.chain('title');210 }, new TypeError('Expected an implementation. Use `test.todo()` for tests without an implementation.'));211});212test('todo test', t => {213 t.plan(3);214 const array = [];215 return promiseEnd(new Runner(), runner => {216 runner.on('stateChange', evt => {217 if (evt.type === 'selected-test' && evt.todo) {218 t.pass();219 }220 if (evt.type === 'test-passed') {221 t.pass();222 }223 });224 runner.chain('test', a => {225 array.push('a');226 a.pass();227 });228 runner.chain.todo('todo');229 }).then(() => {230 t.strictDeepEqual(array, ['a']);231 });232});233test('todo tests must not have an implementation', t => {234 t.plan(1);235 return promiseEnd(new Runner(), runner => {236 t.throws(() => {237 runner.chain.todo('todo', () => {});238 }, new TypeError('`todo` tests are not allowed to have an implementation. Use `test.skip()` for tests with an implementation.'));239 });240});241test('todo tests must have a title', t => {242 t.plan(1);243 return promiseEnd(new Runner(), runner => {244 t.throws(() => {245 runner.chain.todo();246 }, new TypeError('`todo` tests require a title'));247 });248});249test('todo test titles must be unique', t => {250 t.plan(1);251 return promiseEnd(new Runner(), runner => {252 runner.chain('title', t => t.pass());253 t.throws(() => {254 runner.chain.todo('title');255 }, new Error('Duplicate test title: title'));256 });257});258test('only test', t => {259 t.plan(2);260 const array = [];261 return promiseEnd(new Runner(), runner => {262 runner.on('stateChange', evt => {263 if (evt.type === 'selected-test') {264 t.pass();265 }266 });267 runner.chain('test', a => {268 array.push('a');269 a.pass();270 });271 runner.chain.only('only', a => {272 array.push('b');273 a.pass();274 });275 }).then(() => {276 t.strictDeepEqual(array, ['b']);277 });278});279test('options.runOnlyExclusive means only exclusive tests are run', t => {280 t.plan(1);281 return promiseEnd(new Runner({runOnlyExclusive: true}), runner => {282 runner.chain('test', () => {283 t.fail();284 });285 runner.chain.only('test 2', () => {286 t.pass();287 });288 });289});290test('options.serial forces all tests to be serial', t => {291 t.plan(1);292 const array = [];293 return promiseEnd(new Runner({serial: true}), runner => {294 runner.chain.cb('cb', a => {295 setTimeout(() => {296 array.push(1);297 a.end();298 }, 200);299 a.pass();300 });301 runner.chain.cb('cb 2', a => {302 setTimeout(() => {303 array.push(2);304 a.end();305 }, 100);306 a.pass();307 });308 runner.chain('test', a => {309 a.pass();310 t.strictDeepEqual(array, [1, 2]);311 });312 });313});314test('options.failFast does not stop concurrent tests from running', t => {315 const expected = ['first', 'second'];316 t.plan(expected.length);317 promiseEnd(new Runner({failFast: true}), runner => {318 let block;319 let resume;320 runner.chain.beforeEach(() => {321 if (block) {322 return block;323 }324 block = new Promise(resolve => {325 resume = resolve;326 });327 });328 runner.chain('first', a => {329 resume();330 a.fail();331 });332 runner.chain('second', a => {333 a.pass();334 });335 runner.on('stateChange', evt => {336 if (evt.type === 'test-failed' || evt.type === 'test-passed') {337 t.is(evt.title, expected.shift());338 }339 });340 });341});342test('options.failFast && options.serial stops subsequent tests from running ', t => {343 const expected = ['first'];344 t.plan(expected.length);345 promiseEnd(new Runner({failFast: true, serial: true}), runner => {346 let block;347 let resume;348 runner.chain.beforeEach(() => {349 if (block) {350 return block;351 }352 block = new Promise(resolve => {353 resume = resolve;354 });355 });356 runner.chain('first', a => {357 resume();358 a.fail();359 });360 runner.chain('second', a => {361 a.pass();362 });363 runner.on('stateChange', evt => {364 if (evt.type === 'test-failed' || evt.type === 'test-passed') {365 t.is(evt.title, expected.shift());366 }367 });368 });369});370test('options.failFast & failing serial test stops subsequent tests from running ', t => {371 const expected = ['first'];372 t.plan(expected.length);373 promiseEnd(new Runner({failFast: true, serial: true}), runner => {374 let block;375 let resume;376 runner.chain.beforeEach(() => {377 if (block) {378 return block;379 }380 block = new Promise(resolve => {381 resume = resolve;382 });383 });384 runner.chain.serial('first', a => {385 resume();386 a.fail();387 });388 runner.chain.serial('second', a => {389 a.pass();390 });391 runner.chain('third', a => {392 a.pass();393 });394 runner.on('stateChange', evt => {395 if (evt.type === 'test-failed' || evt.type === 'test-passed') {396 t.is(evt.title, expected.shift());397 }398 });399 });400});401test('options.match will not run tests with non-matching titles', t => {402 t.plan(4);403 return promiseEnd(new Runner({match: ['*oo', '!foo']}), runner => {404 runner.on('stateChange', evt => {405 if (evt.type === 'test-passed') {406 t.pass();407 }408 });409 runner.chain('mhm. grass tasty. moo', a => {410 t.pass();411 a.pass();412 });413 runner.chain('juggaloo', a => {414 t.pass();415 a.pass();416 });417 runner.chain('foo', a => {418 t.fail();419 a.pass();420 });421 runner.chain('test', a => {422 t.fail();423 a.pass();424 });425 });426});427test('options.match hold no effect on hooks with titles', t => {428 t.plan(2);429 return promiseEnd(new Runner({match: ['!before*']}), runner => {430 runner.on('stateChange', evt => {431 if (evt.type === 'test-passed') {432 t.pass();433 }434 });435 let actual;436 runner.chain.before('before hook with title', () => {437 actual = 'foo';438 });439 runner.chain('after', a => {440 t.is(actual, 'foo');441 a.pass();442 });443 });444});445test('options.match overrides .only', t => {446 t.plan(4);447 return promiseEnd(new Runner({match: ['*oo']}), runner => {448 runner.on('stateChange', evt => {449 if (evt.type === 'test-passed') {450 t.pass();451 }452 });453 runner.chain('moo', a => {454 t.pass();455 a.pass();456 });457 runner.chain.only('boo', a => {458 t.pass();459 a.pass();460 });461 });462});463test('options.match matches todo tests', t => {464 t.plan(1);465 return promiseEnd(new Runner({match: ['*oo']}), runner => {466 runner.on('stateChange', evt => {467 if (evt.type === 'selected-test' && evt.todo) {468 t.pass();469 }470 });471 runner.chain.todo('moo');472 runner.chain.todo('oom');473 });474});475test('macros: Additional args will be spread as additional args on implementation function', t => {476 t.plan(3);477 return promiseEnd(new Runner(), runner => {478 runner.on('stateChange', evt => {479 if (evt.type === 'test-passed') {480 t.pass();481 }482 });483 runner.chain.before((a, ...rest) => {484 t.deepEqual(rest, ['foo', 'bar']);485 a.pass();486 }, 'foo', 'bar');487 runner.chain('test1', (a, ...rest) => {488 t.deepEqual(rest, ['foo', 'bar']);489 a.pass();490 }, 'foo', 'bar');491 });492});493test('macros: Customize test names attaching a `title` function', t => {494 t.plan(6);495 const expectedTitles = [496 'defaultA',497 'suppliedB',498 'defaultC'499 ];500 const expectedArgs = [501 ['A'],502 ['B'],503 ['C']504 ];505 function macroFn(avaT, ...rest) {506 t.deepEqual(rest, expectedArgs.shift());507 avaT.pass();508 }509 macroFn.title = (title = 'default', firstArg = undefined) => title + firstArg;510 return promiseEnd(new Runner(), runner => {511 runner.on('stateChange', evt => {512 if (evt.type === 'declared-test') {513 t.is(evt.title, expectedTitles.shift());514 }515 });516 runner.chain(macroFn, 'A');517 runner.chain('supplied', macroFn, 'B');518 runner.chain(macroFn, 'C');519 });520});521test('macros: test titles must be strings', t => {522 t.plan(1);523 return promiseEnd(new Runner(), runner => {524 t.throws(() => {525 const macro = t => t.pass();526 macro.title = () => [];527 runner.chain(macro);528 }, new TypeError('Test & hook titles must be strings'));529 });530});531test('macros: hook titles must be strings', t => {532 t.plan(1);533 return promiseEnd(new Runner(), runner => {534 t.throws(() => {535 const macro = t => t.pass();536 macro.title = () => [];537 runner.chain.before(macro);538 }, new TypeError('Test & hook titles must be strings'));539 });540});541test('match applies to macros', t => {542 t.plan(1);543 function macroFn(avaT) {544 avaT.pass();545 }546 macroFn.title = (title, firstArg) => `${firstArg}bar`;547 return promiseEnd(new Runner({match: ['foobar']}), runner => {548 runner.on('stateChange', evt => {549 if (evt.type === 'test-passed') {550 t.is(evt.title, 'foobar');551 }552 });553 runner.chain(macroFn, 'foo');554 runner.chain(macroFn, 'bar');555 });556});557test('arrays of macros', t => {558 const expectedArgsA = [559 ['A'],560 ['B'],561 ['C']562 ];563 const expectedArgsB = [564 ['A'],565 ['B'],566 ['D']567 ];568 function macroFnA(a, ...rest) {569 t.deepEqual(rest, expectedArgsA.shift());570 a.pass();571 }572 macroFnA.title = prefix => `${prefix}.A`;573 function macroFnB(a, ...rest) {574 t.deepEqual(rest, expectedArgsB.shift());575 a.pass();576 }577 macroFnB.title = prefix => `${prefix}.B`;578 return promiseEnd(new Runner(), runner => {579 runner.on('stateChange', evt => {580 if (evt.type === 'test-passed') {581 t.pass();582 }583 });584 runner.chain('A', [macroFnA, macroFnB], 'A');585 runner.chain('B', [macroFnA, macroFnB], 'B');586 runner.chain('C', macroFnA, 'C');587 runner.chain('D', macroFnB, 'D');588 }).then(() => {589 t.is(expectedArgsA.length, 0);590 t.is(expectedArgsB.length, 0);591 });592});593test('match applies to arrays of macros', t => {594 t.plan(1);595 // Foo596 function fooMacro(a) {597 t.fail();598 a.pass();599 }600 fooMacro.title = (title, firstArg) => `${firstArg}foo`;601 function barMacro(avaT) {602 avaT.pass();603 }604 barMacro.title = (title, firstArg) => `${firstArg}bar`;605 function bazMacro(a) {606 t.fail();607 a.pass();608 }609 bazMacro.title = (title, firstArg) => `${firstArg}baz`;610 return promiseEnd(new Runner({match: ['foobar']}), runner => {611 runner.on('stateChange', evt => {612 if (evt.type === 'test-passed') {613 t.is(evt.title, 'foobar');614 }615 });616 runner.chain([fooMacro, barMacro, bazMacro], 'foo');617 runner.chain([fooMacro, barMacro, bazMacro], 'bar');618 });619});620test('silently skips other tests when .only is used', t => {621 t.plan(1);622 return promiseEnd(new Runner(), runner => {623 runner.on('stateChange', evt => {624 if (evt.type === 'test-passed') {625 t.pass();626 }627 });628 runner.chain('skip me', a => a.pass());629 runner.chain.serial('skip me too', a => a.pass());630 runner.chain.only('only me', a => a.pass());631 });632});633test('subsequent always hooks are run even if earlier always hooks fail', t => {634 t.plan(3);635 return promiseEnd(new Runner(), runner => {636 runner.chain('test', a => a.pass());637 runner.chain.serial.after.always(a => {638 t.pass();639 a.fail();640 });641 runner.chain.serial.after.always(a => {642 t.pass();643 a.fail();644 });645 runner.chain.after.always(a => {646 t.pass();647 a.fail();648 });649 });650});651test('hooks run concurrently, but can be serialized', t => {652 t.plan(7);653 let activeCount = 0;654 return promiseEnd(new Runner(), runner => {655 runner.chain('test', a => a.pass());656 runner.chain.before(() => {657 t.is(activeCount, 0);658 activeCount++;659 return new Promise(resolve => {660 setTimeout(resolve, 20);661 }).then(() => {662 activeCount--;663 });664 });665 runner.chain.before(() => {666 t.is(activeCount, 1);667 activeCount++;668 return new Promise(resolve => {...
hooks.js
Source:hooks.js
...11};12test('before', t => {13 t.plan(1);14 const array = [];15 return promiseEnd(new Runner(), runner => {16 runner.chain.before(() => {17 array.push('a');18 });19 runner.chain('test', a => {20 a.pass();21 array.push('b');22 });23 }).then(() => {24 t.strictDeepEqual(array, ['a', 'b']);25 });26});27test('after', t => {28 t.plan(2);29 const array = [];30 return promiseEnd(new Runner(), runner => {31 runner.on('stateChange', evt => {32 if (evt.type === 'test-passed') {33 t.pass();34 }35 });36 runner.chain.after(() => {37 array.push('b');38 });39 runner.chain('test', a => {40 a.pass();41 array.push('a');42 });43 }).then(() => {44 t.strictDeepEqual(array, ['a', 'b']);45 });46});47test('after not run if test failed', t => {48 t.plan(2);49 const array = [];50 return promiseEnd(new Runner(), runner => {51 runner.on('stateChange', evt => {52 if (evt.type === 'test-failed') {53 t.pass();54 }55 });56 runner.chain.after(() => {57 array.push('a');58 });59 runner.chain('test', () => {60 throw new Error('something went wrong');61 });62 }).then(() => {63 t.strictDeepEqual(array, []);64 });65});66test('after.always run even if test failed', t => {67 t.plan(2);68 const array = [];69 return promiseEnd(new Runner(), runner => {70 runner.on('stateChange', evt => {71 if (evt.type === 'test-failed') {72 t.pass();73 }74 });75 runner.chain.after.always(() => {76 array.push('a');77 });78 runner.chain('test', () => {79 throw new Error('something went wrong');80 });81 }).then(() => {82 t.strictDeepEqual(array, ['a']);83 });84});85test('after.always run even if before failed', t => {86 t.plan(1);87 const array = [];88 return promiseEnd(new Runner(), runner => {89 runner.chain.before(() => {90 throw new Error('something went wrong');91 });92 runner.chain('test', a => a.pass());93 runner.chain.after.always(() => {94 array.push('a');95 });96 }).then(() => {97 t.strictDeepEqual(array, ['a']);98 });99});100test('stop if before hooks failed', t => {101 t.plan(1);102 const array = [];103 return promiseEnd(new Runner(), runner => {104 runner.chain.before(() => {105 array.push('a');106 });107 runner.chain.before(() => {108 throw new Error('something went wrong');109 });110 runner.chain('test', a => {111 a.pass();112 array.push('b');113 a.end();114 });115 }).then(() => {116 t.strictDeepEqual(array, ['a']);117 });118});119test('before each with concurrent tests', t => {120 t.plan(1);121 const array = [[], []];122 return promiseEnd(new Runner(), runner => {123 let i = 0;124 let k = 0;125 runner.chain.beforeEach(() => {126 array[i++].push('a');127 });128 runner.chain.beforeEach(() => {129 array[k++].push('b');130 });131 runner.chain('c', a => {132 a.pass();133 array[0].push('c');134 });135 runner.chain('d', a => {136 a.pass();137 array[1].push('d');138 });139 }).then(() => {140 t.strictDeepEqual(array, [['a', 'b', 'c'], ['a', 'b', 'd']]);141 });142});143test('before each with serial tests', t => {144 t.plan(1);145 const array = [];146 return promiseEnd(new Runner(), runner => {147 runner.chain.beforeEach(() => {148 array.push('a');149 });150 runner.chain.beforeEach(() => {151 array.push('b');152 });153 runner.chain.serial('c', a => {154 a.pass();155 array.push('c');156 });157 runner.chain.serial('d', a => {158 a.pass();159 array.push('d');160 });161 }).then(() => {162 t.strictDeepEqual(array, ['a', 'b', 'c', 'a', 'b', 'd']);163 });164});165test('fail if beforeEach hook fails', t => {166 t.plan(2);167 const array = [];168 return promiseEnd(new Runner(), runner => {169 runner.on('stateChange', evt => {170 if (evt.type === 'hook-failed') {171 t.pass();172 }173 });174 runner.chain.beforeEach(a => {175 array.push('a');176 a.fail();177 });178 runner.chain('test', a => {179 array.push('b');180 a.pass();181 });182 }).then(() => {183 t.strictDeepEqual(array, ['a']);184 });185});186test('after each with concurrent tests', t => {187 t.plan(1);188 const array = [[], []];189 return promiseEnd(new Runner(), runner => {190 let i = 0;191 let k = 0;192 runner.chain.afterEach(() => {193 array[i++].push('a');194 });195 runner.chain.afterEach(() => {196 array[k++].push('b');197 });198 runner.chain('c', a => {199 a.pass();200 array[0].push('c');201 });202 runner.chain('d', a => {203 a.pass();204 array[1].push('d');205 });206 }).then(() => {207 t.strictDeepEqual(array, [['c', 'a', 'b'], ['d', 'a', 'b']]);208 });209});210test('after each with serial tests', t => {211 t.plan(1);212 const array = [];213 return promiseEnd(new Runner(), runner => {214 runner.chain.afterEach(() => {215 array.push('a');216 });217 runner.chain.afterEach(() => {218 array.push('b');219 });220 runner.chain.serial('c', a => {221 a.pass();222 array.push('c');223 });224 runner.chain.serial('d', a => {225 a.pass();226 array.push('d');227 });228 }).then(() => {229 t.strictDeepEqual(array, ['c', 'a', 'b', 'd', 'a', 'b']);230 });231});232test('afterEach not run if concurrent tests failed', t => {233 t.plan(1);234 const array = [];235 return promiseEnd(new Runner(), runner => {236 runner.chain.afterEach(() => {237 array.push('a');238 });239 runner.chain('test', () => {240 throw new Error('something went wrong');241 });242 }).then(() => {243 t.strictDeepEqual(array, []);244 });245});246test('afterEach not run if serial tests failed', t => {247 t.plan(1);248 const array = [];249 return promiseEnd(new Runner(), runner => {250 runner.chain.afterEach(() => {251 array.push('a');252 });253 runner.chain.serial('test', () => {254 throw new Error('something went wrong');255 });256 }).then(() => {257 t.strictDeepEqual(array, []);258 });259});260test('afterEach.always run even if concurrent tests failed', t => {261 t.plan(1);262 const array = [];263 return promiseEnd(new Runner(), runner => {264 runner.chain.afterEach.always(() => {265 array.push('a');266 });267 runner.chain('test', () => {268 throw new Error('something went wrong');269 });270 }).then(() => {271 t.strictDeepEqual(array, ['a']);272 });273});274test('afterEach.always run even if serial tests failed', t => {275 t.plan(1);276 const array = [];277 return promiseEnd(new Runner(), runner => {278 runner.chain.afterEach.always(() => {279 array.push('a');280 });281 runner.chain.serial('test', () => {282 throw new Error('something went wrong');283 });284 }).then(() => {285 t.strictDeepEqual(array, ['a']);286 });287});288test('afterEach.always run even if beforeEach failed', t => {289 t.plan(1);290 const array = [];291 return promiseEnd(new Runner(), runner => {292 runner.chain.beforeEach(() => {293 throw new Error('something went wrong');294 });295 runner.chain('test', a => {296 a.pass();297 array.push('a');298 });299 runner.chain.afterEach.always(() => {300 array.push('b');301 });302 }).then(() => {303 t.strictDeepEqual(array, ['b']);304 });305});306test('afterEach: property `passed` of execution-context is false when test failed and true when test passed', t => {307 t.plan(1);308 const passed = [];309 let i;310 return promiseEnd(new Runner(), runner => {311 runner.chain.afterEach(a => {312 passed[i] = a.passed;313 });314 runner.chain('failure', () => {315 i = 0;316 throw new Error('something went wrong');317 });318 runner.chain('pass', a => {319 i = 1;320 a.pass();321 });322 }).then(() => {323 t.strictDeepEqual(passed, [undefined, true]);324 });325});326test('afterEach.always: property `passed` of execution-context is false when test failed and true when test passed', t => {327 t.plan(1);328 const passed = [];329 let i;330 return promiseEnd(new Runner(), runner => {331 runner.chain.afterEach.always(a => {332 passed[i] = a.passed;333 });334 runner.chain('failure', () => {335 i = 0;336 throw new Error('something went wrong');337 });338 runner.chain('pass', a => {339 i = 1;340 a.pass();341 });342 }).then(() => {343 t.strictDeepEqual(passed, [false, true]);344 });345});346test('afterEach.always: property `passed` of execution-context is false when before hook failed', t => {347 t.plan(1);348 let passed;349 return promiseEnd(new Runner(), runner => {350 runner.chain.before(() => {351 throw new Error('something went wrong');352 });353 runner.chain.afterEach.always(a => {354 passed = a.passed;355 });356 runner.chain('pass', a => {357 a.pass();358 });359 }).then(() => {360 t.false(passed);361 });362});363test('afterEach.always: property `passed` of execution-context is true when test passed and afterEach hook failed', t => {364 t.plan(1);365 let passed;366 return promiseEnd(new Runner(), runner => {367 runner.chain.afterEach(() => {368 throw new Error('something went wrong');369 });370 runner.chain.afterEach.always(a => {371 passed = a.passed;372 });373 runner.chain('pass', a => {374 a.pass();375 });376 }).then(() => {377 t.true(passed);378 });379});380test('ensure hooks run only around tests', t => {381 t.plan(1);382 const array = [];383 return promiseEnd(new Runner(), runner => {384 runner.chain.beforeEach(() => {385 array.push('beforeEach');386 });387 runner.chain.before(() => {388 array.push('before');389 });390 runner.chain.afterEach(() => {391 array.push('afterEach');392 });393 runner.chain.after(() => {394 array.push('after');395 });396 runner.chain('test', a => {397 a.pass();398 array.push('test');399 });400 }).then(() => {401 t.strictDeepEqual(array, ['before', 'beforeEach', 'test', 'afterEach', 'after']);402 });403});404test('shared context', t => {405 return promiseEnd(new Runner(), runner => {406 runner.on('stateChange', evt => {407 if (evt.type === 'hook-failed' || evt.type === 'test-failed') {408 t.fail();409 }410 });411 runner.chain.before(a => {412 a.deepEqual(a.context, {});413 a.context.arr = ['a'];414 a.context.prop = 'before';415 });416 runner.chain.after(a => {417 a.deepEqual(a.context.arr, ['a', 'b', 'c', 'd']);418 a.is(a.context.prop, 'before');419 });420 runner.chain.beforeEach(a => {421 a.deepEqual(a.context.arr, ['a']);422 a.context.arr.push('b');423 a.is(a.context.prop, 'before');424 a.context.prop = 'beforeEach';425 });426 runner.chain('test', a => {427 a.pass();428 a.deepEqual(a.context.arr, ['a', 'b']);429 a.context.arr.push('c');430 a.is(a.context.prop, 'beforeEach');431 a.context.prop = 'test';432 });433 runner.chain.afterEach(a => {434 a.deepEqual(a.context.arr, ['a', 'b', 'c']);435 a.context.arr.push('d');436 a.is(a.context.prop, 'test');437 a.context.prop = 'afterEach';438 });439 });440});441test('shared context of any type', t => {442 return promiseEnd(new Runner(), runner => {443 runner.on('stateChange', evt => {444 if (evt.type === 'hook-failed' || evt.type === 'test-failed') {445 t.fail();446 }447 });448 runner.chain.beforeEach(a => {449 a.context = 'foo';450 });451 runner.chain('test', a => {452 a.pass();453 a.is(a.context, 'foo');454 });455 });456});457test('teardowns cannot be used in hooks', async t => {458 let hookFailure = null;459 await promiseEnd(new Runner(), runner => {460 runner.on('stateChange', evt => {461 if (evt.type === 'hook-failed') {462 hookFailure = evt;463 }464 });465 runner.chain.beforeEach(a => {466 a.teardown(() => {});467 });468 runner.chain('test', a => a.pass());469 });470 t.ok(hookFailure);471 t.match(hookFailure.err.message, /not allowed in hooks/);...
Using AI Code Generation
1promiseEnd = require('promise-end');2promiseEnd = require('promise-end')(require('bluebird'));3promiseEnd = require('promise-end')(require('q'));4promiseEnd = require('promise-end')(require('when'));5var promiseEnd = require('promise-end');6var promise = promiseEnd(Promise.resolve(10));7promise.then(function(value) {8 console.log(value);9});10var promiseEnd = require('promise-end');11promiseEnd(Promise.resolve(10)).then(function(value) {12 console.log(value);13});14var promiseEnd = require('promise-end');15promiseEnd(Promise.resolve(10)).then(function(value) {16 console.log(value);17 return value + 1;18}).then(function(value) {19 console.log(value);20});21var promiseEnd = require('promise-end');22promiseEnd(
Using AI Code Generation
1var promise = connection.promiseEnd('SELECT 1 + 1 AS solution');2promise.then(function(rows) {3 console.log('The solution is: ', rows[0].solution);4})5.catch(function(err) {6 console.error(err);7});8var promise = connection.promiseEnd('SELECT 1 + 1 AS solution');9promise.then(function(rows) {10 console.log('The solution is: ', rows[0].solution);11})12.catch(function(err) {13 console.error(err);14});15var promise = connection.promiseEnd('SELECT 1 + 1 AS solution');16promise.then(function(rows) {17 console.log('The solution is: ', rows[0].solution);18})19.catch(function(err) {20 console.error(err);21});22var promise = connection.promiseEnd('SELECT 1 + 1 AS solution');23promise.then(function(rows) {24 console.log('The solution is: ', rows[0].solution);25})26.catch(function(err) {27 console.error(err);28});29var promise = connection.promiseEnd('SELECT 1 + 1 AS solution');30promise.then(function(rows) {31 console.log('The solution is: ', rows[0].solution);32})33.catch(function(err) {34 console.error(err);35});36var promise = connection.promiseEnd('SELECT 1 + 1 AS solution');
Using AI Code Generation
1var promise = require('bluebird');2promise.config({3});4var promiseEnd = promise.prototype.end;5promise.prototype.end = function() {6 return this.then(function(data) {7 return promiseEnd.call(data);8 });9};10var Promise = require('bluebird');11var fs = Promise.promisifyAll(require('fs'));12var path = require('path');13var file = path.join(__dirname, 'file.txt');14var file1 = path.join(__dirname, 'file1.txt');15var file2 = path.join(__dirname, 'file2.txt');16Promise.all([fs.readFileAsync(file, 'utf8'), fs.readFileAsync(file1, 'utf8'), fs.readFileAsync(file2, 'utf8')]).then(function(data) {17 console.log(data);18});
Using AI Code Generation
1const pgp = require('pg-promise')();2db.query('SELECT * FROM users')3 .then((result) => {4 console.log(result);5 })6 .catch((err) => {7 console.log(err);8 });9db.query('SELECT * FROM users')10 .then((result) => {11 console.log(result);12 })13 .catch((err) => {14 console.log(err);15 })16 .finally(() => {17 pgp.end();18 });19const pgp = require('pg-promise')();20db.query('SELECT * FROM users')21 .then((result) => {22 console.log(result);23 })24 .catch((err) => {25 console.log(err);26 });27db.query('SELECT * FROM users')28 .then((result) => {29 console.log(result);30 })31 .catch((err) => {32 console.log(err);33 })34 .finally(() => {35 pgp.end();36 });37const pgp = require('pg-promise')();38db.query('SELECT * FROM users')39 .then((result) => {40 console.log(result);41 })42 .catch((err) => {43 console.log(err);44 });45db.query('SELECT * FROM users')46 .then((result) => {47 console.log(result);48 })49 .catch((err) => {50 console.log(err);51 })52 .finally(() => {53 pgp.end();54 });55const pgp = require('pg-promise')();56db.query('SELECT * FROM users
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!!