Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/christianmurphy/mdast-util-arbitrary
Generate arbitrary mdast with fast check
https://github.com/christianmurphy/mdast-util-arbitrary
markdown mdast mdast-util property-based-testing quickcheck testing unist
Last synced: 10 days ago
JSON representation
Generate arbitrary mdast with fast check
- Host: GitHub
- URL: https://github.com/christianmurphy/mdast-util-arbitrary
- Owner: ChristianMurphy
- License: mit
- Created: 2021-06-16T00:12:27.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-06-26T18:03:33.000Z (over 3 years ago)
- Last Synced: 2024-12-26T16:06:00.031Z (11 days ago)
- Topics: markdown, mdast, mdast-util, property-based-testing, quickcheck, testing, unist
- Language: TypeScript
- Homepage:
- Size: 129 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mdast-util-arbitrary
[![NPM Version](https://img.shields.io/npm/v/mdast-util-arbitrary)](https://www.npmjs.com/package/mdast-util-arbitrary)
[![CI](https://github.com/ChristianMurphy/mdast-util-arbitrary/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/ChristianMurphy/mdast-util-arbitrary/actions/workflows/main.yml)Generate arbitrary, random, and valid [`mdast`](https://github.com/syntax-tree/mdast) with [`fast-check`](https://github.com/dubzzz/fast-check), useful for [property testing](https://medium.com/criteo-engineering/introduction-to-property-based-testing-f5236229d237) [`mdast` utils](https://github.com/syntax-tree/mdast#list-of-utilities) and [`remark` plugins](https://github.com/remarkjs/remark/blob/main/doc/plugins.md#plugins).
## Install
```bash
npm install --save-dev mdast-util-arbitrary
```## Usage
```ts
import { assert, property } from "fast-check";
import { commonmark } from "mdast-util-arbitrary";assert(
property(commonmark().Root, (mdast) => {
// do something with mdast
})
);
```## API
This package exports a commonmark function which returns a dictionary of node types which can be generated (usually `Root` should be used starting node type)
`commonmark(options?: Options) => {[nodeType: string]: Arbitrary}`
### Options
#### `Options.includeData`
Whether to generate arbitrary [`data`](https://github.com/syntax-tree/unist#node) attributes for nodes. Default `false`.
#### `Options.rootNodeMaxChildren`
Limit the maximum number of child nodes the `Root` node can have. Default `100`.
## Example
Using [`uvu`](https://github.com/lukeed/uvu) to test [`mdast-util-to-markdown`](https://github.com/syntax-tree/mdast-util-to-markdown).
Checking three properties of `mdast-util-to-markdown`:
1. it does not throw an exception on valid markdown
2. it produces a string
3. it produces non-empty markdown textGenerating 100 mdast random mdast trees to see if the properties hold true.
```ts
import { test } from "uvu";
import toString from "mdast-util-to-markdown";
import { assert, property } from "fast-check";
import { commonmark } from "mdast-util-arbitrary";test("arbitrary mdast can be stringified", () => {
assert(
property(commonmark().Root, (mdast) => {
const markdown = toString(mdast);
return typeof markdown === "string" && markdown.length > 1;
}),
{ numRuns: 100 }
);
});test.run();
```