|
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; |
| 24 | + } |
| 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'); |
35 | 65 | }
|
36 |
| -}; |
| 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 | + // No additional methods todo tests. |
| 100 | + root.todo = startChain('test.todo', fn, Object.assign({}, defaults, {type: 'test', todo: true})); |
| 101 | + |
| 102 | + root.after = createHookChain(startChain('test.after', fn, Object.assign({}, defaults, {type: 'after'})), true); |
| 103 | + root.afterEach = createHookChain(startChain('test.afterEach', fn, Object.assign({}, defaults, {type: 'afterEach'})), true); |
| 104 | + root.before = createHookChain(startChain('test.before', fn, Object.assign({}, defaults, {type: 'before'})), false); |
| 105 | + root.beforeEach = createHookChain(startChain('test.beforeEach', fn, Object.assign({}, defaults, {type: 'beforeEach'})), false); |
| 106 | + |
| 107 | + // Add to root.serial to support `import {serial} from 'ava'` use cases |
| 108 | + root.serial.after = createHookChain(startChain('test.serial.after', fn, Object.assign({}, defaults, {serial: true, type: 'after'})), true); |
| 109 | + root.serial.afterEach = createHookChain(startChain('test.serial.afterEach', fn, Object.assign({}, defaults, {serial: true, type: 'afterEach'})), true); |
| 110 | + root.serial.before = createHookChain(startChain('test.serial.before', fn, Object.assign({}, defaults, {serial: true, type: 'before'})), false); |
| 111 | + root.serial.beforeEach = createHookChain(startChain('test.serial.beforeEach', fn, Object.assign({}, defaults, {serial: true, type: 'beforeEach'})), false); |
| 112 | + root.serial.todo = startChain('test.serial.todo', fn, Object.assign({}, defaults, {type: 'test', todo: true})); |
| 113 | + |
| 114 | + return root; |
| 115 | +} |
37 | 116 |
|
38 | 117 | function wrapFunction(fn, args) {
|
39 | 118 | return function (t) {
|
@@ -63,7 +142,7 @@ class Runner extends EventEmitter {
|
63 | 142 | compareTestSnapshot: this.compareTestSnapshot.bind(this)
|
64 | 143 | });
|
65 | 144 |
|
66 |
| - this.chain = optionChain(chainableMethods, (opts, args) => { |
| 145 | + this.chain = createChain((opts, args) => { |
67 | 146 | let title;
|
68 | 147 | let fn;
|
69 | 148 | let macroArgIndex;
|
@@ -100,6 +179,14 @@ class Runner extends EventEmitter {
|
100 | 179 | } else {
|
101 | 180 | this.addTest(title, opts, fn, args);
|
102 | 181 | }
|
| 182 | + }, { |
| 183 | + serial: false, |
| 184 | + exclusive: false, |
| 185 | + skipped: false, |
| 186 | + todo: false, |
| 187 | + failing: false, |
| 188 | + callback: false, |
| 189 | + always: false |
103 | 190 | });
|
104 | 191 | }
|
105 | 192 |
|
|
0 commit comments