Guide

Testing JavaScript and TypeScript documentation examples

Run Node examples and type-check TypeScript snippets against the packages and module formats readers use.

Published
Updated
November 10, 2026
Reading time
11 min
Facts checked against
Existing DocsAlive documentation and guide coverage (November 7, 2026)
Outlined documentation pages with a lime pulse line running out of the stack
On this pageThe short answerMap the Node reader journeyDistinguish ESM and CommonJSTest the installed package and versionType-check TypeScript snippetsExtract examples without changing their meaningCompare output without hiding changesHandle credentials, interaction and external statePut the result in contextMake checks part of release workRead reports and improve coverageHow DocsAlive fits

The short answer

Test the JavaScript and TypeScript examples readers actually copy, using the Node version, module format and package version the page promises. Execute complete JavaScript programs in a clean project, type-check TypeScript examples with a deliberate compiler configuration, and check meaningful output and side effects. A type check does not execute code; a successful run does not prove the published types or every supported module format work.

Inventory Markdown and MDX fences, separate alternatives from ordered steps and retain the page location in failures. The Python guide covers a different ecosystem; the release checklist turns these choices into a pre-release record.

Map the Node reader journey

Start at the first documented prerequisite. Record the supported Node versions, package manager, package name, installation source and first successful result. A reader may copy an install command, create a file, choose a module format and run it in a new directory. Reproduce that sequence instead of running only a fragment inside the project's existing development environment.

List complete programs separately from fragments, output blocks and alternative tabs. A browser example may rely on DOM APIs unavailable in Node. A Node example may require a particular working directory or environment variable. Label the intended runtime and explain any prerequisites on the page. Test each supported alternative or mark the gap; running the default tab does not verify the others.

Use a fresh temporary project for the install and execution path. Keep test setup separate from instructions the reader must follow. If the harness creates a configuration file or installs an extra package, determine whether the page must tell the reader to do the same. A global executable or a parent directory's node_modules can otherwise make an incomplete example appear to work.

Distinguish ESM and CommonJS

Node supports both ECMAScript modules and CommonJS, and the way a file is interpreted affects its imports and exports. An .mjs file is treated as an ES module and a .cjs file as CommonJS; the meaning of .js depends on package configuration, including the type field. Package export maps and the package's published files also affect which entry points can be imported. Test the format the page shows in a project configured the way the page describes.

For a self-contained ES module, save this as example.mjs and run node example.mjs:

import { basename } from 'node:path';
console.log(basename('/tmp/example.txt'));

The stable output is example.txt. A CommonJS equivalent uses const { basename } = require('node:path') in an example.cjs file. These are separate examples, not interchangeable instructions for every package. A package may expose different entry points or support only one format. Do not promise a require path merely because an ESM import worked.

For a real library, install the target artifact and use the exact import or require statement shown in the docs. Confirm the package entry point resolves and the expected named or default export is available. Include any required top-level asynchronous handling appropriate to the format. An error such as a missing export can reflect packaging or module configuration, not just a typo in the snippet.

If the documentation supports both module systems, run both paths in clean projects. Check the minimum supported Node version as well as a current supported version. Tooling that transforms modules may accept an example that fails when run directly by Node; make the toolchain and its configuration part of the promise when that is the intended path.

Test the installed package and version

A checkout, a packed candidate and a public registry package are different targets. During development, a workspace dependency can provide fast feedback. Before release, create the package artifact that will be published, install it in a new project and execute the documented examples against that artifact. A post-release check of the public package verifies the installation path available to readers, but cannot block a release already made.

Record the package name, exact version or artifact, Node and package-manager versions, platform and registry. Confirm that the installed files and entry points are the intended ones. A monorepo can resolve a workspace package or undeclared dependency locally even though a user installing the published tarball cannot. Execute the test outside the source tree and avoid inheriting unrelated node_modules directories.

A lockfile improves reproducibility, but a permanently locked test may miss a newly released compatible dependency that breaks a reader's fresh install. Consider one reproducible release check and a separate scheduled fresh-resolution check within supported constraints. Report which one failed. Do not silently update dependency versions or snapshots without checking whether the resulting behavior remains the documented contract.

