mdast-util-arbitrary
v1.1.1
Published
Generate arbitrary mdast with fast check
Downloads
4
Readme
mdast-util-arbitrary
Generate arbitrary, random, and valid mdast
with fast-check
, useful for property testing mdast
utils and remark
plugins.
Install
npm install --save-dev mdast-util-arbitrary
Usage
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
attributes for nodes. Default false
.
Options.rootNodeMaxChildren
Limit the maximum number of child nodes the Root
node can have. Default 100
.
Example
Using uvu
to test mdast-util-to-markdown
.
Checking three properties of mdast-util-to-markdown
:
- it does not throw an exception on valid markdown
- it produces a string
- it produces non-empty markdown text
Generating 100 mdast random mdast trees to see if the properties hold true.
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();