JS-Doctest
Run JSDoc style doc examples as tests within your test suite
Inspired by Elixir Doctests
Write a function with a JSDoc style documentation
/** * Returns fairly unnecessary and uninteresting information about a string * @param * @return * @example * stringData('woah') * //=> * { * length: 4, * vowels: 2, * consonants: 2, * } */ { const vowels = string length; return length: stringlength vowels: vowels consonants: stringlength - vowels ;}
Import the doctest function in your test suite and point it at the file.
; ;
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.
;
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',
}
;