Type-check TypeScript snippets

A TypeScript example can fail before execution because an export, overload, generic constraint or configuration changed. Put a complete example in a temporary project with the intended package and TypeScript version, then run the project's configured compiler, commonly npx tsc --noEmit. Ensure a tsconfig.json includes the extracted file and reflects the module resolution and library settings the documentation expects. Inspect compiler output and confirm that the intended file was included.

Type-checking is distinct from executing JavaScript. --noEmit checks types and configuration without producing runnable output. To test runtime behavior, compile with a suitable configuration or use a documented execution tool, then run the resulting program against the same package. Record both checks. A snippet can satisfy the compiler and still fail at runtime, and JavaScript can run while its published declaration files reject a valid use.

For snippets illustrating a deliberate type error, make the expected failure explicit and verify the relevant diagnostic rather than treating any compiler failure as success. A missing dependency or bad compiler configuration should not count as the intended error. Likewise, a fragment requiring surrounding declarations should say so; a hidden harness can validate an idea without proving the block is independently copyable.

Test compiler and Node versions that the project supports. TypeScript module resolution settings interact with package exports and module format, so use representative consumer configurations rather than assuming the repository's internal configuration is universal. Where examples rely on DOM or other ambient types, state that requirement and include the correct libraries in the test environment.

Extract examples without changing their meaning

Use a format-aware extractor for Markdown or MDX, and retain page and line information. Identify language tags, filenames, expected output, tabs and includes. An example split across blocks may need shared state; alternatives should start independently. Preserve the order and context a reader sees, and avoid executing output blocks or placeholder fragments as though they were programs.

Check stable output exactly when it is part of the promise. For generated IDs or network responses, assert stable properties and explain what varies. Prefer controlled local fixtures for repeatable tests, but record their limitations. An example involving a live service needs a separately authorized integration check if live behavior is the claim. Never expose credentials in the page or logs.

Review what the extractor skipped, including unselected tabs and unsupported syntax. Compare the rendered page to the source when a component or include changes the snippet. For the operational consequences of gaps, see why broken examples hurt adoption.

Compare output without hiding changes

Output can be a contract, an illustration or a partial excerpt. The page should tell readers which. A stable string or a required field can be compared exactly; a generated identifier, timestamp or unordered collection may need a narrower assertion.

Shown output Useful check Review question
Deterministic calculation Compare the exact value Is formatting part of the promise?
Generated identifier Check presence and expected type or form Does the page explain that it varies?
Timestamp Check the relevant field and meaning Is the timezone or format significant?
Unordered results Compare the relevant members Is ordering actually documented?
Error message Check the failure status and meaningful message Does the reader need the exact wording?
Partial output Check the shown properties and label omissions Could an ellipsis be mistaken for literal output?

Normalize only known variable fields. Removing every differing line can hide a renamed field or a missing error. Keep expected and actual evidence so a reviewer can decide whether the documentation, the implementation or the comparison should change. If the README shows a representative response, say which values are examples and which shape or fields the user should rely on.

Output also includes files, exit codes and side effects. A command that prints success but returns a failure status can break a script; a quiet command can succeed. Check the behavior the page describes rather than assuming that text on stdout is the only useful result.

Handle credentials, interaction and external state

Not every README example should run in the same unattended job. A command may require a secret, open a browser, wait for input, start a long-running server or modify a remote resource. Identify these cases and select an appropriate separate check. Do not put real keys in documentation, fixtures or logs.

Case Safer coverage Remaining question
Credential placeholder Check the configuration shape with harmless values Does an authorized live flow work?
Interactive prompt Check an available non-interactive path separately Is the interactive path still understandable?
Long-running process Test a bounded readiness condition and stop it Does the full workflow behave as described?
State-changing command Use an isolated disposable target where appropriate Was cleanup verified?
Remote dependency Use a fixture for local behavior and record its scope Does the deployed dependency agree?
Platform-specific command Run on the relevant platform or record a gap Which supported platforms remain untested?

