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

jsdoctest

v1.7.1

Published

Run jsdoc examples as doctests

Downloads

120

Readme

Build Status Coverage Status Stories in Ready Dependency Status devDependency Status npm downloads npm version Gitter


jsdoctest parses jsdoc @example tags from annotated functions and runs them as if they were doctests.

Inspired by the doctest python library, as well as its doctestjs javascript implementation.

Demo

demo

Set-up

Here's a two line set-up you can use:

$ npm i -g jsdoctest && jsdoctest --init
Adding `jsdoctest` script to your package.json...
Installing `mocha` and `jsdoctest` with npm:
# ... npm doing some work...
You can now run doctests with `npm run jsdoctest` or `npm test`

This will add sensible defaults to your package.json which you can then edit.

Test-case Format

Examples need to be valid javascript, followed by a comment with the string => prefixing the results:

/**
 * @example
 *   returns10()
 *   // => 10
 *   returns20()
 *   // => 20
 */

It doesn't matter if the comment is on the same line or the next one, so the following is also valid:

/**
 * @example
 *   returns10() // => 10
 *   returns20()
 *   // => 20
 */

Async test cases are supported prefixing the expected results with the async => string and pretending to have the cb callback function.

/**
 * @example
 *   takesCallbackAndYields10('here', cb)
 *   // async => 10
 *   takesCallbackAndYields20('here', cb)
 *   // async => 30
 */

Promises are also supported, just add the same // async => prefix and be sure not to use a variable named cb on your text expression.

/**
 * @example
 *   returnsPromiseThatYields10('here')
 *   // async => 10
 */

Examples

The examples directory has a couple of examples, which may be useful. Better documentation will be added if the project raises in complexity.

Usage

The recommended way of using jsdoctest is to use mocha. That is made possible with:

npm i mocha jsdoctest
mocha --require jsdoctest <module-name>

There's also a rudimentary command-line interface, which can be ran with:

npm i jsdoctest
jsdoctest <module-name>

Disabling

To disable running jsdoctests, while still requiring it with mocha (I don't know why, but you may) you can set the JSDOCTEST_DISABLE environment variable to anything (JSDOCTEST_DISABLE=true mocha --require...).

License

This code is licensed under the MIT license for Pedro Tacla Yamada. For more information, please refer to the LICENSE file.

Donations

Would you like to buy me a beer? Send bitcoin to 3JjxJydvoJjTrhLL86LGMc8cNB16pTAF3y