|
| 1 | +[**CTRF v0.0.16**](../README.md) |
| 2 | + |
| 3 | +*** |
| 4 | + |
| 5 | +[CTRF](../README.md) / CtrfRuntime |
| 6 | + |
| 7 | +# Class: CtrfRuntime |
| 8 | + |
| 9 | +Defined in: [src/runtime/runtime.ts:48](https://github.com/ctrf-io/ctrf-core-js/blob/main/src/runtime/runtime.ts#L48) |
| 10 | + |
| 11 | +AsyncLocalStorage-based test context manager. |
| 12 | + |
| 13 | +Provides isolated execution contexts for concurrent test scenarios using |
| 14 | +Node.js AsyncLocalStorage. Context data persists across async boundaries |
| 15 | +within the same logical test execution. |
| 16 | + |
| 17 | +## Example |
| 18 | + |
| 19 | +```typescript |
| 20 | +const runtime = new CtrfRuntime() |
| 21 | +runtime.startTestContext('test-name') |
| 22 | +runtime.addExtra('key', 'value') |
| 23 | +const context = runtime.endTestContext() |
| 24 | +``` |
| 25 | + |
| 26 | +## Constructors |
| 27 | + |
| 28 | +### Constructor |
| 29 | + |
| 30 | +> **new CtrfRuntime**(): `CtrfRuntime` |
| 31 | +
|
| 32 | +#### Returns |
| 33 | + |
| 34 | +`CtrfRuntime` |
| 35 | + |
| 36 | +## Methods |
| 37 | + |
| 38 | +### addExtra() |
| 39 | + |
| 40 | +> **addExtra**(`key`, `value`): `void` |
| 41 | +
|
| 42 | +Defined in: [src/runtime/runtime.ts:108](https://github.com/ctrf-io/ctrf-core-js/blob/main/src/runtime/runtime.ts#L108) |
| 43 | + |
| 44 | +Adds key-value pair to current context's extra object. |
| 45 | + |
| 46 | +Mutates the active context's extra property. Creates extra object if |
| 47 | +it doesn't exist. All values must be JSON-serializable. |
| 48 | + |
| 49 | +#### Parameters |
| 50 | + |
| 51 | +##### key |
| 52 | + |
| 53 | +`string` |
| 54 | + |
| 55 | +Property key for metadata |
| 56 | + |
| 57 | +##### value |
| 58 | + |
| 59 | +`unknown` |
| 60 | + |
| 61 | +JSON-serializable value |
| 62 | + |
| 63 | +#### Returns |
| 64 | + |
| 65 | +`void` |
| 66 | + |
| 67 | +#### Throws |
| 68 | + |
| 69 | +When no active AsyncLocalStorage context exists |
| 70 | + |
| 71 | +#### Example |
| 72 | + |
| 73 | +```typescript |
| 74 | +runtime.addExtra('priority', 'high') |
| 75 | +runtime.addExtra('tags', ['smoke', 'regression']) |
| 76 | +runtime.addExtra('metadata', { browser: 'chrome', viewport: '1920x1080' }) |
| 77 | +``` |
| 78 | + |
| 79 | +*** |
| 80 | + |
| 81 | +### endTestContext() |
| 82 | + |
| 83 | +> **endTestContext**(): `undefined` \| [`TestContext`](../interfaces/TestContext.md) |
| 84 | +
|
| 85 | +Defined in: [src/runtime/runtime.ts:139](https://github.com/ctrf-io/ctrf-core-js/blob/main/src/runtime/runtime.ts#L139) |
| 86 | + |
| 87 | +Returns shallow copy of current context and exits AsyncLocalStorage scope. |
| 88 | + |
| 89 | +Context data is intended for enrichment only. Reporter implementations |
| 90 | +should merge only the `extra` field with their test data. |
| 91 | + |
| 92 | +#### Returns |
| 93 | + |
| 94 | +`undefined` \| [`TestContext`](../interfaces/TestContext.md) |
| 95 | + |
| 96 | +Shallow copy of TestContext or undefined if no active context |
| 97 | + |
| 98 | +#### Example |
| 99 | + |
| 100 | +```typescript |
| 101 | +const context = runtime.endTestContext() |
| 102 | +if (context) { |
| 103 | + const enriched = { ...reporterData, extra: { ...context.extra, ...reporterData.extra } } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +*** |
| 108 | + |
| 109 | +### getCurrentContext() |
| 110 | + |
| 111 | +> **getCurrentContext**(): `undefined` \| [`TestContext`](../interfaces/TestContext.md) |
| 112 | +
|
| 113 | +Defined in: [src/runtime/runtime.ts:162](https://github.com/ctrf-io/ctrf-core-js/blob/main/src/runtime/runtime.ts#L162) |
| 114 | + |
| 115 | +Returns current AsyncLocalStorage context without modifying it. |
| 116 | + |
| 117 | +#### Returns |
| 118 | + |
| 119 | +`undefined` \| [`TestContext`](../interfaces/TestContext.md) |
| 120 | + |
| 121 | +Current TestContext or undefined if no active context |
| 122 | + |
| 123 | +#### Example |
| 124 | + |
| 125 | +```typescript |
| 126 | +const current = runtime.getCurrentContext() |
| 127 | +if (current?.extra?.skipCleanup) { |
| 128 | + // conditional logic based on context |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +*** |
| 133 | + |
| 134 | +### hasActiveContext() |
| 135 | + |
| 136 | +> **hasActiveContext**(): `boolean` |
| 137 | +
|
| 138 | +Defined in: [src/runtime/runtime.ts:178](https://github.com/ctrf-io/ctrf-core-js/blob/main/src/runtime/runtime.ts#L178) |
| 139 | + |
| 140 | +Checks for active AsyncLocalStorage context existence. |
| 141 | + |
| 142 | +#### Returns |
| 143 | + |
| 144 | +`boolean` |
| 145 | + |
| 146 | +true if context exists, false otherwise |
| 147 | + |
| 148 | +#### Example |
| 149 | + |
| 150 | +```typescript |
| 151 | +if (runtime.hasActiveContext()) { |
| 152 | + runtime.addExtra('key', 'value') |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +*** |
| 157 | + |
| 158 | +### startTestContext() |
| 159 | + |
| 160 | +> **startTestContext**(`name`, `options?`): `void` |
| 161 | +
|
| 162 | +Defined in: [src/runtime/runtime.ts:71](https://github.com/ctrf-io/ctrf-core-js/blob/main/src/runtime/runtime.ts#L71) |
| 163 | + |
| 164 | +Creates new AsyncLocalStorage context and enters it synchronously. |
| 165 | + |
| 166 | +#### Parameters |
| 167 | + |
| 168 | +##### name |
| 169 | + |
| 170 | +`string` |
| 171 | + |
| 172 | +Test identifier string |
| 173 | + |
| 174 | +##### options? |
| 175 | + |
| 176 | +Optional context metadata |
| 177 | + |
| 178 | +###### filePath? |
| 179 | + |
| 180 | +`string` |
| 181 | + |
| 182 | +Absolute path to test file |
| 183 | + |
| 184 | +###### id? |
| 185 | + |
| 186 | +`string` |
| 187 | + |
| 188 | +Unique test identifier |
| 189 | + |
| 190 | +###### suite? |
| 191 | + |
| 192 | +`string`[] |
| 193 | + |
| 194 | +Suite hierarchy array |
| 195 | + |
| 196 | +#### Returns |
| 197 | + |
| 198 | +`void` |
| 199 | + |
| 200 | +#### Throws |
| 201 | + |
| 202 | +None - always succeeds |
| 203 | + |
| 204 | +#### Example |
| 205 | + |
| 206 | +```typescript |
| 207 | +runtime.startTestContext('user login test', { |
| 208 | + suite: ['auth', 'integration'], |
| 209 | + filePath: '/tests/auth.spec.ts', |
| 210 | + id: 'declarative-uuid' |
| 211 | +}) |
| 212 | +``` |
0 commit comments