Your JS README says "// Foo-Ba-Rb". The code says Foo-BA-Rb.

2026-11-29来源 dev.toEnglish

JavaScript READMEs have a convention Python doesn't: show a call, then put the expected result in a comment on the next line.

camelCase("foo-bar_baz"); // fooBarBaz 

It reads nicely. It's also an untested claim, sitting right where every new user copies from.

In a previous post I ran every >>> example in a Python library's README as a doctest. This time I wanted the same check for the JS convention, so I wrote a small script and pointed it at scule, a tidy string-case utility from the UnJS ecosystem.

The check: about 70 lines, no dependencies

The idea is simple:

  1. Find fenced js/ts blocks in the Markdown.
  2. Inside them, find an expression line followed directly by a // result comment.
  3. Import the library, evaluate the expression with its exports in scope, and compare String(result) with the comment.
const FENCE = /^```(?:js|javascript|ts|typescript|mjs)\b[^\n]*\n([\s\S]*?)^```\s*$/gm; // for each pair of lines: code; // expected got = run(...names.map((n) => mod[n]), expr); ok = String(got) === expected || isDeepStrictEqual(got, JSON.parse(expected)); 

The result: 8 of 9

$ node md_jscheck.mjs README.md --module scule PASS | README.md:37 | pascalCase("foo-bar_baz") | docs: FooBarBaz | got: FooBarBaz ... FAIL | README.md:86 | trainCase("FooBARb") | docs: Foo-Ba-Rb | got: Foo-BA-Rb ... TOTAL: 8/9 passed 

(scule 1.3.0, Node 26.)

That one failure is a nice example of how docs drift. The README's own note, two lines below the example, says that an uppercase letter followed by more uppercase letters is preserved (WWWAuthenticate becomes WWW-Authenticate). So the code is doing exactly what the notice describes, and the example contradicts its own documentation. Someone already spotted it: there's an open pull request with the fix (unjs/scule#133, opened in July). A check like this in CI would have caught it on the day it changed.

What a comment-style check can't see

The same README's Development section says to enable Corepack with corepack enable. On the Node 26 install I tested, there is no corepack command at all. You'd need to install it separately first. No code-block checker will find that. It's prose. That's the split I keep running into:

  • Examples with results: cheap to verify automatically. Put them in CI.
  • Setup instructions and prose claims: need a fresh environment and a human (or an agent told to actually run things), with each claim marked as verified, read-from-code, or assumed.

Takeaways

  1. If your README uses // result comments, they are tests you're not running. Run them.
  2. When an example contradicts a note right next to it, the note usually came later. Check the example first.
  3. Keep setup instructions honest by running them in a clean environment every release. Tooling defaults (like whether Node ships Corepack) change underneath you.

Disclosure: this post and the script were produced by an AI agent (Claude) working on my behalf; every command and result shown was actually run. The Python version is free and open source: md-doctest. The JS checker is part of my paid Docs That Run toolkit, together with templates for verification logs and audits. Or I can check your README for you.

原文:https://www.a8q9.cn/en/article/your-js-readme-says-foo-ba-rb-the-code-says-foo-ba-rb-3294b0.html

相关文章