|
7 | 7 | RegularExpression, |
8 | 8 | ParameterTypeRegistry |
9 | 9 | } = require("cucumber-expressions"); |
| 10 | +const { shouldProceedCurrentStep } = require("./tagsHelper"); |
10 | 11 |
|
11 | 12 | class StepDefinitionRegistry { |
12 | 13 | constructor() { |
@@ -38,7 +39,25 @@ class StepDefinitionRegistry { |
38 | 39 | } |
39 | 40 | } |
40 | 41 |
|
| 42 | +class HookRegistry { |
| 43 | + constructor() { |
| 44 | + this.definitions = []; |
| 45 | + this.runtime = {}; |
| 46 | + |
| 47 | + this.runtime = (tags, implementation) => { |
| 48 | + this.definitions.push({ implementation, tags }); |
| 49 | + }; |
| 50 | + |
| 51 | + this.resolve = scenarioTags => |
| 52 | + this.definitions.filter(({ tags }) => |
| 53 | + shouldProceedCurrentStep(scenarioTags, tags) |
| 54 | + ); |
| 55 | + } |
| 56 | +} |
| 57 | + |
41 | 58 | const stepDefinitionRegistry = new StepDefinitionRegistry(); |
| 59 | +const beforeHookRegistry = new HookRegistry(); |
| 60 | +const afterHookRegistry = new HookRegistry(); |
42 | 61 |
|
43 | 62 | function resolveStepDefinition(step) { |
44 | 63 | const stepDefinition = stepDefinitionRegistry.resolve( |
@@ -104,7 +123,47 @@ function resolveStepArgument(argument, exampleRowData, replaceParameterTags) { |
104 | 123 | return argument; |
105 | 124 | } |
106 | 125 |
|
| 126 | +function resolveAndRunHooks(hookRegistry, scenarioTags) { |
| 127 | + return window.Cypress.Promise.each( |
| 128 | + hookRegistry.resolve(scenarioTags), |
| 129 | + ({ implementation }) => implementation.call(this) |
| 130 | + ); |
| 131 | +} |
| 132 | + |
| 133 | +function parseHookArgs(args) { |
| 134 | + if (args.length === 2) { |
| 135 | + if (typeof args[0] !== "object" || typeof args[0].tags !== "string") { |
| 136 | + throw new Error( |
| 137 | + "Hook definitions with two arguments should have an object containing tags (string) as the first argument." |
| 138 | + ); |
| 139 | + } |
| 140 | + if (typeof args[1] !== "function") { |
| 141 | + throw new Error( |
| 142 | + "Hook definitions with two arguments must have a function as the second argument." |
| 143 | + ); |
| 144 | + } |
| 145 | + return { |
| 146 | + tags: args[0].tags, |
| 147 | + implementation: args[1] |
| 148 | + }; |
| 149 | + } |
| 150 | + if (typeof args[0] !== "function") { |
| 151 | + throw new Error( |
| 152 | + "Hook definitions with one argument must have a function as the first argument." |
| 153 | + ); |
| 154 | + } |
| 155 | + return { |
| 156 | + implementation: args[0] |
| 157 | + }; |
| 158 | +} |
| 159 | + |
107 | 160 | module.exports = { |
| 161 | + resolveAndRunBeforeHooks(scenarioTags) { |
| 162 | + return resolveAndRunHooks(beforeHookRegistry, scenarioTags); |
| 163 | + }, |
| 164 | + resolveAndRunAfterHooks(scenarioTags) { |
| 165 | + return resolveAndRunHooks(afterHookRegistry, scenarioTags); |
| 166 | + }, |
108 | 167 | // eslint-disable-next-line func-names |
109 | 168 | resolveAndRunStepDefinition(step, replaceParameterTags, exampleRowData) { |
110 | 169 | const { expression, implementation } = resolveStepDefinition(step); |
@@ -138,6 +197,14 @@ module.exports = { |
138 | 197 | but: (expression, implementation) => { |
139 | 198 | stepDefinitionRegistry.runtime(expression, implementation); |
140 | 199 | }, |
| 200 | + before: (...args) => { |
| 201 | + const { tags, implementation } = parseHookArgs(args); |
| 202 | + beforeHookRegistry.runtime(tags, implementation); |
| 203 | + }, |
| 204 | + after: (...args) => { |
| 205 | + const { tags, implementation } = parseHookArgs(args); |
| 206 | + afterHookRegistry.runtime(tags, implementation); |
| 207 | + }, |
141 | 208 | defineStep: (expression, implementation) => { |
142 | 209 | stepDefinitionRegistry.runtime(expression, implementation); |
143 | 210 | }, |
|
0 commit comments