`prosemirror-test-builder` is a specialized utility package designed to simplify the programmatic construction of ProseMirror documents for testing purposes. It provides a comprehensive set of helper functions, often named after common HTML tags like `p`, `h1`, `ul`, and `li`, allowing developers to intuitively build nodes and marks within a default, feature-rich ProseMirror schema. The package also includes a `builders` utility for generating these helpers based on a custom schema. A key differentiating feature is its integrated tag system, which enables developers to embed named markers within document content strings (e.g., `<a>`) to precisely track and retrieve cursor positions within the generated document, significantly streamlining assertion logic in tests. This current stable version is 1.1.1, and its release cadence is generally aligned with the broader ProseMirror ecosystem, ensuring compatibility for test setups.
npm install prosemirror-test-builderVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates building a ProseMirror document using both the default builders and the custom `builders` factory function, along with accessing the embedded tag positions.
Ensure `prosemirror-test-builder`'s major version is compatible with your `prosemirror-model` version. Consult the release notes for both packages.
Correct the import path to `import { builders } from 'prosemirror-test-builder/dist/build';`Remember that `doc(p('Text<a>'))` results in a paragraph with 'Text', and `doc.tag.a` will hold the position *after* 'Text', not 'Text<a>'.Prefer `import { p } from 'prosemirror-test-builder';` for ESM. For CommonJS, if issues arise, try `const prosemirrorTestBuilder = require('prosemirror-test-builder'); const p = prosemirrorTestBuilder.p;` or ensure your build setup correctly handles ESM-CJS interop.Use ES module `import { p } from 'prosemirror-test-builder';` if in an ESM context, or ensure correct CommonJS interop. If forced to use `require`, try `const pkg = require('prosemirror-test-builder'); const p = pkg.p;`Verify that the tag (`<a>`) is correctly placed within the string arguments of your builders and that the builder functions themselves are returning valid `Node` objects. Tags are stored on the root node or any node that directly contains them.
If using a custom schema, ensure you use `import { builders } from 'prosemirror-test-builder/dist/build';` and then create your schema-specific builders: `const { custom_paragraph } = builders(myCustomSchema);`. Do not mix builders from different schemas without explicit schema conversion or mapping.