npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

markunit

v1.0.7

Published

A simple assertion library for unit testing Markdown

Downloads

7

Readme

Markunit

A simple assertion library for unit testing Markdown.

Build Status Version Depfu License

Wait a second...

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.

Installation

Get the package from npm:

$ npm install markunit -D

Then require the package and load your Markdown document:

var markunit = require("markunit");
var markdown =
  "# Test document\nThis is _example_ Markdown content for `markunit.js` documentation.";
var doc = markunit(markdown);

Renditions

There are five different renditions of the original Markdown content that can be analyzed: source, rendered, copy, code, and markup. Each of them possesses a .has() and a .no() method.

Source

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."
doc.source.has("# Test document"); // pass
doc.source.no("Doesn't exist"); // pass

Rendered

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>
doc.rendered.has("<h1>Test document</h1>"); // pass
doc.rendered.no("<h1>Not the title</h1>"); // pass

Copy

Check for the presence or absence of patterns in contents of the doc, excluding code elements:

// Will look for text matches in "Test document This is example Markdown content for" and "documentation."
doc.copy.has("This is example Markdown"); // pass
doc.copy.no("markunit.js"); // pass

Code

Check for the presence or absence of patterns in contents of only the code elements:

// Will look for text matches in "markunit.js"
doc.code.has("markunit.js"); // pass
doc.code.no("mark-unit.js"); // pass

Markup

Check for the presence or absence of jquery-style selectors:

// Will look for structural matches in "<h1></h1><p><em></em><code></code></p>"
doc.markup.has("p"); // pass
doc.markup.no("li li"); // pass

Patterns

For the source, rendered, copy, and 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.

doc.source.has("test"); // pass
doc.source.has(/t.?*t/); // pass
doc.source.has(["test", /Not present/]); // pass

Ignoring

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.

doc.ignore("content");
doc.source.no("content"); // pass

Example Usage

The below demonstrates a simple setup and test suite for the typical repository README, using a framework like Mocha.

// test/test.js

var markunit = require("markunit");
var fs = require("fs");

var input = fs.readFileSync("../README.md", "utf8");
var readme = markunit(input);
readme.ignore("It’s spelled MyLibrary, not my-library.");

describe("README", function() {
  it("should have a title", function() {
    readme.markup.has("h1");
  });
  it("should not contain double-indented lists", function() {
    readme.markup.no("li li");
  });
  it("should not have any raw HTML in the source", function() {
    readme.source.no(/<.*?>.*?<\/.*?>/);
  });
  it("should have a link to npm", function() {
    readme.rendered.has(/<a.*?href="https:\/\/npmjs\.com/);
  });
  it("should not spell the library name with a hyphen", function() {
    readme.copy.no("my-library");
  });
  it("should not have any curly quotes in code snippets", function() {
    readme.code.no(["“", "”"]);
  });
  it("should not reference old API calls", function() {
    readme.code.no([
      "deprecatedCall1(",
      "deprecatedCall2(",
      "deprecatedCall3("
    ]);
  });
  it("should contain installation instructions", function() {
    readme.code.has("npm install");
  });
});