This library is used to run code samples in markdown documents as acceptance tests, using simple comment-based assertions. By testing code snippets in documentation, you can have greater confidence that code in your documentation works as advertised. doc-tester expects code snippets in the markdown to follow a certain assertion language. Please rewrite markdown file to adhere to doc-tester's format.
npm install doc-tester
If you are using yarn
yarn add doc-tester
doc-tester
Path of file to be tested (default:
./README.md
).
Setting this to false will not remove the test file generated by parsing the documentation file (default :
true
).
Runs tests w/ the node
--inspect
option, allowing a debugger to be attached (default:false
).
Path where generated test file will be written. (default:
./test.js
)
import { runTest } from 'doc-tester';
await runTest({
codeArray: ['add(3,4) // equals: 7;'],
importsArray: [`import { add } from './add'`]
} /* , options */) // equals: true;
- testName
Name for the test block. Defaulted to
Doc Test
.
In order for absolute import
s to work, make sure what you're importing is present in your project's node_modules/
folder.
Relative imports are generally a bad idea in documentation examples -- create examples as your users would need to think about them -- consuming a library as a dependency, using absolute imports.
Please keep in mind that some import-related issues can be solved by symlinking a project's root directory (or build output) into its own node_modules/
folder, effectively simulating what your users might see in their own node_modules
when depending on your library.
You can use the code below sample code get the test working, replace doc-tester
with your module name.
const fs = require('fs-extra');
try {
fs.symlinkSync(__dirname, `./node_modules/doc-tester`);
} catch(err) {
if (err.code == 'EEXIST') {
console.log('doc-tester already symlinked to node_modules');
} else {
throw err;
}
}
Check the package.json of this project how I do it.
The doc-tester expects code block to be marked with language, ex: js
, ts
etc. It decides whether to generate tests for the given code block or not based on this.
This library also expects to follow the below syntax in the doc.
statement // assertionType: expectedResult
Assertion types equals
, not-equals
, throws
and deep-equals
are supported as of now.
// Let's assume README has snippet Math.sqrt(4) // 2
// statement is Math.sqrt(4), expected value is 2 and assertionType is equals. We rewrite it to,
Math.sqrt(4); // equals: 2
Math.sqrt(4) // not-equals: 4
new Error('3'); // throws
{ a : 5, b: 6}; // deep-equal: { a : 5, b: 6 }
Note: The statement should be a non-assignment statement.
ex:
a=3; //equals: 3
can not be tested.