(Zone: ZoneType)
| 46 | type TEST_MODIFIER_NAME = (typeof DIRECT_MODIFIER_NAMES | typeof CURRIED_MODIFIER_NAMES)[number]; |
| 47 | |
| 48 | export function patchVitest(Zone: ZoneType): void { |
| 49 | Zone.__load_patch('vitest', (context: any, Zone: TestingZoneType) => { |
| 50 | // Vitest global variable set by the Vitest runner during test execution |
| 51 | const vitestGlobal = context['vitest'] as {['__zone_patch__']?: boolean} | undefined; |
| 52 | |
| 53 | // Skip patching if vitest is not present or has already been patched |
| 54 | if (typeof vitestGlobal === 'undefined' || vitestGlobal['__zone_patch__']) { |
| 55 | return; |
| 56 | } |
| 57 | vitestGlobal['__zone_patch__'] = true; |
| 58 | |
| 59 | // Ensure other testing related Zone.js patches have been applied |
| 60 | if (!Zone.ProxyZoneSpec) { |
| 61 | throw new Error('Missing ProxyZoneSpec'); |
| 62 | } |
| 63 | if (!Zone.SyncTestZoneSpec) { |
| 64 | throw new Error('Missing SyncTestZoneSpec'); |
| 65 | } |
| 66 | |
| 67 | // Setup testing related Zone instances |
| 68 | const rootZone = Zone.current; |
| 69 | const syncZone = rootZone.fork(new Zone.SyncTestZoneSpec('vitest.describe')); |
| 70 | const proxyZone = rootZone.fork(new Zone.ProxyZoneSpec()); |
| 71 | |
| 72 | /** |
| 73 | * Gets a function wrapping the body of a vitest `describe` block to execute in a |
| 74 | * synchronous-only zone. |
| 75 | */ |
| 76 | function wrapDescribeInZone(describeBody: Function): Function { |
| 77 | // `describe` might be called without a body (e.g. `describe.todo`) |
| 78 | if (typeof describeBody !== 'function') { |
| 79 | return describeBody; |
| 80 | } |
| 81 | return function (this: unknown, ...args: unknown[]) { |
| 82 | return syncZone.run(describeBody, this, args); |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Gets a function wrapping the body of a vitest `it/beforeEach/afterEach` block to |
| 88 | * execute in a ProxyZone zone. |
| 89 | * This will run in the `proxyZone`. |
| 90 | */ |
| 91 | function wrapTestInZone(testBody: Function): Function { |
| 92 | if (typeof testBody !== 'function') { |
| 93 | return testBody; |
| 94 | } |
| 95 | const wrappedFunc = function () { |
| 96 | return proxyZone.run(testBody, null, arguments as any); |
| 97 | }; |
| 98 | // Update the length of wrappedFunc to be the same as the length of the testBody |
| 99 | // So vitest core can handle whether the test function has `done()` or not correctly |
| 100 | Object.defineProperty(wrappedFunc, 'length', { |
| 101 | configurable: true, |
| 102 | writable: true, |
| 103 | enumerable: false, |
| 104 | }); |
| 105 | wrappedFunc.length = testBody.length; |
no test coverage detected