Patch functions with modifiers (i.e. `describe`/`it`).
(methodName: string, wrapFn: (fn: Function) => Function)
| 108 | |
| 109 | /** Patch functions with modifiers (i.e. `describe`/`it`). */ |
| 110 | function patchFnWithModifiers(methodName: string, wrapFn: (fn: Function) => Function) { |
| 111 | const originalVitestFn: Function & Record<TEST_MODIFIER_NAME, Function> = context[methodName]; |
| 112 | // Skip if already patched |
| 113 | if (context[Zone.__symbol__(methodName)]) { |
| 114 | return; |
| 115 | } |
| 116 | context[Zone.__symbol__(methodName)] = originalVitestFn; |
| 117 | |
| 118 | // Patching the main function |
| 119 | context[methodName] = function (this: unknown, ...args: [unknown, Function, ...unknown[]]) { |
| 120 | args[1] = wrapFn(args[1]); |
| 121 | return originalVitestFn.apply(this, args); |
| 122 | }; |
| 123 | |
| 124 | // Patching direct modifier calls |
| 125 | for (const modifierName of DIRECT_MODIFIER_NAMES) { |
| 126 | context[methodName][modifierName] = function ( |
| 127 | this: unknown, |
| 128 | ...args: [unknown, Function, ...unknown[]] |
| 129 | ) { |
| 130 | args[1] = wrapFn(args[1]); |
| 131 | return originalVitestFn[modifierName].apply(this, args); |
| 132 | }; |
| 133 | } |
| 134 | |
| 135 | // Patching curried modifier calls |
| 136 | for (const modifierName of CURRIED_MODIFIER_NAMES) { |
| 137 | context[methodName][modifierName] = function (this: unknown, ...modifierArgs: unknown[]) { |
| 138 | // Since we are patching a curried function, we need |
| 139 | // to pass the original context first (`originalVitestFn`). |
| 140 | // Else, the chaining won't be possible (will get an error). |
| 141 | const originalFn = originalVitestFn[modifierName].apply(originalVitestFn, modifierArgs); |
| 142 | |
| 143 | return function (this: unknown, ...args: [unknown, Function, ...unknown[]]) { |
| 144 | args[1] = wrapFn(args[1]); |
| 145 | return originalFn.apply(this, args); |
| 146 | }; |
| 147 | }; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | ['suite', 'describe'].forEach((methodName) => |
| 152 | patchFnWithModifiers(methodName, wrapDescribeInZone), |
no test coverage detected