Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/big-masks-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/doctest": patch
---

Support `.mdx` files
6 changes: 3 additions & 3 deletions packages/tools/doctest/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `@effect/doctest`

`@effect/doctest` extracts marked TypeScript examples from JSDoc comments and Markdown files, then runs each example as an isolated Vitest module.
`@effect/doctest` extracts marked TypeScript examples from JSDoc comments, Markdown, and MDX files, then runs each example as an isolated Vitest module.

Mark runnable fences with `import.meta.vitest`:

Expand Down Expand Up @@ -51,7 +51,7 @@ export const value = 1

Markers must trail a complete expression statement or supported `const` declaration on the same line. Standalone markers, destructuring declarations, multiple declarations, and `let` or `var` declarations are not supported. The transform does not implicitly await promises, run Effects, or consume iterators; write those operations explicitly. Ordinary comments are ignored. Await asynchronous work so all assertions and cleanup occur before the snippet module finishes evaluating.

Regular tests can use `include` in the same project. Documentation sources use `includeSource`, which lets Vitest discard files without the marker before collection. The plugin resolves imports relative to each example's original TypeScript or Markdown file:
Regular tests can use `include` in the same project. Documentation sources use `includeSource`, which lets Vitest discard files without the marker before collection. The plugin resolves imports relative to each example's original TypeScript, Markdown, or MDX file:

```ts
import * as Doctest from "@effect/doctest/Plugin"
Expand All @@ -61,7 +61,7 @@ export default defineConfig({
plugins: [Doctest.plugin()],
test: {
include: ["test/**/*.test.ts"],
includeSource: ["src/**/*.ts", "docs/**/*.md"]
includeSource: ["src/**/*.ts", "docs/**/*.{md,mdx}"]
}
})
```
Expand Down
4 changes: 3 additions & 1 deletion packages/tools/doctest/src/Source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,6 @@ export const extract = (source: string, format: SourceFormat = "jsdoc"): Readonl
* @since 4.0.0
*/
export const extractFile = (file: string): Promise<ReadonlyArray<Snippet>> =>
readFile(file, "utf8").then((source) => extract(source, file.endsWith(".md") ? "markdown" : "jsdoc"))
readFile(file, "utf8").then((source) =>
extract(source, file.endsWith(".md") || file.endsWith(".mdx") ? "markdown" : "jsdoc")
)
20 changes: 20 additions & 0 deletions packages/tools/doctest/test/Source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,24 @@ describe("Source", () => {
])
})
})

describe("MDX", () => {
it("extracts marked TypeScript fences from mdx content", () => {
Comment thread
fubhy marked this conversation as resolved.
const source = [
"import { SomeComponent } from './component'",
"",
"## Example",
"",
"<SomeComponent />",
"",
"```ts import.meta.vitest",
"const result = 42",
"```"
].join("\n")

assert.deepStrictEqual(Source.extract(source, "markdown"), [
{ source: "const result = 42", line: 7, name: undefined }
])
})
})
})
Loading