ts-testdoc helps you keep your documentation accurate by running example code blocks as tests. This ensures that your examples work correctly every time you update your code.
To get started, youβll need to download the software. Visit the releases page to download the latest version:
Follow these steps to install ts-testdoc:
Run the following command to install ts-testdoc:
pnpm add -D ts-testdoc tsx
Note: If you donβt have pnpm installed, you can install it with the following command:
npm install -g pnpm
Once you have installed ts-testdoc, you can use it to run your documentation tests. Here are a few commands to help you get started:
To run the tests in your documentation, use this command:
npx ts-testdoc src/
If you want to see how your examples appear without actually running them, you can use the dry-run option:
npx ts-testdoc --dry-run src/
You can specify a timeout for when tests should stop running. Hereβs how to set a timeout of 10 seconds:
npx ts-testdoc --timeout 10000 src/
To write tests for your code examples, add @example blocks to your JSDoc comments. Hereβs a simple example:
/**
* Adds two numbers together.
*
* @example
* ```ts
* import { add } from "my-package";
*
* const result = add(2, 3);
* assertEqual(result, 5);
* ```
*/
export function add(a: number, b: number): number {
return a + b;
}
ts-testdoc provides the following assertion helpers for your examples:
assert(condition, message?) // Throws an error if condition is false
assertEqual(actual, expected) // Throws an error if actual is not equal to expected
pnpm is required, but you can also use npm or yarnWe welcome contributions to improve ts-testdoc! If you want to contribute, please follow these steps:
Please ensure that your code is well-tested and adheres to our coding standards.
ts-testdoc is licensed under the MIT License. Feel free to use it in your projects, but please give credit to the original authors when appropriate.
If you encounter any issues or have questions, please create an issue in the GitHub repository. You can also check the existing issues for common problems and solutions.
Visit the releases page to download the latest version of ts-testdoc: