--- title: doctest — Functions description: Functions and classes for the doctest package --- Functions and classes exported by this package. ## extractExamples(symbols) Extracts all `@example` blocks from a list of `ForgeSymbol` objects. **Signature** ```typescript (symbols: ForgeSymbol[]) => ExtractedExample[] ``` **Parameters** | Name | Type | Description | | --------- | --------------- | -------------------------------------------- | | `symbols` | `ForgeSymbol[]` | The symbols produced by the core AST walker. | **Returns** — A flat array of `ExtractedExample` objects, one per code block. **Example** ```typescript import { createWalker, loadConfig } from "@forge-ts/core"; import { extractExamples } from "@forge-ts/doctest"; const config = await loadConfig(); const symbols = createWalker(config).walk(); const examples = extractExamples(symbols); console.log(`Found ${examples.length} examples`); ``` ## generateTestFiles(examples, options) Generates a virtual test file for a set of extracted examples. Each example is wrapped in an `it()` block using the Node built-in `node:test` runner so that no additional test framework is required. Auto-imports the tested symbol from the source file, processes `// =>` assertion patterns, and appends an inline source map. **Signature** ```typescript (examples: ExtractedExample[], options: GeneratorOptions) => VirtualTestFile[] ``` **Parameters** | Name | Type | Description | | ---------- | -------------------- | ------------------------------------------ | | `examples` | `ExtractedExample[]` | Examples to include in the generated file. | | `options` | `GeneratorOptions` | Output configuration. | **Returns** — An array of `VirtualTestFile` objects (one per source file). **Example** ```typescript import { generateTestFiles } from "@forge-ts/doctest"; const files = generateTestFiles(examples, { cacheDir: "/tmp/doctest-cache" }); console.log(`Generated ${files.length} test file(s)`); ``` ## runTests(files) Writes virtual test files to disk and executes them with Node 24 native TypeScript support (`--experimental-strip-types --test`). **Signature** ```typescript (files: VirtualTestFile[]) => Promise ``` **Parameters** | Name | Type | Description | | ------- | ------------------- | ---------------------------------------- | | `files` | `VirtualTestFile[]` | The virtual test files to write and run. | **Returns** — A `RunResult` summarising the test outcome. **Example** ```typescript import { runTests } from "@forge-ts/doctest"; const result = await runTests(virtualFiles); if (!result.success) { console.error(`${result.failed} doctest(s) failed`); } ``` ## doctest(config) Runs the full doctest pipeline: extract → generate → run. **Signature** ```typescript (config: ForgeConfig) => Promise ``` **Parameters** | Name | Type | Description | | -------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `config` | `ForgeConfig` | The resolved `ForgeConfig` for the project. The `config.doctest.cacheDir` field controls where generated test files are written; all other doctest behaviour uses `config.rootDir` and `config.tsconfig`. | **Returns** — A `ForgeResult` whose `success` field is `true` only when every extracted example passes. `errors` contains `D001`/`D002` diagnostics on failure; `symbols` is the full symbol graph from the walk step. **Example** ```typescript import { loadConfig } from "@forge-ts/core"; import { doctest } from "@forge-ts/doctest"; const config = await loadConfig(); const result = await doctest(config); if (!result.success) { console.error(`${result.errors.length} doctest failure(s)`); process.exit(1); } ``` ## extractExamples(symbols) Extracts all `@example` blocks from a list of `ForgeSymbol` objects. **Signature** ```typescript (symbols: ForgeSymbol[]) => ExtractedExample[] ``` **Parameters** | Name | Type | Description | | --------- | --------------- | -------------------------------------------- | | `symbols` | `ForgeSymbol[]` | The symbols produced by the core AST walker. | **Returns** — A flat array of `ExtractedExample` objects, one per code block. **Example** ```typescript import { createWalker, loadConfig } from "@forge-ts/core"; import { extractExamples } from "@forge-ts/doctest"; const config = await loadConfig(); const symbols = createWalker(config).walk(); const examples = extractExamples(symbols); console.log(`Found ${examples.length} examples`); ``` ## generateTestFiles(examples, options) Generates a virtual test file for a set of extracted examples. Each example is wrapped in an `it()` block using the Node built-in `node:test` runner so that no additional test framework is required. Auto-imports the tested symbol from the source file, processes `// =>` assertion patterns, and appends an inline source map. **Signature** ```typescript (examples: ExtractedExample[], options: GeneratorOptions) => VirtualTestFile[] ``` **Parameters** | Name | Type | Description | | ---------- | -------------------- | ------------------------------------------ | | `examples` | `ExtractedExample[]` | Examples to include in the generated file. | | `options` | `GeneratorOptions` | Output configuration. | **Returns** — An array of `VirtualTestFile` objects (one per source file). **Example** ```typescript import { generateTestFiles } from "@forge-ts/doctest"; const files = generateTestFiles(examples, opts); console.log(`Generated ${files.length} test file(s)`); ``` ## runTests(files) Writes virtual test files to disk and executes them with Node 24 native TypeScript support (`--experimental-strip-types --test`). **Signature** ```typescript (files: VirtualTestFile[]) => Promise ``` **Parameters** | Name | Type | Description | | ------- | ------------------- | ---------------------------------------- | | `files` | `VirtualTestFile[]` | The virtual test files to write and run. | **Returns** — A `RunResult` summarising the test outcome. **Example** ```typescript import { runTests } from "@forge-ts/doctest"; const result = await runTests(virtualFiles); if (!result.success) { console.error(`${result.failed} doctest(s) failed`); } ``` ## doctest(config) Runs the full doctest pipeline: extract → generate → run. **Signature** ```typescript (config: ForgeConfig) => Promise ``` **Parameters** | Name | Type | Description | | -------- | ------------- | ------------------------------------------- | | `config` | `ForgeConfig` | The resolved `ForgeConfig` for the project. | **Returns** — A `ForgeResult` with success/failure and any diagnostics. **Example** ```typescript import { loadConfig } from "@forge-ts/core"; import { doctest } from "@forge-ts/doctest"; const config = await loadConfig(); const result = await doctest(config); if (!result.success) { console.error(`${result.errors.length} doctest failure(s)`); } ```