Giter Site home page Giter Site logo

markdown-doctest's Introduction

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 embarrassing 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? 😉

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:

markdown-doctest's People

Contributors

bebraw avatar greenkeeper[bot] avatar greenkeeperio-bot avatar kylesnowschwartz avatar raquelxmoss avatar simonwep avatar singingwolfboy avatar staltz avatar widdershin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

markdown-doctest's Issues

JSX support

Right now markdown-doctest:

  1. Ignores jsx language tag.
  2. Don’t understand JSX even if js language tag is used.

Allow customization of the line parser

We have some custom code in the examples at the maintenance book we are writing (incidentally we'll cover your tool there as well so expect more people to find it). The problem is that there's no way to skip the custom code while processing.

I had a quick look and it seems we would have to extend the line parser so that you can pass a custom rule to it. We could skip lines containing ... etc. there. This would allow us to check the book code using your tool.

We are disabling the broken examples for now but a parser level solution would be better.

Custom location of .markdown-doctest-setup.js

Currently there's no other way to use a custom- name/location of the config file. If you already have a large amount of config files it'd nice to have a option like --config <path> where you can specify a custom config-module.

Great module btw! Is it still maintained?

Upgrade to Babel 6

$ ./node_modules/markdown-doctest/bin/cmd.js
/Users/nickj/Projects/cycle-animation-driver/node_modules/markdown-doctest/node_modules/babel/node_modules/babel-core/lib/transformation/file/options/option-manager.js:126
      if (!option) this.log.error("Unknown option: " + alias + "." + key, ReferenceError);
                           ^

TypeError: Cannot read property 'error' of undefined
    at OptionManager.mergeOptions (/Users/nickj/Projects/cycle-animation-driver/node_modules/markdown-doctest/node_modules/babel/node_modules/babel-core/lib/transformation/file/options/option-manager.js:126:28)
    at OptionManager.addConfig (/Users/nickj/Projects/cycle-animation-driver/node_modules/markdown-doctest/node_modules/babel/node_modules/babel-core/lib/transformation/file/options/option-manager.js:107:10)
    at OptionManager.findConfigs (/Users/nickj/Projects/cycle-animation-driver/node_modules/markdown-doctest/node_modules/babel/node_modules/babel-core/lib/transformation/file/options/option-manager.js:168:35)
    at OptionManager.init (/Users/nickj/Projects/cycle-animation-driver/node_modules/markdown-doctest/node_modules/babel/node_modules/babel-core/lib/transformation/file/options/option-manager.js:229:12)
    at compile (/Users/nickj/Projects/cycle-animation-driver/node_modules/markdown-doctest/node_modules/babel/node_modules/babel-core/lib/api/register/node.js:117:22)
    at normalLoader (/Users/nickj/Projects/cycle-animation-driver/node_modules/markdown-doctest/node_modules/babel/node_modules/babel-core/lib/api/register/node.js:199:14)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/nickj/Projects/cycle-animation-driver/node_modules/markdown-doctest/node_modules/babel/node_modules/babel-core/lib/api/register/node.js:216:7)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)

As a user, I want to prevent examples from being run

There should be some way to alter the markdown to prevent an example from being run.

Currently the only flexibility I've found is in the number of backticks you place before the language.

So maybe use 4+ backticks to disable the example?

Work alongside existing babel 6 installation

Thanks for markdown-doctest 😄

I tried to start converting most.js's unit tests to allow ES6 using babel 6 and babel-register. The buster.js unit tests run, but markdown-doctest fails. I figure this is related to #19. Here's a branch that's setup to use babel 6 to run the unit tests, and with no changes (vs. master) to the markdown-doctest version or config. And Here's the error output of running npm test.

Is this be possible now, or will it require #19?

Thanks!

Support expecting failure from example

Some examples exist to demonstrate a failure can occur.

It would be nice if we could prepend these examples with <!-- expect-error --> or something like that so we don't need to skip them. Possibly we could specify the expected error message but that is a step beyond how the rest of the comment instructions work.

A typo in the README.md

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!

embarrassing ;)

As a user, I want to run setup code before my examples

I need a place to import all of the implicit dependencies used in my example.

It should be checked into source control.

Perhaps a .markdown-doctest file that is just a Javascript file that gets executed.

So the one for RxJS might look like:

var RxJS = require('.');

No red

Just a tiny feedback, when I get the report:


Passed: 10
Skipped: 6
Failed: 0

Failed: 0 is in red. The color itself makes me alerted. I don't know, it's something about red. Perhaps it could be gray/white if ===0 and red if >0 ?

Add markdown-doctest --init

It would be nice to have a command that creates a nice default config, perhaps with comments explain what things do, a la Rails.

This could be interactive, and I could imagine it running markdown-doctest, then helping you set up any imports or globals required.

Allow writing config file in es6, coffee, typescript

My suggestion is to make use of https://github.com/js-cli/js-interpret like it's done in webpack. For example, in order to enable babel transpiling of the config, user will have to do following:

  1. npm install babel-register
  2. configure .babelrc
  3. rename config to .markdown-doctest-setup.babel.js

I can work on it and provide PR if you are ok with this change.

For those who came here looking for a solution, quick fix is to follow steps from above by putting this to .markdown-doctest-setup.js:

  require('babel-register')
  module.exports = require('./.markdown-doctest-setup.babel.js') 

Merging with mos

I've created a package that generates and tests markdown files to keep them up to date. It is called mos

It has several plugins and the mos-plugin-example is meant to do the same as markdown-doctest.

Currently it can run js and es6, test the example files and combine the output of the examples with the code that outputs them.

For instance, this example:

console.log('Hello world!')

Is transformed into this example in the markdown:

console.log('Hello world!')
//> Hello world!

IMHO, we can unite efforts to make these toolings better and more popular.

An in-range update of babel7 is breaking the build 🚨


☝️ Important announcement: Greenkeeper will be saying goodbye 👋 and passing the torch to Snyk on June 3rd, 2020! Find out how to migrate to Snyk and more at greenkeeper.io


There have been updates to the babel7 monorepo:

    • The dependency @babel/core was updated from 7.8.7 to 7.9.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

This monorepo update includes releases of one or more dependencies which all belong to the babel7 group definition.

babel7 is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.