@swedevtools/livedoc-vitest 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 dotnetprofessional
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,95 @@
1
+ <div align="center">
2
+
3
+ # @swedevtools/livedoc-vitest
4
+
5
+ ### Turn your tests into living documentation
6
+
7
+ [![npm version](https://img.shields.io/npm/v/@swedevtools/livedoc-vitest.svg)](https://www.npmjs.com/package/@swedevtools/livedoc-vitest)
8
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
9
+
10
+ **Write tests in Gherkin. Get documentation that never goes stale.**
11
+
12
+ 📖 **[Full Documentation →](https://livedoc.swedevtools.com/vitest/learn/getting-started)**
13
+
14
+ </div>
15
+
16
+ ---
17
+
18
+ ## What is LiveDoc?
19
+
20
+ LiveDoc brings Behavior-Driven Development to Vitest with full Gherkin syntax — **Feature / Scenario / Given / When / Then**, **Specification / Rule**, **Scenario Outlines**, **Tags & Filtering**, and **beautiful reports**.
21
+
22
+ ## Quick Start
23
+
24
+ ### Install
25
+
26
+ ```bash
27
+ npm install --save-dev vitest @swedevtools/livedoc-vitest
28
+ ```
29
+
30
+ ### Create a spec
31
+
32
+ ```ts
33
+ // tests/Calculator.Spec.ts
34
+ import { feature, scenario, given, when, Then as then, and } from '@swedevtools/livedoc-vitest';
35
+
36
+ feature("Calculator", () => {
37
+ scenario("Adding two numbers", () => {
38
+ let result = 0;
39
+
40
+ given("I have entered '50' into the calculator", (ctx) => {
41
+ result = ctx.step.values[0];
42
+ });
43
+
44
+ and("I have entered '70' into the calculator", (ctx) => {
45
+ result += ctx.step.values[0];
46
+ });
47
+
48
+ when("I press add", () => {
49
+ // Addition already happened above
50
+ });
51
+
52
+ then("the result should be '120'", (ctx) => {
53
+ expect(result).toBe(ctx.step.values[0]);
54
+ });
55
+ });
56
+ });
57
+ ```
58
+
59
+ > **Why `Then as then`?** ES modules treat `then` as a thenable indicator. We export `Then` (uppercase) and you alias it.
60
+
61
+ ### Configure Vitest
62
+
63
+ ```ts
64
+ // vitest.config.ts
65
+ import { defineConfig } from 'vitest/config';
66
+ import { LiveDocSpecReporter } from '@swedevtools/livedoc-vitest/reporter';
67
+
68
+ export default defineConfig({
69
+ test: {
70
+ globals: true,
71
+ include: ['**/*.Spec.ts'],
72
+ reporters: [new LiveDocSpecReporter()],
73
+ },
74
+ });
75
+ ```
76
+
77
+ ### Run
78
+
79
+ ```bash
80
+ npx vitest run
81
+ ```
82
+
83
+ ---
84
+
85
+ ## Documentation
86
+
87
+ 📖 **[Full documentation at livedoc.swedevtools.com →](https://livedoc.swedevtools.com/vitest/learn/getting-started)**
88
+
89
+ Covers getting started, BDD & Specification patterns, data extraction, scenario outlines, tags & filtering, reporters, viewer integration, CI/CD, troubleshooting, and more.
90
+
91
+ ---
92
+
93
+ ## License
94
+
95
+ MIT
@@ -0,0 +1,206 @@
1
+ import { Attachment } from '@swedevtools/livedoc-schema';
2
+
3
+ /**
4
+ * Global type definitions for LiveDoc-Vitest
5
+ */
6
+
7
+ type DataTableRow = any[] | { [key: string]: any };
8
+
9
+ interface LiveDocMetaTable {
10
+ name: string;
11
+ description: string;
12
+ dataTable: DataTableRow[];
13
+ }
14
+
15
+ interface LiveDocStepTaskMeta {
16
+ kind: "step";
17
+ step: {
18
+ rawTitle: string;
19
+ type: string;
20
+ };
21
+ scenarioOutline?: {
22
+ title?: string;
23
+ description: string;
24
+ tables: LiveDocMetaTable[];
25
+ tags: string[];
26
+ example: {
27
+ sequence: number;
28
+ values: Record<string, unknown>;
29
+ };
30
+ };
31
+ }
32
+
33
+ interface LiveDocRuleExampleTaskMeta {
34
+ kind: "ruleExample";
35
+ ruleOutline: {
36
+ title: string;
37
+ description: string;
38
+ tables: LiveDocMetaTable[];
39
+ tags: string[];
40
+ example: {
41
+ sequence: number;
42
+ values: Record<string, unknown>;
43
+ };
44
+ };
45
+ }
46
+
47
+ interface LiveDocRuleTaskMeta {
48
+ kind: "rule";
49
+ rule: {
50
+ title: string;
51
+ description: string;
52
+ tags: string[];
53
+ };
54
+ }
55
+
56
+ type LiveDocTaskMeta = LiveDocStepTaskMeta | LiveDocRuleExampleTaskMeta | LiveDocRuleTaskMeta;
57
+
58
+ /**
59
+ * Extend Vitest's TaskMeta to include LiveDoc context
60
+ */
61
+ declare module "@vitest/runner" {
62
+ interface TaskMeta {
63
+ livedoc?: LiveDocTaskMeta;
64
+ }
65
+ }
66
+
67
+ /**
68
+ * Framework metadata about the current step
69
+ * READ-ONLY - contains title, parsed values, tables, docStrings
70
+ * Provides helpers for accessing step data in various formats
71
+ */
72
+ declare class StepContext {
73
+ private _table?;
74
+ private _attachments;
75
+ title: string;
76
+ displayTitle: string;
77
+ dataTable: DataTableRow[];
78
+ docString: string;
79
+ type: string;
80
+ values: any[];
81
+ valuesRaw: string[];
82
+ params: Record<string, any>;
83
+ paramsRaw: Record<string, string>;
84
+ constructor(attachments?: Attachment[]);
85
+ /**
86
+ * Attach arbitrary data (base64-encoded) to this step.
87
+ */
88
+ attach(data: string, opts?: {
89
+ title?: string;
90
+ mimeType?: string;
91
+ kind?: 'image' | 'screenshot' | 'file';
92
+ }): void;
93
+ /**
94
+ * Convenience: attach a PNG screenshot.
95
+ */
96
+ attachScreenshot(base64: string, title?: string): void;
97
+ /**
98
+ * Convenience: attach a JSON payload (e.g., API response).
99
+ */
100
+ attachJSON(data: unknown, title?: string): void;
101
+ /** Attachments collected during step execution. */
102
+ get attachments(): Attachment[];
103
+ /**
104
+ * Parse docString as JSON entity
105
+ */
106
+ get docStringAsEntity(): any;
107
+ /**
108
+ * Get data table with headers as column names
109
+ */
110
+ get table(): DataTableRow[];
111
+ /**
112
+ * Convert 2-column table to key-value entity
113
+ */
114
+ get tableAsEntity(): DataTableRow | undefined;
115
+ /**
116
+ * Get data table as-is (raw array of arrays)
117
+ */
118
+ tableAsList(): DataTableRow[];
119
+ /**
120
+ * Get first column as single array
121
+ */
122
+ get tableAsSingleList(): any[];
123
+ private convertToTable;
124
+ private convertDataTableRowToEntity;
125
+ private coerceValue;
126
+ private convertToDateIfPossible;
127
+ }
128
+
129
+ /**
130
+ * Framework metadata about the scenario
131
+ * READ-ONLY - contains title/description/tags/step references
132
+ * NOT for user test data! Use local variables instead.
133
+ */
134
+ declare class ScenarioContext {
135
+ title: string;
136
+ description: string;
137
+ given?: StepContext;
138
+ and: StepContext[];
139
+ tags: string[];
140
+ /** All steps in this scenario */
141
+ steps: StepContext[];
142
+ }
143
+
144
+ /**
145
+ * Framework metadata about the feature
146
+ * READ-ONLY - contains file/title/description/tags
147
+ * NOT for user test data! Use local variables instead.
148
+ */
149
+ declare class FeatureContext {
150
+ filename: string;
151
+ title: string;
152
+ description: string;
153
+ tags: string[];
154
+ }
155
+
156
+ /**
157
+ * Framework metadata about the background
158
+ * READ-ONLY - extends ScenarioContext with background-specific data
159
+ */
160
+ declare class BackgroundContext extends ScenarioContext {
161
+ }
162
+
163
+ /**
164
+ * Framework metadata about the specification
165
+ * READ-ONLY - contains file/title/description/tags
166
+ * NOT for user test data! Use local variables instead.
167
+ */
168
+ declare class SpecificationContext {
169
+ filename: string;
170
+ title: string;
171
+ description: string;
172
+ tags: string[];
173
+ }
174
+
175
+ /**
176
+ * Framework metadata about the rule.
177
+ * Provides title, description, tags, and extracted values/params from the rule title.
178
+ *
179
+ * @example
180
+ * ```typescript
181
+ * rule("Adding '5' and '3' returns '8'", (ctx) => {
182
+ * const [a, b, expected] = ctx.rule.values; // [5, 3, 8]
183
+ * expect(a + b).toBe(expected);
184
+ * });
185
+ *
186
+ * rule("Processing <action:login> for <user:alice>", (ctx) => {
187
+ * const action = ctx.rule.params.action; // "login"
188
+ * });
189
+ * ```
190
+ */
191
+ declare class RuleContext {
192
+ title: string;
193
+ description: string;
194
+ tags: string[];
195
+ specification: SpecificationContext;
196
+ /** Extracted and type-coerced quoted values from the rule title. */
197
+ values: any[];
198
+ /** Raw string values before type coercion. */
199
+ valuesRaw: string[];
200
+ /** Extracted and type-coerced named parameters from <name:value> patterns. */
201
+ params: Record<string, any>;
202
+ /** Raw string named parameters before type coercion. */
203
+ paramsRaw: Record<string, string>;
204
+ }
205
+
206
+ export { BackgroundContext as B, type DataTableRow as D, FeatureContext as F, RuleContext as R, ScenarioContext as S, StepContext as a, SpecificationContext as b };
@@ -0,0 +1,206 @@
1
+ import { Attachment } from '@swedevtools/livedoc-schema';
2
+
3
+ /**
4
+ * Global type definitions for LiveDoc-Vitest
5
+ */
6
+
7
+ type DataTableRow = any[] | { [key: string]: any };
8
+
9
+ interface LiveDocMetaTable {
10
+ name: string;
11
+ description: string;
12
+ dataTable: DataTableRow[];
13
+ }
14
+
15
+ interface LiveDocStepTaskMeta {
16
+ kind: "step";
17
+ step: {
18
+ rawTitle: string;
19
+ type: string;
20
+ };
21
+ scenarioOutline?: {
22
+ title?: string;
23
+ description: string;
24
+ tables: LiveDocMetaTable[];
25
+ tags: string[];
26
+ example: {
27
+ sequence: number;
28
+ values: Record<string, unknown>;
29
+ };
30
+ };
31
+ }
32
+
33
+ interface LiveDocRuleExampleTaskMeta {
34
+ kind: "ruleExample";
35
+ ruleOutline: {
36
+ title: string;
37
+ description: string;
38
+ tables: LiveDocMetaTable[];
39
+ tags: string[];
40
+ example: {
41
+ sequence: number;
42
+ values: Record<string, unknown>;
43
+ };
44
+ };
45
+ }
46
+
47
+ interface LiveDocRuleTaskMeta {
48
+ kind: "rule";
49
+ rule: {
50
+ title: string;
51
+ description: string;
52
+ tags: string[];
53
+ };
54
+ }
55
+
56
+ type LiveDocTaskMeta = LiveDocStepTaskMeta | LiveDocRuleExampleTaskMeta | LiveDocRuleTaskMeta;
57
+
58
+ /**
59
+ * Extend Vitest's TaskMeta to include LiveDoc context
60
+ */
61
+ declare module "@vitest/runner" {
62
+ interface TaskMeta {
63
+ livedoc?: LiveDocTaskMeta;
64
+ }
65
+ }
66
+
67
+ /**
68
+ * Framework metadata about the current step
69
+ * READ-ONLY - contains title, parsed values, tables, docStrings
70
+ * Provides helpers for accessing step data in various formats
71
+ */
72
+ declare class StepContext {
73
+ private _table?;
74
+ private _attachments;
75
+ title: string;
76
+ displayTitle: string;
77
+ dataTable: DataTableRow[];
78
+ docString: string;
79
+ type: string;
80
+ values: any[];
81
+ valuesRaw: string[];
82
+ params: Record<string, any>;
83
+ paramsRaw: Record<string, string>;
84
+ constructor(attachments?: Attachment[]);
85
+ /**
86
+ * Attach arbitrary data (base64-encoded) to this step.
87
+ */
88
+ attach(data: string, opts?: {
89
+ title?: string;
90
+ mimeType?: string;
91
+ kind?: 'image' | 'screenshot' | 'file';
92
+ }): void;
93
+ /**
94
+ * Convenience: attach a PNG screenshot.
95
+ */
96
+ attachScreenshot(base64: string, title?: string): void;
97
+ /**
98
+ * Convenience: attach a JSON payload (e.g., API response).
99
+ */
100
+ attachJSON(data: unknown, title?: string): void;
101
+ /** Attachments collected during step execution. */
102
+ get attachments(): Attachment[];
103
+ /**
104
+ * Parse docString as JSON entity
105
+ */
106
+ get docStringAsEntity(): any;
107
+ /**
108
+ * Get data table with headers as column names
109
+ */
110
+ get table(): DataTableRow[];
111
+ /**
112
+ * Convert 2-column table to key-value entity
113
+ */
114
+ get tableAsEntity(): DataTableRow | undefined;
115
+ /**
116
+ * Get data table as-is (raw array of arrays)
117
+ */
118
+ tableAsList(): DataTableRow[];
119
+ /**
120
+ * Get first column as single array
121
+ */
122
+ get tableAsSingleList(): any[];
123
+ private convertToTable;
124
+ private convertDataTableRowToEntity;
125
+ private coerceValue;
126
+ private convertToDateIfPossible;
127
+ }
128
+
129
+ /**
130
+ * Framework metadata about the scenario
131
+ * READ-ONLY - contains title/description/tags/step references
132
+ * NOT for user test data! Use local variables instead.
133
+ */
134
+ declare class ScenarioContext {
135
+ title: string;
136
+ description: string;
137
+ given?: StepContext;
138
+ and: StepContext[];
139
+ tags: string[];
140
+ /** All steps in this scenario */
141
+ steps: StepContext[];
142
+ }
143
+
144
+ /**
145
+ * Framework metadata about the feature
146
+ * READ-ONLY - contains file/title/description/tags
147
+ * NOT for user test data! Use local variables instead.
148
+ */
149
+ declare class FeatureContext {
150
+ filename: string;
151
+ title: string;
152
+ description: string;
153
+ tags: string[];
154
+ }
155
+
156
+ /**
157
+ * Framework metadata about the background
158
+ * READ-ONLY - extends ScenarioContext with background-specific data
159
+ */
160
+ declare class BackgroundContext extends ScenarioContext {
161
+ }
162
+
163
+ /**
164
+ * Framework metadata about the specification
165
+ * READ-ONLY - contains file/title/description/tags
166
+ * NOT for user test data! Use local variables instead.
167
+ */
168
+ declare class SpecificationContext {
169
+ filename: string;
170
+ title: string;
171
+ description: string;
172
+ tags: string[];
173
+ }
174
+
175
+ /**
176
+ * Framework metadata about the rule.
177
+ * Provides title, description, tags, and extracted values/params from the rule title.
178
+ *
179
+ * @example
180
+ * ```typescript
181
+ * rule("Adding '5' and '3' returns '8'", (ctx) => {
182
+ * const [a, b, expected] = ctx.rule.values; // [5, 3, 8]
183
+ * expect(a + b).toBe(expected);
184
+ * });
185
+ *
186
+ * rule("Processing <action:login> for <user:alice>", (ctx) => {
187
+ * const action = ctx.rule.params.action; // "login"
188
+ * });
189
+ * ```
190
+ */
191
+ declare class RuleContext {
192
+ title: string;
193
+ description: string;
194
+ tags: string[];
195
+ specification: SpecificationContext;
196
+ /** Extracted and type-coerced quoted values from the rule title. */
197
+ values: any[];
198
+ /** Raw string values before type coercion. */
199
+ valuesRaw: string[];
200
+ /** Extracted and type-coerced named parameters from <name:value> patterns. */
201
+ params: Record<string, any>;
202
+ /** Raw string named parameters before type coercion. */
203
+ paramsRaw: Record<string, string>;
204
+ }
205
+
206
+ export { BackgroundContext as B, type DataTableRow as D, FeatureContext as F, RuleContext as R, ScenarioContext as S, StepContext as a, SpecificationContext as b };
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1,104 @@
1
+ import { F as FeatureContext, S as ScenarioContext, a as StepContext, B as BackgroundContext, b as SpecificationContext, R as RuleContext } from './RuleContext-BZhuy-zS.cjs';
2
+ import '@swedevtools/livedoc-schema';
3
+
4
+ /**
5
+ * The context object passed to all LiveDoc test functions.
6
+ * Provides access to feature, scenario, step, example, and background contexts.
7
+ */
8
+ interface LiveDocTestContext {
9
+ /** Framework metadata about the current feature */
10
+ feature?: FeatureContext;
11
+ /** Framework metadata about the current scenario */
12
+ scenario?: ScenarioContext;
13
+ /** Framework metadata about the current step */
14
+ step?: StepContext;
15
+ /** Framework metadata about the current scenario outline example */
16
+ example?: ScenarioContext;
17
+ /** Framework metadata about the background */
18
+ background?: BackgroundContext;
19
+ }
20
+
21
+ /**
22
+ * The context object passed to Specification pattern test functions.
23
+ */
24
+ interface SpecificationTestContext {
25
+ /** Framework metadata about the current specification */
26
+ specification?: SpecificationContext;
27
+ /** Framework metadata about the current rule */
28
+ rule?: RuleContext;
29
+ /** Example data for rule outlines */
30
+ example?: Record<string, any>;
31
+ }
32
+
33
+ declare global {
34
+ /**
35
+ * Define a Gherkin feature
36
+ */
37
+ function feature(title: string, fn: (ctx: LiveDocTestContext) => void): void;
38
+
39
+ /**
40
+ * Define a scenario within a feature
41
+ */
42
+ function scenario(title: string, fn: (ctx: LiveDocTestContext) => void | Promise<void>): void;
43
+
44
+ /**
45
+ * Define a scenario outline (data-driven test)
46
+ * Examples are extracted from the title string by the parser
47
+ */
48
+ function scenarioOutline(title: string, fn: (ctx: LiveDocTestContext) => void): void;
49
+
50
+ /**
51
+ * Define background steps that run before each scenario
52
+ */
53
+ function background(title: string, fn: (ctx: LiveDocTestContext & {
54
+ afterBackground: (fn: () => void | Promise<void>) => void;
55
+ }) => void): void;
56
+
57
+ /**
58
+ * Define a given step (precondition)
59
+ */
60
+ function given(title: string, fn?: (ctx: LiveDocTestContext) => void | Promise<void>, passedParam?: object | Function): void;
61
+
62
+ /**
63
+ * Define a when step (action)
64
+ */
65
+ function when(title: string, fn?: (ctx: LiveDocTestContext) => void | Promise<void>, passedParam?: object | Function): void;
66
+
67
+ /**
68
+ * Define a then step (assertion)
69
+ * Available as lowercase global when using globals mode.
70
+ */
71
+ function then(title: string, fn?: (ctx: LiveDocTestContext) => void | Promise<void>, passedParam?: object | Function): void;
72
+
73
+ /**
74
+ * Define an and step (continuation)
75
+ */
76
+ function and(title: string, fn?: (ctx: LiveDocTestContext) => void | Promise<void>, passedParam?: object | Function): void;
77
+
78
+ /**
79
+ * Define a but step (continuation with contrast)
80
+ */
81
+ function but(title: string, fn?: (ctx: LiveDocTestContext) => void | Promise<void>, passedParam?: object | Function): void;
82
+
83
+ // ============================================
84
+ // Specification Pattern Globals
85
+ // ============================================
86
+
87
+ /**
88
+ * Define a specification (container for rules)
89
+ */
90
+ function specification(title: string, fn: (ctx: SpecificationTestContext) => void): void;
91
+
92
+ /**
93
+ * Define a rule within a specification
94
+ */
95
+ function rule(title: string, fn: (ctx: SpecificationTestContext) => void | Promise<void>): void;
96
+
97
+ /**
98
+ * Define a rule outline (data-driven rules)
99
+ * Examples are extracted from the title string by the parser
100
+ */
101
+ function ruleOutline(title: string, fn: (ctx: SpecificationTestContext) => void | Promise<void>): void;
102
+ }
103
+
104
+ export type { LiveDocTestContext, SpecificationTestContext };