On this page
The short answerHow txm selects and runs examplesWalk through an annotated READMEChoose expectations that test the contractRun txm in CICompare txm with the engineTriage findingsA checklist for annotated pagesWhere DocsAlive fitsThe sources we checkedFAQThe short answer
txm is a Node.js command-line tool that tests annotated Markdown examples and reports results in TAP format. A program annotation chooses the command that receives a block on standard input, so the tested language can be any language that command supports. DocsAlive provides a complementary scan: engine 0.6.3 ignores txm annotations, runs recognized unannotated blocks too, executes JavaScript on Node 20, and does not compare JavaScript output. Each page starts in an empty work directory, so a relative require of a repository file fails as a runtime error; packages need an install step or --setup.
Use txm when you need a precise input/output contract for selected examples. Use the scan to look beyond annotations and classify failures, while keeping its language coverage and clean-work-directory behavior in view. Neither a txm pass nor a scan pass proves that every visible example was exercised.
How txm selects and runs examples
txm reads annotations in HTML comments before code blocks. A program annotation sets a shell command for following tests, until another program is declared. txm sends each input block to that command on standard input. The command may be a whole script, so the model is not limited to JavaScript even though txm itself runs on Node.js.
The checked release is txm 8.2.0. Its package metadata specifies Node 16 or newer; its README describes Node.js as a requirement and says the minimum tested version is the current LTS. The example run below used Node 24.22.1.
Named in, out, and err annotations associate a block with input, expected stdout, or expected stderr. Names must match exactly. The halves can appear in any order in the document, but missing input, missing both output and error expectations, or duplicate pieces produce errors. Matching is exact text; the diff can show invisible characters such as spaces.
A check test runs its block and ignores output. It passes when the exit status is zero, or matches an exit annotation. exit nonzero accepts any nonzero status, which is convenient for an error example but cannot prove that the intended error occurred. If any test has an only annotation, tests without it are skipped.
Indented and fenced blocks both work. Each annotation is its own comment. Within a comment, the hyphen escape #- becomes a hyphen. The program receives TXM_INDEX, TXM_NAME, TXM_INPUT_LANG, and TXM_HAS_COLOUR environment variables. These details matter when a shared test command needs to identify the current test or control its display.
Walk through an annotated README
The package name acme-slug and the module slug.js below are illustrative, not a published package. The README and local files show the relationship between annotations, program selection, and results.
Install txm as a development dependency:
npm install --save-dev txm
The illustrative module exposes a slug function and a shortening function. The README will test both the returned text and a self-checking call.
// Tiny slug helpers used in the DocsAlive guides.
function slugify(text, sep = "-") {
const keep = [...text].map((c) => (/[\p{L}\p{N}]/u.test(c) ? c.toLowerCase() : " ")).join("");
return keep.split(/\s+/).filter(Boolean).join(sep);
}
function truncateSlug(slug, limit) {
if (slug.length <= limit) return slug;
const cut = slug.slice(0, limit + 1);
return cut.includes("-") ? cut.slice(0, cut.lastIndexOf("-")) : slug.slice(0, limit);
}
module.exports = { slugify, truncateSlug };
The illustrative README first selects Node. It pairs a named input with expected stdout, uses an assertion for a check test, and expects a nonzero exit for invalid input. It then selects Bash for a separate shell test. The output blocks are part of txm’s contract because their names match the corresponding inputs.
# acme-slug
<!-- !test program node -->
Make a slug:
<!-- !test in make a slug -->
```js
const { slugify } = require("./slug.js");
console.log(slugify("Docs That Run Everywhere"));
```
It prints:
<!-- !test out make a slug -->
```text
docs-that-run-everywhere
```
Shorten it for a URL. This example checks itself with assert:
<!-- !test check shorten a slug -->
```js
const assert = require("node:assert");
const { truncateSlug } = require("./slug.js");
assert.strictEqual(truncateSlug("docs-that-run-everywhere", 13), "docs-that-run");
```
Passing something that is not a string throws:
<!-- !test exit nonzero -->
<!-- !test check bad input -->
```js
require("./slug.js").slugify(42);
```
<!-- !test program bash -->
The same slug from a shell:
<!-- !test in shell slug -->
```bash
node -e 'console.log(require("./slug.js").slugify("Hello, World"))'
```
<!-- !test out shell slug -->
```text
hello-world
```
Run txm against the README from the project directory:
npx txm README.md
The checked run produced four passing tests in TAP version 13. Those four results correspond to the annotated tests; an unannotated block would not add a result.
TAP version 13
1..4
ok 1 make a slug
ok 2 shorten a slug
ok 3 bad input
ok 4 shell slug
# 4/4 passed
# OK
The next change is broken on purpose relative to the README. It changes the default separator and renames the shortening function, leaving the documented calls and expected values behind.
--- a/slug.js
+++ b/slug.js
@@ -1,13 +1,13 @@
// Tiny slug helpers used in the DocsAlive guides.
-function slugify(text, sep = "-") {
+function slugify(text, sep = "_") {
const keep = [...text].map((c) => (/[\p{L}\p{N}]/u.test(c) ? c.toLowerCase() : " ")).join("");
return keep.split(/\s+/).filter(Boolean).join(sep);
}
-function truncateSlug(slug, limit) {
+function shortenSlug(slug, limit) {
if (slug.length <= limit) return slug;
const cut = slug.slice(0, limit + 1);
return cut.includes("-") ? cut.slice(0, cut.lastIndexOf("-")) : slug.slice(0, limit);
}
-module.exports = { slugify, truncateSlug };
+module.exports = { slugify, shortenSlug };
After the change, two output checks mismatch and the shortening check exits with an error. The invalid-input test still passes because any nonzero status satisfies its expectation. The checked run has one pass and three failures, and txm exits with status 1.
not ok 1 make a slug: output mismatch
---
expected stdout: |
docs-that-run-everywhere
actual stdout: |
docs_that_run_everywhere
program: 'node'
...
not ok 2 shorten a slug: program exited with error
---
program: 'node'
exit status: 1
stderr: |
[stdin]:3
...
TypeError: truncateSlug is not a function
...
ok 3 bad input
not ok 4 shell slug: output mismatch
...
# 1/4 passed
# FAILED 3
The illustrative typo.md below is broken on purpose. Its input is named a, while its expected output is named b. txm treats these as two incomplete tests rather than guessing that they belong together.
<!-- !test program node -->
<!-- !test in a -->
```js
console.log(1)
```
<!-- !test out b -->
```text
1
```
The checked run reports both missing halves and exits with status 1. When a page contains many annotations, search for the full test name on both sides before changing the example itself.
not ok 1 a: only input defined
...
not ok 2 b: input not defined
...
# 0/2 passed
# FAILED 2
Choose expectations that test the contract
Use an in and out pair when the exact stdout is a stable part of what the page promises. This catches a changed value even if the program exits successfully. Review spaces and line endings when a mismatch looks visually identical: exact matching makes invisible differences relevant.
Use err when the complete stderr is stable and is the contract you intend to check. txm compares against all of stderr. A Node stack trace can include paths and other details that make a one-line expected error brittle. A check combined with exit nonzero avoids matching the full trace, but accepts an unrelated failure too. Alternatively, select a program that filters stderr and make the filtered contract explicit.
A check can contain its own assertions. Give those assertions enough context to identify what failed, and consider whether the example remains useful to a reader who copies it. An exit annotation can require a specific status; exit nonzero is broader. When the exact error matters, do not treat a generic nonzero result as proof of its cause.
Unannotated blocks are a separate coverage problem. txm neither runs nor reports them. A checked probe containing an unannotated JavaScript block that threw still reported the one annotated test as passing. Audit the rendered page against the test inventory, especially after adding examples. The JavaScript docs testing guide discusses other ways to exercise examples.
Run txm in CI
The command accepts an optional filename; without one, it reads standard input. --jobs controls parallel tests and defaults to the number of CPUs reported by the operating system. Results still print in definition order. Use --jobs 1 when serial execution helps investigate shared resources, but do not assume parallel execution is safe for tests that mutate the same file.
txm exits zero if and only if all tests pass. It also offers --help and --version. Color can be controlled with --color or --no-color, or with FORCE_COLOR and NO_COLOR. TAP version 13 output makes the test list and status suitable for tools that consume that format.
Make the working directory and dependencies explicit in CI. A program that reads a local module needs that module available where it executes. A test that passes only because a developer has a global package or generated file is weaker evidence for a new reader. Check the documented installation path separately from the test harness.
Compare txm with the engine
| txm feature | What txm does | What the engine does in 0.6.3 |
|---|---|---|
| Annotation selection | Runs annotated tests; unannotated blocks are absent from the report | Ignores annotation comments and runs recognized unannotated blocks too |
| Program | Uses the selected shell command for following tests | Chooses behavior from the fence language |
| Input and output names | Matches named halves and reports incomplete or duplicate tests | Does not interpret txm names |
| stdout and stderr | Compares annotated expectations as exact text | Does not compare JavaScript output or txm output blocks |
check and exit |
Tests exit status, including an optional nonzero expectation | Ignores those annotations and classifies actual execution results |
only |
Skips tests without it when an only test exists |
Ignores the annotation |
| Local module | The program can use files available in its working environment | Each page starts in an empty work directory; repository files are not copied in |
| JavaScript runtime | Runs the chosen program | Runs recognized JavaScript fences on Node 20 |
In the clean-sandbox probe of the README, the JavaScript and shell examples that required the illustrative local module failed as runtime errors needing review. That is a setup difference, not proof that the module itself is broken. Install the package through the page or --setup when that reflects the reader’s path. A file that exists only in your repository checkout is not available to the page in the scan.
When the examples were instead run from the repository folder for a separate probe, the simple JavaScript call ran before and after the separator change: the engine did not compare its output. The renamed function and invalid-input examples produced runtime errors. The text output blocks were skipped. Keep txm’s exact expectations for the values it is meant to guard.
The engine compares printed output only in Python pycon or python-repl sessions. It also has a known limitation for unknown fence labels: a label with no checker can be reported as passing without being run or parsed. Check framework coverage and the operation behind a status.
Triage findings
| Finding | Likely cause | What to do |
|---|---|---|
| A visible block has no txm result | It has no annotation | Add an appropriate test or record why it is intentionally uncovered |
| Input or output is missing | Names differ, a half is absent, or a piece is duplicated | Match names exactly and remove duplicate definitions |
| Output mismatch | The implementation, expected text, or whitespace changed | Inspect the exact diff and decide which behavior is correct |
| Expected-error test passes unexpectedly | exit nonzero accepts any failure |
Check the error more specifically or use a controlled stderr filter |
| Error expectation is brittle | Full stderr includes stack traces or paths | Make the expectation stable or use a check with a consciously broader exit condition |
| Relative module fails in a scan | The page’s work directory starts empty | Provide a reader-realistic install or setup path; review the runtime error |
| JavaScript runs but a value changed | No txm output assertion covers it, or only the scan ran | Add or retain an exact txm expectation |
| Tests interfere with each other | Parallel jobs share external state | Isolate test resources or investigate with one job |
The README testing guide covers the full onboarding path; CLI docs testing covers commands, prerequisites, and exit behavior. If prompt-and-output examples are central to your docs, compare byexample as well.
A checklist for annotated pages
[ ] List the examples readers are expected to run.
[ ] Confirm every intended txm test has an annotation and a program.
[ ] Match in, out, and err names exactly and remove duplicates.
[ ] Choose exact output checks for stable, meaningful values.
[ ] Review full stderr expectations for paths and unstable traces.
[ ] Check whether nonzero exits could hide an unrelated failure.
[ ] Remove accidental only annotations before merging.
[ ] Make dependencies and working-directory assumptions explicit.
[ ] Review unannotated fences and scan findings separately.
[ ] Rerun the affected tests after changing code or documentation.
Where DocsAlive fits
txm is a strong fit for explicitly annotated, language-independent contracts. Its selected program can execute a language-specific tool and its named expectations can catch changed text. That precision depends on authors annotating the examples they want covered.
A scan with DocsAlive adds a page-oriented pass over recognized fences, a fresh container for each page, and triage of likely docs bugs versus results needing context. It does not interpret txm’s annotations or replace its JavaScript output checks. Read the CLI reference for setup and scope, and findings for categories.
For a Python-focused plugin with its own markers, see pytest-codeblocks. For Sphinx directive tests and Markdown fences, see sphinx.ext.doctest. Keep the tool choice tied to the examples and contracts your readers depend on.
The sources we checked
These sources were read on November 17, 2026. Engine behavior was checked against engine 0.6.3, the engine code in the deployed release, on the same day.
| Source | What it covers |
|---|---|
| txm repository | Annotations, CLI behavior, matching, and supported test patterns |
| TAP version 13 specification | The test output format |
FAQ
Can txm test languages other than JavaScript?
Yes. txm is a Node.js command-line tool, but the program annotation selects the command that receives each test input. That command can run another language or a script.
Does txm run every code block in a Markdown page?
No. Blocks without test annotations are neither run nor reported. Compare the page with the test inventory to find examples that have no coverage.
Can an expected failure pass for the wrong reason?
An exit nonzero expectation accepts any nonzero status. It can therefore accept an unrelated crash. Use a more specific check when the cause of the failure is part of the documented contract.
Why did txm report two errors for one naming typo?
Input and expected output are matched by name. If their names differ, txm sees one test with only input and another without input. Correct the names and rerun the page.
How does DocsAlive handle txm pages?
Engine 0.6.3 ignores txm annotations and runs recognized unannotated blocks. It runs JavaScript on Node 20 without comparing JavaScript output. Each page starts in an empty work directory, so requiring a repository file by relative path fails as a runtime error; packages need an install step or --setup.
Should I use txm and a scan together?
They answer different questions. txm checks the contracts authors annotate, including exact expected text. A scan can surface recognized unannotated examples and classify failures, but it does not reproduce txm annotations or JavaScript output matching.




