A simple assertion library for unit testing Markdown.
Unit testing Markdown? Yeah, it might sound odd. But Markdown is code, and it can have bugs just like anything else. Maybe there are lingering references to an API call you’ve removed. Or you want to avoid double-level lists for stylistic reasons. Or perhaps contributors keep misspelling your library's name. Wouldn’t it be nice to catch those things automatically? Exactly. Markunit provides assert methods for analyzing your Markdown content and the HTML it creates, which can be run inside your favorite test runner and continuous integration system.
Get the package from npm:
$ npm install markunit -D
Then require the package and load your Markdown document:
var markunit =var markdown = "# Test document\nThis is _example_ Markdown content for `markunit.js` documentation."var doc =
There are five different renditions of the original Markdown content that can be analyzed:
markup. Each of them possesses a
.has() and a
Check for the presence or absence of patterns in the source Markdown:
// Will look for text matches in "# Test document\nThis is _example_ Markdown content for `markunit.js` documentation."docsource // passdocsource // pass
Check for the presence or absence of patterns in the rendered HTML:
// Will look for text matches in "<h1>Test document</h1><p>This is <em>example</em> Markdown content for <code>markunit.js</code>" documentation.</p>docrendered // passdocrendered // pass
Check for the presence or absence of patterns in contents of the doc, excluding
// Will look for text matches in "Test document This is example Markdown content for" and "documentation."doccopy // passdoccopy // pass
Check for the presence or absence of patterns in contents of only the
// Will look for text matches in "markunit.js"doccode // passdoccode // pass
Check for the presence or absence of
// Will look for structural matches in "<h1></h1><p><em></em><code></code></p>"docmarkup // passdocmarkup // pass
code renditions, the pattern to match can be a string, a regular expression, or an array of either. For the
markup rendition, the pattern can be a string or an array of strings, where each string represents a
jquery-style selector. A single match from an array is sufficient for the
.has() method to pass or the
.no() method to fail.
docsource // passdocsource // passdocsource // pass
A pattern can be excluded from review using
.ignore(), which will completely remove matches from the input Markdown. The pattern to ignore can be a string, a regular expression, or an array of either. Subsequent calls will overwrite the pattern, and passing
null will clear the pattern.
docdocsource // pass
The below demonstrates a simple setup and test suite for the typical repository README, using a framework like Mocha.
// test/test.jsvar markunit =var fs =var input = fsvar readme =readme