Let your documentation be your testing suite.
Write JSDoc style doc examples on all your functions and then test them using doctest-js
.
Contents
npm install @supabase/doctest-js
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 //=>
.
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')
})
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 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() })
})
See this in the wild:
Ready for production! Watch and star this repo to keep updated on releases.
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.