Home

Awesome

doctest-js

Let your documentation be your testing suite.

Write JSDoc style doc examples on all your functions and then test them using doctest-js.

Contents

Getting Started

1. Install

npm install @supabase/doctest-js

2. Write @example comments

Create a JSDoc style @example on any functions that you want tested.

/**
 * Returns the sum of 2 numbers
 *
 * @example sum(1, 2)
 * //=> 3
 */
export const sum = (a, b) => {
  return a + b
}

Note that the expected return value must be prefixed by //=>.

3. Run the tests

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

import doctest from '@supabase/doctest-js';

describe('Doctests', () => {
  // file paths are relative to root of directory
  doctest('src/sum.js')
  doctest('src/someOtherFile.js')
})

Advanced

Multiple tests

You can run multiple tests for the same function.

/**
 * @example sum(1, 2)
 * //=> 3
 * @example sum(3, 4)
 * //=> 7
 */
export const sum = (a, b) => {
  return a + b
}

Testing classes

Testing classes requires you to pass a newed up instance of the class into the test itself. Here is a simple example:

// Arithmetic.js - a basic class which we need to test

class Arithmetic {
  constructor() {}

  /**
   * @example add(1, 2)
   * //=> 3
   */
  add(a, b) {
    return a + b
  }
}

export { Arithmetic }
// Arithmetic.test.js

const { Arithmetic } = require('./Arithmetic.js')

describe('passing doctest', () => {
  doctest('./Arithmetic.js', { instance: new Arithmetic() })
})

Usage online

See this in the wild:

Contributing

Credits

Status

Ready for production! Watch and star this repo to keep updated on releases.

Watch this repo

Sponsors

We are building the features of Firebase using enterprise-grade, open source products. We support existing communities wherever possible, and if the products don’t exist we build them and open source them ourselves. Thanks to these sponsors who are making the OSS ecosystem better for everyone.

Worklife VC New Sponsor