123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- 'use strict';
- require('../lib/chalk').set();
- require('../lib/worker/options').set({});
- const test = require('tap').test;
- const Runner = require('../lib/runner');
- const promiseEnd = (runner, next) => {
- return new Promise(resolve => {
- resolve(runner.once('finish'));
- next(runner);
- }).then(() => runner);
- };
- test('before', t => {
- t.plan(1);
- const arr = [];
- return promiseEnd(new Runner(), runner => {
- runner.chain.before(() => {
- arr.push('a');
- });
- runner.chain('test', a => {
- a.pass();
- arr.push('b');
- });
- }).then(() => {
- t.strictDeepEqual(arr, ['a', 'b']);
- });
- });
- test('after', t => {
- t.plan(2);
- const arr = [];
- return promiseEnd(new Runner(), runner => {
- runner.on('stateChange', evt => {
- if (evt.type === 'test-passed') {
- t.pass();
- }
- });
- runner.chain.after(() => {
- arr.push('b');
- });
- runner.chain('test', a => {
- a.pass();
- arr.push('a');
- });
- }).then(() => {
- t.strictDeepEqual(arr, ['a', 'b']);
- });
- });
- test('after not run if test failed', t => {
- t.plan(2);
- const arr = [];
- return promiseEnd(new Runner(), runner => {
- runner.on('stateChange', evt => {
- if (evt.type === 'test-failed') {
- t.pass();
- }
- });
- runner.chain.after(() => {
- arr.push('a');
- });
- runner.chain('test', () => {
- throw new Error('something went wrong');
- });
- }).then(() => {
- t.strictDeepEqual(arr, []);
- });
- });
- test('after.always run even if test failed', t => {
- t.plan(2);
- const arr = [];
- return promiseEnd(new Runner(), runner => {
- runner.on('stateChange', evt => {
- if (evt.type === 'test-failed') {
- t.pass();
- }
- });
- runner.chain.after.always(() => {
- arr.push('a');
- });
- runner.chain('test', () => {
- throw new Error('something went wrong');
- });
- }).then(() => {
- t.strictDeepEqual(arr, ['a']);
- });
- });
- test('after.always run even if before failed', t => {
- t.plan(1);
- const arr = [];
- return promiseEnd(new Runner(), runner => {
- runner.chain.before(() => {
- throw new Error('something went wrong');
- });
- runner.chain('test', a => a.pass());
- runner.chain.after.always(() => {
- arr.push('a');
- });
- }).then(() => {
- t.strictDeepEqual(arr, ['a']);
- });
- });
- test('stop if before hooks failed', t => {
- t.plan(1);
- const arr = [];
- return promiseEnd(new Runner(), runner => {
- runner.chain.before(() => {
- arr.push('a');
- });
- runner.chain.before(() => {
- throw new Error('something went wrong');
- });
- runner.chain('test', a => {
- a.pass();
- arr.push('b');
- a.end();
- });
- }).then(() => {
- t.strictDeepEqual(arr, ['a']);
- });
- });
- test('before each with concurrent tests', t => {
- t.plan(1);
- const arr = [[], []];
- return promiseEnd(new Runner(), runner => {
- let i = 0;
- let k = 0;
- runner.chain.beforeEach(() => {
- arr[i++].push('a');
- });
- runner.chain.beforeEach(() => {
- arr[k++].push('b');
- });
- runner.chain('c', a => {
- a.pass();
- arr[0].push('c');
- });
- runner.chain('d', a => {
- a.pass();
- arr[1].push('d');
- });
- }).then(() => {
- t.strictDeepEqual(arr, [['a', 'b', 'c'], ['a', 'b', 'd']]);
- });
- });
- test('before each with serial tests', t => {
- t.plan(1);
- const arr = [];
- return promiseEnd(new Runner(), runner => {
- runner.chain.beforeEach(() => {
- arr.push('a');
- });
- runner.chain.beforeEach(() => {
- arr.push('b');
- });
- runner.chain.serial('c', a => {
- a.pass();
- arr.push('c');
- });
- runner.chain.serial('d', a => {
- a.pass();
- arr.push('d');
- });
- }).then(() => {
- t.strictDeepEqual(arr, ['a', 'b', 'c', 'a', 'b', 'd']);
- });
- });
- test('fail if beforeEach hook fails', t => {
- t.plan(2);
- const arr = [];
- return promiseEnd(new Runner(), runner => {
- runner.on('stateChange', evt => {
- if (evt.type === 'hook-failed') {
- t.pass();
- }
- });
- runner.chain.beforeEach(a => {
- arr.push('a');
- a.fail();
- });
- runner.chain('test', a => {
- arr.push('b');
- a.pass();
- });
- }).then(() => {
- t.strictDeepEqual(arr, ['a']);
- });
- });
- test('after each with concurrent tests', t => {
- t.plan(1);
- const arr = [[], []];
- return promiseEnd(new Runner(), runner => {
- let i = 0;
- let k = 0;
- runner.chain.afterEach(() => {
- arr[i++].push('a');
- });
- runner.chain.afterEach(() => {
- arr[k++].push('b');
- });
- runner.chain('c', a => {
- a.pass();
- arr[0].push('c');
- });
- runner.chain('d', a => {
- a.pass();
- arr[1].push('d');
- });
- }).then(() => {
- t.strictDeepEqual(arr, [['c', 'a', 'b'], ['d', 'a', 'b']]);
- });
- });
- test('after each with serial tests', t => {
- t.plan(1);
- const arr = [];
- return promiseEnd(new Runner(), runner => {
- runner.chain.afterEach(() => {
- arr.push('a');
- });
- runner.chain.afterEach(() => {
- arr.push('b');
- });
- runner.chain.serial('c', a => {
- a.pass();
- arr.push('c');
- });
- runner.chain.serial('d', a => {
- a.pass();
- arr.push('d');
- });
- }).then(() => {
- t.strictDeepEqual(arr, ['c', 'a', 'b', 'd', 'a', 'b']);
- });
- });
- test('afterEach not run if concurrent tests failed', t => {
- t.plan(1);
- const arr = [];
- return promiseEnd(new Runner(), runner => {
- runner.chain.afterEach(() => {
- arr.push('a');
- });
- runner.chain('test', () => {
- throw new Error('something went wrong');
- });
- }).then(() => {
- t.strictDeepEqual(arr, []);
- });
- });
- test('afterEach not run if serial tests failed', t => {
- t.plan(1);
- const arr = [];
- return promiseEnd(new Runner(), runner => {
- runner.chain.afterEach(() => {
- arr.push('a');
- });
- runner.chain.serial('test', () => {
- throw new Error('something went wrong');
- });
- }).then(() => {
- t.strictDeepEqual(arr, []);
- });
- });
- test('afterEach.always run even if concurrent tests failed', t => {
- t.plan(1);
- const arr = [];
- return promiseEnd(new Runner(), runner => {
- runner.chain.afterEach.always(() => {
- arr.push('a');
- });
- runner.chain('test', () => {
- throw new Error('something went wrong');
- });
- }).then(() => {
- t.strictDeepEqual(arr, ['a']);
- });
- });
- test('afterEach.always run even if serial tests failed', t => {
- t.plan(1);
- const arr = [];
- return promiseEnd(new Runner(), runner => {
- runner.chain.afterEach.always(() => {
- arr.push('a');
- });
- runner.chain.serial('test', () => {
- throw new Error('something went wrong');
- });
- }).then(() => {
- t.strictDeepEqual(arr, ['a']);
- });
- });
- test('afterEach.always run even if beforeEach failed', t => {
- t.plan(1);
- const arr = [];
- return promiseEnd(new Runner(), runner => {
- runner.chain.beforeEach(() => {
- throw new Error('something went wrong');
- });
- runner.chain('test', a => {
- a.pass();
- arr.push('a');
- });
- runner.chain.afterEach.always(() => {
- arr.push('b');
- });
- }).then(() => {
- t.strictDeepEqual(arr, ['b']);
- });
- });
- test('ensure hooks run only around tests', t => {
- t.plan(1);
- const arr = [];
- return promiseEnd(new Runner(), runner => {
- runner.chain.beforeEach(() => {
- arr.push('beforeEach');
- });
- runner.chain.before(() => {
- arr.push('before');
- });
- runner.chain.afterEach(() => {
- arr.push('afterEach');
- });
- runner.chain.after(() => {
- arr.push('after');
- });
- runner.chain('test', a => {
- a.pass();
- arr.push('test');
- });
- }).then(() => {
- t.strictDeepEqual(arr, ['before', 'beforeEach', 'test', 'afterEach', 'after']);
- });
- });
- test('shared context', t => {
- return promiseEnd(new Runner(), runner => {
- runner.on('stateChange', evt => {
- if (evt.type === 'hook-failed' || evt.type === 'test-failed') {
- t.fail();
- }
- });
- runner.chain.before(a => {
- a.deepEqual(a.context, {});
- a.context.arr = ['a'];
- a.context.prop = 'before';
- });
- runner.chain.after(a => {
- a.deepEqual(a.context.arr, ['a', 'b', 'c', 'd']);
- a.is(a.context.prop, 'before');
- });
- runner.chain.beforeEach(a => {
- a.deepEqual(a.context.arr, ['a']);
- a.context.arr.push('b');
- a.is(a.context.prop, 'before');
- a.context.prop = 'beforeEach';
- });
- runner.chain('test', a => {
- a.pass();
- a.deepEqual(a.context.arr, ['a', 'b']);
- a.context.arr.push('c');
- a.is(a.context.prop, 'beforeEach');
- a.context.prop = 'test';
- });
- runner.chain.afterEach(a => {
- a.deepEqual(a.context.arr, ['a', 'b', 'c']);
- a.context.arr.push('d');
- a.is(a.context.prop, 'test');
- a.context.prop = 'afterEach';
- });
- });
- });
- test('shared context of any type', t => {
- return promiseEnd(new Runner(), runner => {
- runner.on('stateChange', evt => {
- if (evt.type === 'hook-failed' || evt.type === 'test-failed') {
- t.fail();
- }
- });
- runner.chain.beforeEach(a => {
- a.context = 'foo';
- });
- runner.chain('test', a => {
- a.pass();
- a.is(a.context, 'foo');
- });
- });
- });
|