Skip to content

Commit b77acac

Browse files
committed
feat: add initial runtime api
1 parent f9d51eb commit b77acac

22 files changed

+1124
-48
lines changed

docs/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,21 @@
5858
- [setTestId](functions/setTestId.md)
5959
- [setTestIdsForReport](functions/setTestIdsForReport.md)
6060

61+
## Runtime
62+
63+
- [mergeTestData](functions/mergeTestData.md)
64+
65+
## Classes
66+
67+
- [CtrfRuntime](classes/CtrfRuntime.md)
68+
6169
## Enumerations
6270

6371
- [SortOrder](enumerations/SortOrder.md)
6472

6573
## Interfaces
6674

75+
- [TestContext](interfaces/TestContext.md)
6776
- [TestTree](interfaces/TestTree.md)
6877
- [TreeNode](interfaces/TreeNode.md)
6978
- [TreeOptions](interfaces/TreeOptions.md)
@@ -75,4 +84,7 @@
7584

7685
## Variables
7786

87+
- [ctrf](variables/ctrf.md)
7888
- [CTRF\_NAMESPACE](variables/CTRF_NAMESPACE.md)
89+
- [ctrfRuntime](variables/ctrfRuntime.md)
90+
- [runtime](variables/runtime.md)

docs/classes/CtrfRuntime.md

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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+
```

docs/functions/findTestById.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
> **findTestById**(`report`, `testId`): `undefined` \| [`Test`](../interfaces/Test.md)
1010
11-
Defined in: src/methods/test-id.ts:84
11+
Defined in: [src/methods/test-id.ts:91](https://github.com/ctrf-io/ctrf-core-js/blob/main/src/methods/test-id.ts#L91)
1212

1313
Finds a test by its ID in a report
1414

docs/functions/generateTestIdFromProperties.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
> **generateTestIdFromProperties**(`name`, `suite?`, `filePath?`): `string`
1010
11-
Defined in: src/methods/test-id.ts:95
11+
Defined in: [src/methods/test-id.ts:102](https://github.com/ctrf-io/ctrf-core-js/blob/main/src/methods/test-id.ts#L102)
1212

1313
Generates a new test ID based on test properties (exposed utility)
1414

docs/functions/getTestId.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
> **getTestId**(`test`): `string`
1010
11-
Defined in: src/methods/test-id.ts:61
11+
Defined in: [src/methods/test-id.ts:68](https://github.com/ctrf-io/ctrf-core-js/blob/main/src/methods/test-id.ts#L68)
1212

1313
Gets the test ID from a test object, generating one if it doesn't exist
1414

docs/functions/mergeTestData.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
[**CTRF v0.0.16**](../README.md)
2+
3+
***
4+
5+
[CTRF](../README.md) / mergeTestData
6+
7+
# Function: mergeTestData()
8+
9+
> **mergeTestData**\<`T`\>(`reporterTest`, `runtimeContext`): `T`
10+
11+
Defined in: [src/runtime/runtime.ts:213](https://github.com/ctrf-io/ctrf-core-js/blob/main/src/runtime/runtime.ts#L213)
12+
13+
Merges reporter test data with runtime context metadata.
14+
15+
Reporter data takes precedence. Runtime context enriches only the `extra`
16+
field. In case of key conflicts in `extra`, reporter values win.
17+
18+
## Type Parameters
19+
20+
### T
21+
22+
`T` *extends* `Record`\<`string`, `any`\>
23+
24+
## Parameters
25+
26+
### reporterTest
27+
28+
`T`
29+
30+
Test data from framework reporter (source of truth)
31+
32+
### runtimeContext
33+
34+
Context from ctrfRuntime.endTestContext()
35+
36+
`undefined` | [`TestContext`](../interfaces/TestContext.md)
37+
38+
## Returns
39+
40+
`T`
41+
42+
Merged test object with enriched extra metadata
43+
44+
## Example
45+
46+
```typescript
47+
const merged = mergeTestData(
48+
{ name: 'test', status: 'passed', extra: { framework: 'data' } },
49+
{ name: 'test', extra: { runtime: 'metadata', framework: 'ignored' } }
50+
)
51+
// Result: { name: 'test', status: 'passed', extra: { runtime: 'metadata', framework: 'data' } }
52+
```

docs/functions/setTestId.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
> **setTestId**(`test`): [`Test`](../interfaces/Test.md)
1010
11-
Defined in: src/methods/test-id.ts:49
11+
Defined in: [src/methods/test-id.ts:56](https://github.com/ctrf-io/ctrf-core-js/blob/main/src/methods/test-id.ts#L56)
1212

1313
Sets a test ID for a test object based on its properties
1414

docs/functions/setTestIdsForReport.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
> **setTestIdsForReport**(`report`): [`Report`](../interfaces/Report.md)
1010
11-
Defined in: src/methods/test-id.ts:73
11+
Defined in: [src/methods/test-id.ts:80](https://github.com/ctrf-io/ctrf-core-js/blob/main/src/methods/test-id.ts#L80)
1212

1313
Sets test IDs for all tests in a report
1414

0 commit comments

Comments
 (0)