Coder Social home page Coder Social logo

deltazero-cz / jsdoctest Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yamadapc/jsdoctest

0.0 0.0 0.0 2.56 MB

Run jsdoc examples as doctests.

Home Page: https://yamadapc.github.io/jsdoctest

License: MIT License

JavaScript 97.08% Shell 2.43% Makefile 0.49%

jsdoctest's Introduction

jsdoctest

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

jsdoctest's People

Contributors

deltazero-cz avatar suddi avatar yamadapc avatar

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.