|  | 
| 2 | 2 | const EventEmitter = require('events'); | 
| 3 | 3 | const path = require('path'); | 
| 4 | 4 | const Bluebird = require('bluebird'); | 
| 5 |  | -const optionChain = require('option-chain'); | 
| 6 | 5 | const matcher = require('matcher'); | 
| 7 | 6 | const snapshotManager = require('./snapshot-manager'); | 
| 8 | 7 | const TestCollection = require('./test-collection'); | 
| 9 | 8 | const validateTest = require('./validate-test'); | 
| 10 | 9 | 
 | 
| 11 |  | -const chainableMethods = { | 
| 12 |  | -	defaults: { | 
| 13 |  | -		type: 'test', | 
| 14 |  | -		serial: false, | 
| 15 |  | -		exclusive: false, | 
| 16 |  | -		skipped: false, | 
| 17 |  | -		todo: false, | 
| 18 |  | -		failing: false, | 
| 19 |  | -		callback: false, | 
| 20 |  | -		always: false | 
| 21 |  | -	}, | 
| 22 |  | -	chainableMethods: { | 
| 23 |  | -		test: {}, | 
| 24 |  | -		serial: {serial: true}, | 
| 25 |  | -		before: {type: 'before'}, | 
| 26 |  | -		after: {type: 'after'}, | 
| 27 |  | -		skip: {skipped: true}, | 
| 28 |  | -		todo: {todo: true}, | 
| 29 |  | -		failing: {failing: true}, | 
| 30 |  | -		only: {exclusive: true}, | 
| 31 |  | -		beforeEach: {type: 'beforeEach'}, | 
| 32 |  | -		afterEach: {type: 'afterEach'}, | 
| 33 |  | -		cb: {callback: true}, | 
| 34 |  | -		always: {always: true} | 
|  | 10 | +const chainRegistry = new WeakMap(); | 
|  | 11 | + | 
|  | 12 | +function startChain(name, call, defaults) { | 
|  | 13 | +	const fn = function () { | 
|  | 14 | +		call(Object.assign({}, defaults), Array.from(arguments)); | 
|  | 15 | +	}; | 
|  | 16 | +	Object.defineProperty(fn, 'name', {value: name}); | 
|  | 17 | +	chainRegistry.set(fn, {call, defaults, fullName: name}); | 
|  | 18 | +	return fn; | 
|  | 19 | +} | 
|  | 20 | + | 
|  | 21 | +function extendChain(prev, name, flag) { | 
|  | 22 | +	if (!flag) { | 
|  | 23 | +		flag = name; | 
| 35 | 24 | 	} | 
| 36 |  | -}; | 
|  | 25 | + | 
|  | 26 | +	const fn = function () { | 
|  | 27 | +		callWithFlag(prev, flag, Array.from(arguments)); | 
|  | 28 | +	}; | 
|  | 29 | +	const fullName = `${chainRegistry.get(prev).fullName}.${name}`; | 
|  | 30 | +	Object.defineProperty(fn, 'name', {value: fullName}); | 
|  | 31 | +	prev[name] = fn; | 
|  | 32 | + | 
|  | 33 | +	chainRegistry.set(fn, {flag, fullName, prev}); | 
|  | 34 | +	return fn; | 
|  | 35 | +} | 
|  | 36 | + | 
|  | 37 | +function callWithFlag(prev, flag, args) { | 
|  | 38 | +	const combinedFlags = {[flag]: true}; | 
|  | 39 | +	do { | 
|  | 40 | +		const step = chainRegistry.get(prev); | 
|  | 41 | +		if (step.call) { | 
|  | 42 | +			step.call(Object.assign({}, step.defaults, combinedFlags), args); | 
|  | 43 | +			prev = null; | 
|  | 44 | +		} else { | 
|  | 45 | +			combinedFlags[step.flag] = true; | 
|  | 46 | +			prev = step.prev; | 
|  | 47 | +		} | 
|  | 48 | +	} while (prev); | 
|  | 49 | +} | 
|  | 50 | + | 
|  | 51 | +function createHookChain(hook, isAfterHook) { | 
|  | 52 | +	// Hook chaining rules: | 
|  | 53 | +	// * `always` comes immediately after "after hooks" | 
|  | 54 | +	// * `skip` must come at the end | 
|  | 55 | +	// * no `only` | 
|  | 56 | +	// * no repeating | 
|  | 57 | +	extendChain(hook, 'cb', 'callback'); | 
|  | 58 | +	extendChain(hook, 'skip', 'skipped'); | 
|  | 59 | +	extendChain(hook.cb, 'skip', 'skipped'); | 
|  | 60 | +	if (isAfterHook) { | 
|  | 61 | +		extendChain(hook, 'always'); | 
|  | 62 | +		extendChain(hook.always, 'cb', 'callback'); | 
|  | 63 | +		extendChain(hook.always, 'skip', 'skipped'); | 
|  | 64 | +		extendChain(hook.always.cb, 'skip', 'skipped'); | 
|  | 65 | +	} | 
|  | 66 | +	return hook; | 
|  | 67 | +} | 
|  | 68 | + | 
|  | 69 | +function createChain(fn, defaults) { | 
|  | 70 | +	// Test chaining rules: | 
|  | 71 | +	// * `serial` must come at the start | 
|  | 72 | +	// * `only` and `skip` must come at the end | 
|  | 73 | +	// * `failing` must come at the end, but can be followed by `only` and `skip` | 
|  | 74 | +	// * `only` and `skip` cannot be chained together | 
|  | 75 | +	// * no repeating | 
|  | 76 | +	const root = startChain('test', fn, Object.assign({}, defaults, {type: 'test'})); | 
|  | 77 | +	extendChain(root, 'cb', 'callback'); | 
|  | 78 | +	extendChain(root, 'failing'); | 
|  | 79 | +	extendChain(root, 'only', 'exclusive'); | 
|  | 80 | +	extendChain(root, 'serial'); | 
|  | 81 | +	extendChain(root, 'skip', 'skipped'); | 
|  | 82 | +	extendChain(root.cb, 'failing'); | 
|  | 83 | +	extendChain(root.cb, 'only', 'exclusive'); | 
|  | 84 | +	extendChain(root.cb, 'skip', 'skipped'); | 
|  | 85 | +	extendChain(root.cb.failing, 'only', 'exclusive'); | 
|  | 86 | +	extendChain(root.cb.failing, 'skip', 'skipped'); | 
|  | 87 | +	extendChain(root.failing, 'only', 'exclusive'); | 
|  | 88 | +	extendChain(root.failing, 'skip', 'skipped'); | 
|  | 89 | +	extendChain(root.serial, 'cb', 'callback'); | 
|  | 90 | +	extendChain(root.serial, 'failing'); | 
|  | 91 | +	extendChain(root.serial, 'only', 'exclusive'); | 
|  | 92 | +	extendChain(root.serial, 'skip', 'skipped'); | 
|  | 93 | +	extendChain(root.serial.cb, 'failing'); | 
|  | 94 | +	extendChain(root.serial.cb, 'only', 'exclusive'); | 
|  | 95 | +	extendChain(root.serial.cb, 'skip', 'skipped'); | 
|  | 96 | +	extendChain(root.serial.cb.failing, 'only', 'exclusive'); | 
|  | 97 | +	extendChain(root.serial.cb.failing, 'skip', 'skipped'); | 
|  | 98 | + | 
|  | 99 | +	root.after = createHookChain(startChain('test.after', fn, Object.assign({}, defaults, {type: 'after'})), true); | 
|  | 100 | +	root.afterEach = createHookChain(startChain('test.afterEach', fn, Object.assign({}, defaults, {type: 'afterEach'})), true); | 
|  | 101 | +	root.before = createHookChain(startChain('test.before', fn, Object.assign({}, defaults, {type: 'before'})), false); | 
|  | 102 | +	root.beforeEach = createHookChain(startChain('test.beforeEach', fn, Object.assign({}, defaults, {type: 'beforeEach'})), false); | 
|  | 103 | + | 
|  | 104 | +	// Todo tests cannot be chained. Allow todo tests to be flagged as needing to | 
|  | 105 | +	// be serial. | 
|  | 106 | +	root.todo = startChain('test.todo', fn, Object.assign({}, defaults, {type: 'test', todo: true})); | 
|  | 107 | +	root.serial.todo = startChain('test.serial.todo', fn, Object.assign({}, defaults, {serial: true, type: 'test', todo: true})); | 
|  | 108 | + | 
|  | 109 | +	return root; | 
|  | 110 | +} | 
| 37 | 111 | 
 | 
| 38 | 112 | function wrapFunction(fn, args) { | 
| 39 | 113 | 	return function (t) { | 
| @@ -63,7 +137,7 @@ class Runner extends EventEmitter { | 
| 63 | 137 | 			compareTestSnapshot: this.compareTestSnapshot.bind(this) | 
| 64 | 138 | 		}); | 
| 65 | 139 | 
 | 
| 66 |  | -		this.chain = optionChain(chainableMethods, (opts, args) => { | 
|  | 140 | +		this.chain = createChain((opts, args) => { | 
| 67 | 141 | 			let title; | 
| 68 | 142 | 			let fn; | 
| 69 | 143 | 			let macroArgIndex; | 
| @@ -100,6 +174,14 @@ class Runner extends EventEmitter { | 
| 100 | 174 | 			} else { | 
| 101 | 175 | 				this.addTest(title, opts, fn, args); | 
| 102 | 176 | 			} | 
|  | 177 | +		}, { | 
|  | 178 | +			serial: false, | 
|  | 179 | +			exclusive: false, | 
|  | 180 | +			skipped: false, | 
|  | 181 | +			todo: false, | 
|  | 182 | +			failing: false, | 
|  | 183 | +			callback: false, | 
|  | 184 | +			always: false | 
| 103 | 185 | 		}); | 
| 104 | 186 | 	} | 
| 105 | 187 | 
 | 
|  | 
0 commit comments