A timeout is evidence to investigate, not an automatic verdict that the README is wrong. The process may be waiting for a prompt, an external service may be unavailable, or the command may be intended to remain running. Preserve the command and environment with the result. For credentialed behavior, use a separate authorized environment and a narrowly scoped test account when appropriate.

DocsAlive classifies examples needing credentials rather than executing them with real keys, including customer-provided test credentials. Interactive, graphical, destructive and long-running commands need a deliberate separate strategy; a skip or timeout does not establish whether the reader's full path works.

Put the result in context

A green run describes a particular set of examples, an environment and a point in time. Keep those details with the result: the documentation revision, artifact and version, operating system, runtime, dependency source, and commands used to set up the environment. Record the examples that were executed, checked without execution, skipped or left outside the selection. This makes the result useful when a reader reports a different outcome later.

Separate a broken example from a test infrastructure problem, but investigate both. A registry outage can block a valid install; an incorrect package name can look similar in a short log. Preserve the original error and identify the failing step. If a retry succeeds, retain enough information to see that the first attempt failed. Repeated intermittent failures can still affect readers even when a later retry works.

Give each failure an owner and a path to resolution. A documentation change may be the right fix when the product behavior is intentional; a product change may be needed when the documented behavior is the supported contract. If the release proceeds with a known gap, record the affected path, reason, owner and follow-up date. A hidden skip is not the same as an explicit decision to accept a limitation.

Make checks part of release work

Run fast, relevant checks when a change is proposed, and run the complete release selection against the candidate artifact before publishing. The GitHub Actions guide explains how pull request checks and scheduled runs answer different questions. Scheduled runs are useful for detecting changes in external dependencies or installation paths even when no documentation file has changed.

A release gate needs a named decision maker, an identified artifact and an agreed failure policy. If the suite only runs after publication it can detect an issue, but it cannot prevent that release. After publication, a separate check of the public package or endpoint can verify the path readers actually use. Preserve the distinction between candidate and published checks in the report.

A matrix should reflect supported promises rather than every possible combination. Start with the primary onboarding path and the oldest and newest supported runtimes where appropriate. Add operating systems, package managers or version combinations when the documentation promises them or past failures justify them. Show the matrix and its omissions to reviewers, so a single successful Linux run is not mistaken for proof of every platform.

Keep setup explicit. A fixture, mock server or prepared file may be necessary for a repeatable test, but it can also conceal a prerequisite that the page fails to explain. List what the harness supplies and compare it with what the reader sees. If a local fixture replaces a remote service, a pass establishes behavior against that fixture, not availability or compatibility of the live service.

Read reports and improve coverage

A useful report identifies the page, example, category, expected behavior, actual result and environment. Review skips and warnings alongside failures. A syntax check, a type check and an execution check establish different things; label each accurately. An example that runs and exits successfully may still return the wrong value or write a file somewhere unexpected, so check the behavior the surrounding text promises.

Avoid changing expected output simply to make a run green. First decide whether the product behavior is correct for the target release and whether the changed output affects the reader's task. Compare stable values exactly when possible. For timestamps, generated identifiers and unordered results, assert stable properties and explain variability. Normalize only what is truly variable, since a broad replacement can hide a meaningful regression.

Track coverage as an inventory, not just a percentage. Record the important paths that have no automated check, their owners and the reason. A credentialed example may need a separate authorized integration environment; an interactive setup may require a manual rehearsal. Assign an owner and review the list each release. Do not put real credentials in documentation, logs or public pull requests.

How DocsAlive fits

DocsAlive extracts supported fenced examples and runs or checks them in page order, with a fresh container for each page. It selects one variant from each tab group. Python, JavaScript on Node 20, simple TypeScript, shell and curl or HTTP examples have the coverage described in the framework reference. Compiled-language examples are recognized but not executed yet, and credential-dependent examples are classified rather than executed with real keys.

