Test Example

A JSDoc plugin for generating test files by parsing @example.

NPM Dependency

NPM

Sponsor

Feature

  1. BDD (Behavior-driven development) style Test wrapper, such as Mocha

  2. Should style Assertion

  3. One Source file to one Test file

  4. One Doclet to one describe()

  5. One @example to one it()

  6. // also supports multiple-line code with the example result

Usage

  1. npm i test-example -D

  2. Add some config options

  3. Custom hook module let you do more

  4. jsdoc -c path/to/config.json

  5. npm test

Example

source/say-sth.js

/**
 * Hello function
 *
 * @function hello
 *
 * @param {string} [name="World"]
 *
 * @return {string}
 *
 * @example  // No parameter
 *
 *     hello()    //  "Hello, World !"
 *
 * @example  // One parameter
 *
 *     hello('JSDoc')    //  "Hello, JSDoc !"
 */

exports.hello = function (name) {

    return  'Hello, ' + (name || 'World') + ' !';
};

will generate test/say-sth.js

'use strict';

require('should');

var saySth = require('../source/say-sth');


describe('hello',  function () {


    it('No parameter',  function () {

        var result = saySth.hello();

        result.should.be.deepEqual( "Hello, World !" );
    });


    it('One parameter',  function () {

        var result = saySth.hello('JSDoc');

        result.should.be.deepEqual( "Hello, JSDoc !" );
    });

});

[ Remark ] A blank line is necessary between the title & code of @example.

User Case

  1. iQuery.js