• Stars
    star
    165
  • Rank 228,906 (Top 5 %)
  • Language
    JavaScript
  • License
    Do What The F*ck ...
  • Created over 12 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Doctests for JavaScript (and CoffeeScript)

doctest

Doctests are executable usage examples sometimes found in "docstrings". JavaScript doesn't have docstrings, but inline documentation can be included in code comments. doctest finds and evaluates usage examples in code comments and reports any inaccuracies. doctest works with JavaScript and CoffeeScript modules.

Example

// toFahrenheit :: Number -> Number
//
// Convert degrees Celsius to degrees Fahrenheit.
//
// > toFahrenheit(0)
// 32
// > toFahrenheit(100)
// 212
function toFahrenheit(degreesCelsius) {
  return degreesCelsius * 9 / 5 + 32;
}

Doctest will execute toFahrenheit(0) and verify that its output is 32.

Installation

$ npm install doctest

Running doctests

Test a module via JavaScript API:

> doctest ({}) ('lib/temperature.js')

Test a module via command-line interface:

$ doctest lib/temperature.js

The exit code is 0 if all tests pass, 1 otherwise.

Supported module systems

Module system Option Node.js Dependencies
CommonJS commonjs βœ”οΈŽ βœ”οΈŽ
ECMAScript modules esm βœ”οΈŽ βœ”οΈŽ

Specify module system via JavaScript API:

> doctest ({module: 'esm'}) ('path/to/esm/module.js')

Specify module system via command-line interface:

$ doctest --module commonjs path/to/commonjs/module.js

Line wrapping

Input lines may be wrapped by beginning each continuation with FULL STOP (.):

// > reverse([
// .   'foo',
// .   'bar',
// .   'baz',
// . ])
// ['baz', 'bar', 'foo']

Output lines may be wrapped in the same way:

// > reverse([
// .   'foo',
// .   'bar',
// .   'baz',
// . ])
// [ 'baz',
// . 'bar',
// . 'foo' ]

Exceptions

An output line beginning with EXCLAMATION MARK (!) indicates that the preceding expression is expected to throw. The exclamation mark must be followed by SPACE ( ) and the name of an Error constructor. For example:

// > null.length
// ! TypeError

The constructor name may be followed by COLON (:), SPACE ( ), and the expected error message. For example:

// > null.length
// ! TypeError: Cannot read property 'length' of null

Scoping

Each doctest has access to variables in its scope chain.

Integrations

Running the test suite

$ npm install
$ npm test

More Repositories

1

Base64.js

Polyfill for browsers that don't provide window.btoa and window.atob
JavaScript
489
star
2

string-format

JavaScript string formatting inspired by Python’s `str.format()`
JavaScript
332
star
3

tutor

JavaScript interface for the Gatherer card database
CoffeeScript
149
star
4

xyz

Publish npm packages with fewer screw-ups
Shell
101
star
5

transcribe

πŸ“ Generate Markdown documentation from code comments
JavaScript
79
star
6

an.hour.ago

A small utility which enables wonderfully expressive date and time manipulation in JavaScript
CoffeeScript
69
star
7

nucleotides

The building blocks of JavaScript programs
JavaScript
32
star
8

airwaves

Broadcast on a dedicated frequency
CoffeeScript
25
star
9

mathmethods

A tiny script which makes methods of the `Math` object available to numbers by adding properties to `Number.prototype`
CoffeeScript
17
star
10

orthogonal

DSL for describing simple vector graphics
CoffeeScript
14
star
11

CANON

Canonical object notation
JavaScript
12
star
12

jQuery.localize

⌚ jQuery plugin for localizing dates and times via the `datetime` attribute of the HTML5 `<time>` element
JavaScript
8
star
13

hex2xterm

🌈 Convert hexadecimal colour codes to xterm colour codes
JavaScript
3
star
14

fp-workshop

FP workshop presented at HolidayCheck in Munich on 2016-12-14
JavaScript
3
star
15

remember-bower

🐦 Never forget to update dependencies in bower.json again
JavaScript
2
star
16

cube

Magic: The Gathering Cube
HTML
2
star
17

deedpoll

πŸ“„ Flag incorrectly named identifiers in JavaScript programs
Shell
2
star
18

contenteditable

A jQuery utility which adds useful behaviour to `contenteditable` text
CoffeeScript
2
star
19

rich

JavaScript’s missing stdlib
1
star
20

davidchambersdesign.com

1
star
21

Homelands-Constructed

Lists from a terrific Homelands Constructed tournament held in Auckland on 27/12/2011
1
star
22

got-lambda

Repository used during Got.Ξ» presentation on 1 September 2016
JavaScript
1
star