Giter Club home page Giter Club logo

doctest's Introduction

doctest

Inline unit testing of JS code

This approach is inspired by the Python doctest library enabling the developer to run small unit tests from a docstring comment next to the code, which is a quick and handy way to do unit testing.

Doctests are easy to read, they can work as human-readable examples giving insights to the module's use cases and can be regarded as an extension to the module's documentation (such as JSDoc).

They are also easy to write, and in the process of development, they can be immediately spotted without the need to look through other files and folders. And if you remove a module file, the doctests are immediately removed as well.

Doctests are also a shorter path to test-driven development.

Example

import ComplexNumber from './ComplexNumber';

// @test expect(abs(new ComplexNumber(-3, 4))).toEqual(5);
// @test expect(abs(new ComplexNumber(1, -1))).toEqual(Math.sqrt(2));

// @test Absolute value of imaginary unit is 1
// import IM_1 from './IM_1';
// expect(abs(IM_1)).toEqual(1);

export default function abs(z) {
    if (!(z instanceof ComplexNumber))
        throw new Error('Invalid argument: should be a ComplexNumber');
    return Math.sqrt(z.re*z.re + z.im*z.im);
}

+ Unit testing of class methods

Usage

(With jest as an example)

$ npx doctest build ./src/**/*.js
$ npx jest
$ npx doctest cleanup

or in a one-liner suitable for a package.json script:

"test": "npx doctest build ./src/**/*.js && npx jest && npx doctest cleanup",

Under the hood

Doctest generates temporary test files based on the content of the comments in the code and cleans up these files, once a test run is over. This is represented by the commands npx doctest build and npx doctest cleanup. The list of test files generated in the build phase is stored in the temporary .doctestdump file which can be used during the test phase.

For the sake of separation of concerns, the job of running the generated tests is entrusted to a dedicated unit testing utility (such as jest in the setting above) which is not part of this package. The choice of the testing utility is up to the developer's preference.

Options

The doctest output format can be customized via an optional doctest.config.js file (which should resemble the default doctest.config.js). The config will be picked either from the application root or from the location specified in the optional -c <config_path> command line argument.

Installation

$ npm i -D github:axtk/doctest

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.