unified

Project: ChristianMurphy/mdast-util-arbitrary

Package: mdast-util-arbitrary@1.1.1

  1. Dependents: 0
  2. Generate arbitrary mdast with fast check

    mdast-util-arbitrary

    NPM Version CI

    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:

    1. it does not throw an exception on valid markdown
    2. it produces a string
    3. 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();