Read report.md and report.json with the findings reference. A pass can represent execution or a syntax check, and skipped blocks and unselected tab variants remain coverage gaps. A person still needs to review the explanation, the reader journey and any behavior the automated checks do not cover. When a model provider is configured, DocsAlive can draft a small fix and re-run the fixed page, retaining the fix only if it passes; fix pull requests are drafts and are designed not to merge automatically.

See pricing for DocsAlive plans: Team is $299 per month and Growth is $699 per month. To discuss coverage for your documentation, contact us.

If your JavaScript docs are published with Docusaurus, see testing Docusaurus documentation examples.

For TypeScript examples specifically, see testing TypeScript documentation examples.

Keep reading
Outlined documentation pages with a lime pulse line running out of the stackGuide

Docs Testing: How to Test the Code Samples in Your Docs

Why code examples in documentation break, the ways teams test them (doctest, docs-as-tests, CI runners) and how to run every sample on each release.

Two lenses held over the same documentation page, each bringing a different layer into focusComparison

Vale vs markdownlint: What Each Docs Linter Catches

Vale checks prose style; markdownlint checks Markdown structure. What each catches, sample configs, running both in CI and what neither one tests.

Blueprint of two documentation pages: one with a handwritten test script, one whose code blocks are framed and checkedComparison

Doc Detective vs DocsAlive: Testing the Code in Your Docs

Doc Detective is a free, open-source docs-as-tests framework; DocsAlive is a hosted docs CI for code examples. Features, price, setup and when to use each.

A machine blueprint above a ruled documentation page, with a caliper measuring between themComparison

Promptless Alternative: Promptless vs DocsAlive Compared

Promptless drafts docs updates from what you ship; DocsAlive runs the code examples you've published. Features, pricing, security and when to use each.

A long workbench of outlined testing instruments, each with a small lime light, beside a stack of documentation pagesGuide

Best Free Tools to Test Code Examples in Documentation

Free, open-source tools that run the code examples in your docs: rustdoc, Go examples, doctest, pytest plugins, Sphinx doctest, Doc Detective and byexample.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Testing SDK Docs Before Every Release: A Release-Gate Guide

Test SDK and client-library docs against the package you ship: build the wheel or npm tarball, install it clean, run every example, and gate the release in CI.

Outlined documentation pages with a lime pulse line running out of the stackGuide

How to Test Quickstarts and Tutorials in Your Docs

Test the onboarding path a new reader follows: run quickstarts in a clean environment, in page order, sort real bugs from credentials and fragments, and wire it into CI.

Outlined documentation pages with a lime pulse line running out of the stackGuide

How to Test CLI Documentation: Commands, Flags and Output

Test the commands a CLI page promises: install in a clean container, run examples in order, compare flags with --help, check exit codes and output, and run it in CI.

Outlined documentation pages with a lime pulse line running out of the stackGuide

How to Test API Reference Docs: Requests, Responses and SDKs

Test API reference pages against the contract they publish: methods, parameters, request and response examples, SDK snippets, status codes and OpenAPI samples.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Docs testing for API-first companies

Keep API quickstarts, SDK snippets and curl examples working across releases.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Docs testing for CLI and developer-tool vendors

Test developer-tool install paths, package managers and command examples across releases.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Testing README Code Examples: Install, Usage and CI

Keep README install commands and usage examples working with clean environments, output checks and CI.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Testing Notebooks and Executable Docs: Cells, Kernels and Output

Test notebooks in fresh kernels, check meaningful output and manage dependencies, data and drift in CI.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Testing Docs in GitHub Actions: Pull Requests and Scheduled Checks

Run documentation example checks in GitHub Actions with practical guidance on schedules, path filters, caching, secrets and reports.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Testing Code Examples in MkDocs, Docusaurus and Sphinx Sites

Find and test documentation examples in Markdown, MDX and reStructuredText, alongside site builds and link checks.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Python Docs Testing: Doctest, Pytest and Markdown

Test Python documentation with doctest, pytest, Markdown example execution and isolated environments.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Testing Go documentation examples

