rollup-plugin-ava-test-example
Injects JavaScript code from AVA unit test files into JSDoc examples
This rollup plugin does the following:
- Checks if the incoming file is a
.js
file (configurable viaextensions
setting) - Checks if the file contains the JSDoc comment tag
@example-test
(configurable viajsdocTag
setting) - Checks if a file exists within the same directory of the input file, using the same name but with the suffix
.unit.js
(configurable viasuffix
setting) - Parses the AVA unit test file using ava-to-json
- Maps the array of the parsed tests using the following nomenclature:
@example <caption>{{ test-name }}</caption> ```js {{ test-code }} ```
- Joins the array using two new lines and replaces the found JSDoc tag
@example-test
in the given input with this computed string.
Setup
rollup.config.js
const plugins = input: 'src/my-function.js' output: file: `dist/my-function.js` format: 'esm' plugins
src/my-function.js (entry point)
/** * Adds a + b * @example-test */ { return a + b}
src/my-function.unit.js (AVA unit test)
dist/my-function.unit.js (output)
/** * Adds a + b * @example <caption>Add a with b</caption> * * ```js * t.is(add(1, 2), 3) * t.not(add(2, 2), 5) // two and two NOT always makes a 5 ): * ``` */ { return a + b}
License
© 2020-present Martin Rafael Gonzalez tin@devtin.io