Markdown Doctest
raw JSON →markdown-doctest is a command-line utility designed to automatically test code examples embedded within Markdown documentation files. Its primary purpose is to prevent out-of-date or broken code snippets from appearing in project READMEs and other documentation, thereby improving the user experience and reducing embarrassing issues for open-source maintainers. The current stable version is 1.1.0. The project has seen releases relatively consistently in its past, indicating an active maintenance phase, though a specific release cadence is not strictly defined. It differentiates itself by focusing specifically on validating documentation code for executability rather than correctness (unless assertions are explicitly added), integrating seamlessly into CI/CD pipelines, and offering flexible configuration for requiring modules, defining globals, and ignoring specific examples or file patterns. It's not a replacement for a comprehensive test suite but rather a complementary tool for documentation integrity.
Common errors
error ReferenceError: <variable> is not defined ↓
.markdown-doctest-setup.js to expose necessary globals or require common modules for your examples. error Error: Cannot find module '<module-name>' ↓
require section of your .markdown-doctest-setup.js file, e.g., module.exports = { require: { Rx: require('rx') } }. error Error loading .markdown-doctest-setup.js: <syntax error details> ↓
.markdown-doctest-setup.js file. These errors will now cause the test run to halt with a non-zero exit code. Warnings
breaking The tool will now return a non-zero exit code if an error is encountered loading the `.markdown-doctest-config.js` or `.markdown-doctest-setup.js` files. This prevents silent failures in CI/CD environments where a broken configuration might previously have gone unnoticed. ↓
breaking Code snippets with the language specifier `json` will no longer be incorrectly attempted to be run as JavaScript. If you relied on this behavior for any reason, those blocks will now be skipped. ↓
breaking The package upgraded to Babel 6. This change ensures compatibility with newer JavaScript syntax and tooling but might impact projects that were configured for older Babel versions or have specific Babel 5 dependencies. ↓
gotcha By default, `markdown-doctest` recursively scans all `.md` or `.markdown` files but automatically ignores `node_modules` directories in all locations, not just the root. ↓
Install
npm install markdown-doctest yarn add markdown-doctest pnpm add markdown-doctest Imports
- CLI Execution wrong
node markdown-doctestcorrectnpx markdown-doctest [glob] - .markdown-doctest-setup.js wrong
export default { /* config object */ };correctmodule.exports = { /* config object */ }; - Skipping Examples
<!-- skip-example --> ```js // code to skip ```
Quickstart
mkdir doctest-example
cd doctest-example
npm init -y
npm install markdown-doctest
cat > README.md <<EOF
# My Awesome Project
This is a great project!
## Example Usage
```js
const greeting = "Hello";
const name = "World";
console.log(greeting + ", " + name + "!");
```
## Broken Example
```js
// This will fail
const x = 10;
console.log(x + y);
```
EOF
# Run the doctests
npx markdown-doctest
# Expected Output (truncated, showing failure)
# x.
#
# Failed - README.md:16:17
# evalmachine.<anonymous>:7
# console.log(x + y);
# ^
#
# ReferenceError: y is not defined