Test Go example functions and Markdown snippets against the intended module versions in clean environments.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Sphinx Docs Testing: doctest, code-block and MyST

Test Sphinx documentation examples: what the doctest and linkcheck builders prove, code-block and literalinclude, MyST fences, and what a Markdown scan covers.

Outlined documentation pages with a lime pulse line running out of the stackGuide

ReadMe Docs Testing: CLI, OpenAPI and Code Examples

Test ReadMe documentation examples: docs-as-code and bi-directional sync, what lint and oas:validate prove, Tabs variants and runnable code.

Outlined documentation pages with a lime pulse line running out of the stackGuide

TypeScript Docs Testing: tsc --noEmit and Runtime

Test TypeScript documentation examples: type-check snippets with tsc --noEmit in a consumer project, then run them, and what each check proves.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Docusaurus Docs Testing: Code Blocks, Tabs and MDX

Test Docusaurus documentation examples: builds vs runnable code, code block metadata, Tabs install variants, MDX pages and clean-container runs.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Mintlify Docs Testing: CodeGroup, Tabs and the CLI

Test Mintlify documentation examples: docs.json, MDX pages, CodeGroup and Tabs variants, what mint broken-links and mint validate prove, and clean runs.

Outlined documentation pages with a lime pulse line running out of the stackGuide

MkDocs Docs Testing: Code Examples, Tabs and Snippets

Test MkDocs documentation examples: strict builds, Material tabs, Snippets includes, mkdocstrings pages and clean-container runs.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Testing Installation Instructions: pip, npm and Containers

Test documented install steps in clean environments: pip and extras, venv, npm install vs npm ci, containers and the first import.

Outlined documentation pages with a lime pulse line running out of the stackGuide

AI SDK Docs Testing: LLM Examples, Keys and Drift

Test AI and LLM SDK documentation examples: requests, responses, streaming, tool calls, credentials, model retirements and CI.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Testing migration guides and changelog examples

Test before and after examples against the right releases and make migration documentation part of the release gate.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Docs Testing Checklist: Code Examples Before Release

A practical pre-release checklist for testing documentation examples, reviewing gaps and verifying published packages.

Outlined documentation pages with a lime pulse line running out of the stackGuide

The Cost of Broken Code Examples in Documentation

Understand how broken documentation examples create friction in developer adoption and support, and how to reduce it.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Testing Rust documentation examples

Test Rust rustdoc examples and cargo documentation tests, and keep native Rust checks because compiled Rust fences are recognized but not executed.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Testing VitePress documentation examples

Test VitePress Markdown examples separately from the site build, including code groups, includes and what a plain Markdown scan covers.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Testing docs in GitLab CI

Run documentation example checks in GitLab CI jobs, keep site builds separate from execution, and review what a scan covers and skips.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Testing JSON, TOML and YAML examples in docs

Test JSON, TOML and YAML config examples in documentation: parse, schema and tool checks, honest fence labels, fragments, CI jobs and what a scan covers.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Testing Java documentation examples

Test Java examples in READMEs, SDK docs and Javadoc: the source launcher, javac, JShell scripts, assertions, @snippet files, CI and what a scan covers.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Testing PHP documentation examples

Test PHP examples in docs: php -l lint, clean-directory runs, assertions with zend.assertions, Composer scratch projects, PHPUnit, CI and scan coverage.

Outlined documentation pages with a lime pulse line running out of the stackGuide

Testing docs examples that need API keys

Test credential-gated docs examples: honest placeholders, offline stubs and fixtures, live sandbox jobs, CI secrets and fork rules, and how a scan classifies them.

A machine blueprint above a ruled documentation page, with a caliper measuring between themGuide

Documentation Drift: How to Detect It Before Users Do

What documentation drift is, why code examples drift first, and practical ways to detect it: ownership, release checks, link and execution tests.

See which of your code samples still run.

Send us a public repository. Our team runs DocsAlive on its docs and emails you the real report within 2 business days. Free, no signup.