Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kiwicopple/doctest-js
Run JSDoc style doc examples as tests within your test suite
https://github.com/kiwicopple/doctest-js
hacktoberfest
Last synced: 2 months ago
JSON representation
Run JSDoc style doc examples as tests within your test suite
- Host: GitHub
- URL: https://github.com/kiwicopple/doctest-js
- Owner: kiwicopple
- License: mit
- Created: 2020-01-29T04:51:25.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-10T19:13:00.000Z (about 2 years ago)
- Last Synced: 2024-12-10T15:07:14.307Z (2 months ago)
- Topics: hacktoberfest
- Language: JavaScript
- Homepage:
- Size: 1.69 MB
- Stars: 101
- Watchers: 5
- Forks: 4
- Open Issues: 26
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# doctest-js
Let your documentation be your testing suite.
Write [JSDoc](http://usejsdoc.org/about-getting-started.html) style doc examples on all your functions and then test them using `doctest-js`.
**Contents**
- [Getting Started](#getting-started)
- [1. Install](#1-install)
- [2. Write @example comments](#2-write-example-comments)
- [3. Run the tests](#3-run-the-tests)
- [Advanced](#advanced)
- [Multiple tests](#multiple-tests)
- [Testing classes](#testing-classes)
- [Usage online](#usage-online)
- [Contributing](#contributing)
- [Credits](#credits)
- [Status](#status)## Getting Started
### 1. Install
```sh
npm install @supabase/doctest-js
```### 2. Write @example comments
Create a [JSDoc style @example](https://jsdoc.app/tags-example.html) on any functions that you want tested.
```javascript
/**
* 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.
```javascript
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.
```javascript
/**
* @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:
```js
// Arithmetic.js - a basic class which we need to testclass Arithmetic {
constructor() {}/**
* @example add(1, 2)
* //=> 3
*/
add(a, b) {
return a + b
}
}export { Arithmetic }
``````js
// Arithmetic.test.jsconst { Arithmetic } = require('./Arithmetic.js')
describe('passing doctest', () => {
doctest('./Arithmetic.js', { instance: new Arithmetic() })
})
```## Usage online
See this in the wild:
- [supabase/postgrest-js](https://github.com/supabase/postgrest-js/blob/master/test/unit/Doctests.test.js)
## Contributing
- Fork the repo on GitHub
- Clone the project to your own machine
- Commit changes to your own branch
- Push your work back up to your fork
- Submit a Pull request so that we can review your changes and merge## Credits
* Inspired by [Elixir Doctests](https://elixir-lang.org/getting-started/mix-otp/docs-tests-and-with.html)
* Original fork of [mainshayne223/doctest-js](https://github.com/MainShayne233/js-doctest). See [issue #1](https://github.com/MainShayne233/js-doctest/issues/1).## Status
Ready for production! Watch and star this repo to keep updated on releases.

## 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.
[](https://www.worklife.vc)
[](https://github.com/sponsors/supabase)