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

jsdoc-test

v0.0.10

Published

Run doc examples as tests

Downloads

4

Readme

JS-Doctest

Run JSDoc style doc examples as tests within your test suite

Inspired by Elixir Doctests

npm version Build Status

Write a function with a JSDoc style documentation

/**
 * Returns fairly unnecessary and uninteresting information about a string
 * @param {string} string - The string of disinterest
 * @return {object} Useless information
 * @example
 * stringData('woah')
 * //=>
 * {
 *   length: 4,
 *   vowels: 2,
 *   consonants: 2,
 * }
 */
export function stringData(string) {
  const vowels = string
    .toLowerCase()
    .split('')
    .filter((char) => {
      return ['a', 'e', 'i', 'o', 'u', 'y'].find((v) => char === v);
    }).length;
  return {
    length: string.length,
    vowels: vowels,
    consonants: string.length - vowels,
  };
}

Import the doctest function in your test suite and point it at the file.

import doctest from 'jsdoc-test';

describe('stringData Doctests', () => {
  doctest('src/string/index.js'); // path is relative to root of directory
});

And this will run and test all instances of @example in the given file

Notes:

  • The only JSDoc component you need is the @example.
  • You can have as many @examples as you'd like for any one function.
  • Example function calls and return values can span multiple lines.
  • Currently only works for exported functions

Testing Function

By default, doctest will use a basic chai expect as its testing function.

it(`${functionName} should match its doc example value`, () => {
  expect(actualReturnValue).to.eql(expectedReturnValue);
});

But this function can be overridden by any function that takes three arguments:

(actualValue, expectedValue, doctestObject, doctestIndex).

Where the doctestObject is the parsed doctest that looks like:

{
  resultString: 'titleize('WoAh')',
  stringToEval: 'Woah',
}
describe('stringData Doctests', () => {
  doctest('src/string/index.js', {
    testingFunction: (actual, expected, doctestObject, doctestIndex) => {
      if (actual === expected) console.log(functionName + ', you did it!');
      else console.log('Better luck next time, ' + functionName);
    },
  });
});