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

markdown-doctest

v1.1.0

Published

Test all the code in your markdown docs!

Downloads

10,475

Readme

npm version Build Status Greenkeeper badge


markdown-doctest

Test all the code in your markdown docs!

Why on earth?

As an open source developer, there are few things more embarassing than a user opening an issue to inform you that your README example is broken! With markdown-doctest, you can rest easy knowing that your example code is actually runnable.

Installation

Just npm install markdown-doctest and run markdown-doctest. It will run all of the Javascript code examples tucked away in your markdown, and let you know if any blow up.

Okay, how do I use it?

Let's try it on this repo!

var a = 5;

var b = 10;

console.log(a + c);

There's a problem with that example. markdown-doctest finds it for us:

$ markdown-doctest
x..

Failed - README.md:32:17
evalmachine.<anonymous>:7
console.log(a + c);
                ^

ReferenceError: c is not defined

Awesome! No excuse for broken documentation ever again, right? :wink:

We can also run specific files or folders by running markdown-doctest with a glob, like markdown-doctest docs/**/*.md. By default markdown-doctest will recursively run all the .md or .markdown files starting with the current directory, with the exception of the node_modules directory.

Note: markdown-doctest doesn't actually attempt to provide any guarantee that your code worked, only that it didn't explode in a horrible fashion. If you would like to use markdown-doctest for actually testing the correctness of your code, you can add some asserts to your examples.

markdown-doctest is not a replacement for your test suite. It's designed to run with your CI build and give you peace of mind that all of your examples are at least vaguely runnable.

So how do I write those examples?

In your markdown files, anything inside of code blocks with 'js' or 'es6' will be run. E.g:

```js
console.log("Yay, tests in my docs");
```

```es6
const a = 5;
console.log({a, foo: 'test'});
```

I have a code example I don't want tested!

You can tell markdown-doctest to skip examples by adding <!-- skip-example --> before the example. E.g:

<!-- skip-example -->
```js
// not a runnable example

var foo = download(...);
```

How do requires work? And other setup logic?

You can require any needed modules or example helpers in .markdown-doctest-setup.js. E.g:

// .markdown-doctest-setup.js
module.exports = {
  require: {
    Rx: require('rx')
  },

  globals: {
    $: require('jquery')
  }
}

Anything exported under require will then be used by any examples that require that key. You must explicitly configure all of the dependencies used in your examples.

Anything exported under globals will be available globally across all examples.

You can also specify a regexRequire section to handle anything more complex than an exact string match!

// .markdown-doctest-setup.js
module.exports = {
  require: {
    Rx: require('rx')
  },

  regexRequire: {
    'rx/(.*)': function (fullPath, matchedModuleName) {
      return require('./dist/' + matchedModuleName);
    }
  }
}

Do I have to enable es6 support?

Nope, ES6 support is on by default. You can disable babel support in your .markdown-doctest-setup.js file. This will speed things up drastically:

//.markdown-doctest-setup.js
module.exports = {
  babel: false
}

What if I have global state that needs to be reset after my examples run?

//.markdown-doctest-setup.js
module.exports = {
  beforeEach: function () {
    // reset your awesome global state
  }
}

You can specify a function to be run before each example in your .markdown-doctest-setup.js.

What if I want to remove custom syntax from examples before processing?

//.markdown-doctest-setup.js
module.exports = {
  transformCode(code) {
    // Remove ... from code syntax
    return code.replace(/\.\.\./g, "");
  }
}

Who uses markdown-doctest?

All of these projects either run markdown-doctest with npm test or as part of their CI process: