|
| 1 | +'use strict'; |
| 2 | +const chainRegistry = new WeakMap(); |
| 3 | + |
| 4 | +function startChain(name, call, defaults) { |
| 5 | + const fn = function () { |
| 6 | + call(Object.assign({}, defaults), Array.from(arguments)); |
| 7 | + }; |
| 8 | + Object.defineProperty(fn, 'name', {value: name}); |
| 9 | + chainRegistry.set(fn, {call, defaults, fullName: name}); |
| 10 | + return fn; |
| 11 | +} |
| 12 | + |
| 13 | +function extendChain(prev, name, flag) { |
| 14 | + if (!flag) { |
| 15 | + flag = name; |
| 16 | + } |
| 17 | + |
| 18 | + const fn = function () { |
| 19 | + callWithFlag(prev, flag, Array.from(arguments)); |
| 20 | + }; |
| 21 | + const fullName = `${chainRegistry.get(prev).fullName}.${name}`; |
| 22 | + Object.defineProperty(fn, 'name', {value: fullName}); |
| 23 | + prev[name] = fn; |
| 24 | + |
| 25 | + chainRegistry.set(fn, {flag, fullName, prev}); |
| 26 | + return fn; |
| 27 | +} |
| 28 | + |
| 29 | +function callWithFlag(prev, flag, args) { |
| 30 | + const combinedFlags = {[flag]: true}; |
| 31 | + do { |
| 32 | + const step = chainRegistry.get(prev); |
| 33 | + if (step.call) { |
| 34 | + step.call(Object.assign({}, step.defaults, combinedFlags), args); |
| 35 | + prev = null; |
| 36 | + } else { |
| 37 | + combinedFlags[step.flag] = true; |
| 38 | + prev = step.prev; |
| 39 | + } |
| 40 | + } while (prev); |
| 41 | +} |
| 42 | + |
| 43 | +function createHookChain(hook, isAfterHook) { |
| 44 | + // Hook chaining rules: |
| 45 | + // * `always` comes immediately after "after hooks" |
| 46 | + // * `skip` must come at the end |
| 47 | + // * no `only` |
| 48 | + // * no repeating |
| 49 | + extendChain(hook, 'cb', 'callback'); |
| 50 | + extendChain(hook, 'skip', 'skipped'); |
| 51 | + extendChain(hook.cb, 'skip', 'skipped'); |
| 52 | + if (isAfterHook) { |
| 53 | + extendChain(hook, 'always'); |
| 54 | + extendChain(hook.always, 'cb', 'callback'); |
| 55 | + extendChain(hook.always, 'skip', 'skipped'); |
| 56 | + extendChain(hook.always.cb, 'skip', 'skipped'); |
| 57 | + } |
| 58 | + return hook; |
| 59 | +} |
| 60 | + |
| 61 | +function createChain(fn, defaults) { |
| 62 | + // Test chaining rules: |
| 63 | + // * `serial` must come at the start |
| 64 | + // * `only` and `skip` must come at the end |
| 65 | + // * `failing` must come at the end, but can be followed by `only` and `skip` |
| 66 | + // * `only` and `skip` cannot be chained together |
| 67 | + // * no repeating |
| 68 | + const root = startChain('test', fn, Object.assign({}, defaults, {type: 'test'})); |
| 69 | + extendChain(root, 'cb', 'callback'); |
| 70 | + extendChain(root, 'failing'); |
| 71 | + extendChain(root, 'only', 'exclusive'); |
| 72 | + extendChain(root, 'serial'); |
| 73 | + extendChain(root, 'skip', 'skipped'); |
| 74 | + extendChain(root.cb, 'failing'); |
| 75 | + extendChain(root.cb, 'only', 'exclusive'); |
| 76 | + extendChain(root.cb, 'skip', 'skipped'); |
| 77 | + extendChain(root.cb.failing, 'only', 'exclusive'); |
| 78 | + extendChain(root.cb.failing, 'skip', 'skipped'); |
| 79 | + extendChain(root.failing, 'only', 'exclusive'); |
| 80 | + extendChain(root.failing, 'skip', 'skipped'); |
| 81 | + extendChain(root.serial, 'cb', 'callback'); |
| 82 | + extendChain(root.serial, 'failing'); |
| 83 | + extendChain(root.serial, 'only', 'exclusive'); |
| 84 | + extendChain(root.serial, 'skip', 'skipped'); |
| 85 | + extendChain(root.serial.cb, 'failing'); |
| 86 | + extendChain(root.serial.cb, 'only', 'exclusive'); |
| 87 | + extendChain(root.serial.cb, 'skip', 'skipped'); |
| 88 | + extendChain(root.serial.cb.failing, 'only', 'exclusive'); |
| 89 | + extendChain(root.serial.cb.failing, 'skip', 'skipped'); |
| 90 | + |
| 91 | + root.after = createHookChain(startChain('test.after', fn, Object.assign({}, defaults, {type: 'after'})), true); |
| 92 | + root.afterEach = createHookChain(startChain('test.afterEach', fn, Object.assign({}, defaults, {type: 'afterEach'})), true); |
| 93 | + root.before = createHookChain(startChain('test.before', fn, Object.assign({}, defaults, {type: 'before'})), false); |
| 94 | + root.beforeEach = createHookChain(startChain('test.beforeEach', fn, Object.assign({}, defaults, {type: 'beforeEach'})), false); |
| 95 | + |
| 96 | + root.serial.after = createHookChain(startChain('test.after', fn, Object.assign({}, defaults, {serial: true, type: 'after'})), true); |
| 97 | + root.serial.afterEach = createHookChain(startChain('test.afterEach', fn, Object.assign({}, defaults, {serial: true, type: 'afterEach'})), true); |
| 98 | + root.serial.before = createHookChain(startChain('test.before', fn, Object.assign({}, defaults, {serial: true, type: 'before'})), false); |
| 99 | + root.serial.beforeEach = createHookChain(startChain('test.beforeEach', fn, Object.assign({}, defaults, {serial: true, type: 'beforeEach'})), false); |
| 100 | + |
| 101 | + // "todo" tests cannot be chained. Allow todo tests to be flagged as needing |
| 102 | + // to be serial. |
| 103 | + root.todo = startChain('test.todo', fn, Object.assign({}, defaults, {type: 'test', todo: true})); |
| 104 | + root.serial.todo = startChain('test.serial.todo', fn, Object.assign({}, defaults, {serial: true, type: 'test', todo: true})); |
| 105 | + |
| 106 | + return root; |
| 107 | +} |
| 108 | +module.exports = createChain; |
0 commit comments