Unit Testing Plate
Unit test Plate editors, transforms, and plugins with headless fixtures.
@platejs/test keeps Plate fixtures and test harnesses in one package without
mixing their runtimes. Its root works in plain Node. Import React and
Playwright helpers only from their explicit subpaths.
Installation
Install React test peers only when a test imports @platejs/test/react:
| Import | Use for |
|---|---|
@platejs/test | Node-safe JSX fixtures, editor builders, selection projection, and DataTransfer fixtures |
@platejs/test/react | EditorTest and Testing Library interaction helpers |
@platejs/test/playwright | Playwright editor harnesses and failure state capture |
Setting up tests
Use a .tsx file and select the classic JSX runtime with the fixture factory:
/** @jsxRuntime classic */
/** @jsx jsx */
import {
jsx,
projectTestSelectionRange,
type TestEditorFixture,
} from '@platejs/test';
jsx; // so Biome doesn't remove unused imports/** @jsxRuntime classic */
/** @jsx jsx */
import {
jsx,
projectTestSelectionRange,
type TestEditorFixture,
} from '@platejs/test';
jsx; // so Biome doesn't remove unused importsThis allows you to use JSX syntax for creating editor values.
Testing React components
Use the React entrypoint when the test mounts EditorContent or interacts with
the rendered editor:
import { createTestEditor } from '@platejs/test/react';
import { ParagraphPlugin } from 'platejs/react';
const [editor, actions] = await createTestEditor({
initialValue: [
{ children: [{ text: '' }], type: 'paragraph' },
],
plugins: [ParagraphPlugin],
});
await actions.type('Hello');
expect(editor.read.children()).toEqual([
{ children: [{ text: 'Hello' }], type: 'paragraph' },
]);import { createTestEditor } from '@platejs/test/react';
import { ParagraphPlugin } from 'platejs/react';
const [editor, actions] = await createTestEditor({
initialValue: [
{ children: [{ text: '' }], type: 'paragraph' },
],
plugins: [ParagraphPlugin],
});
await actions.type('Hello');
expect(editor.read.children()).toEqual([
{ children: [{ text: 'Hello' }], type: 'paragraph' },
]);Creating test cases
Editor state representation
Use JSX to represent editor states:
const input = (
<editor>
<hp>
Hello<cursor /> world
</hp>
</editor>
) as TestEditorFixture;const input = (
<editor>
<hp>
Hello<cursor /> world
</hp>
</editor>
) as TestEditorFixture;Node elements like <hp />, <hheading />, <htable /> represent different types of nodes.
Special elements like <cursor />, <anchor />, and <focus /> represent selection states.
Testing transforms
- Create an input state
- Define the expected output state
- Use
createEditorto set up the editor - Apply the transform(s) directly
- Assert the editor's new state
Example testing bold formatting:
it('should apply bold formatting', () => {
const input = (
<editor>
<hp>
Hello <anchor />
world
<focus />
</hp>
</editor>
) as TestEditorFixture;
const output = (
<editor>
<hp>
Hello <htext bold>world</htext>
</hp>
</editor>
) as TestEditorFixture;
const editor = createEditor({
plugins: [BoldPlugin],
initialValue: input.children,
initialSelection: input.selection,
});
editor.update((tx) => {
tx.marks.toggle('bold');
});
expect(editor.read.children()).toEqual(output.children);
});it('should apply bold formatting', () => {
const input = (
<editor>
<hp>
Hello <anchor />
world
<focus />
</hp>
</editor>
) as TestEditorFixture;
const output = (
<editor>
<hp>
Hello <htext bold>world</htext>
</hp>
</editor>
) as TestEditorFixture;
const editor = createEditor({
plugins: [BoldPlugin],
initialValue: input.children,
initialSelection: input.selection,
});
editor.update((tx) => {
tx.marks.toggle('bold');
});
expect(editor.read.children()).toEqual(output.children);
});Testing selection
Test how operations affect the editor's selection:
it('should collapse selection on backspace', () => {
const input = (
<editor>
<hp>
He<anchor />llo wor<focus />ld
</hp>
</editor>
) as TestEditorFixture;
const output = (
<editor>
<hp>
He<cursor />ld
</hp>
</editor>
) as TestEditorFixture;
const editor = createEditor({
initialValue: input.children,
initialSelection: input.selection,
});
editor.update.slice.delete({ direction: 'backward' });
expect(editor.read.children()).toEqual(output.children);
expect(editor.read.selection()).toEqual(
projectTestSelectionRange(output.selection)
);
});it('should collapse selection on backspace', () => {
const input = (
<editor>
<hp>
He<anchor />llo wor<focus />ld
</hp>
</editor>
) as TestEditorFixture;
const output = (
<editor>
<hp>
He<cursor />ld
</hp>
</editor>
) as TestEditorFixture;
const editor = createEditor({
initialValue: input.children,
initialSelection: input.selection,
});
editor.update.slice.delete({ direction: 'backward' });
expect(editor.read.children()).toEqual(output.children);
expect(editor.read.selection()).toEqual(
projectTestSelectionRange(output.selection)
);
});Testing key events
To test that a plugin's key handler runs on a key press:
import { createTestEditor } from '@platejs/test/react';
import { definePlugin } from 'platejs/react';
it('should call the keyDown handler', async () => {
const input = (
<editor>
<hp>
Hello <anchor />world<focus />
</hp>
</editor>
) as TestEditorFixture;
// Create a mock handler to verify it's called
const keyDownMock = jest.fn();
const TestPlugin = definePlugin('test', {
on: {
keyDown: keyDownMock,
},
});
const [, actions] = await createTestEditor({
initialValue: input.children,
initialSelection: input.selection,
plugins: [TestPlugin],
});
// Press Enter in the mounted editor
await actions.triggerKeyboardEvent('Enter');
// Verify the handler was called
expect(keyDownMock).toHaveBeenCalled();
});import { createTestEditor } from '@platejs/test/react';
import { definePlugin } from 'platejs/react';
it('should call the keyDown handler', async () => {
const input = (
<editor>
<hp>
Hello <anchor />world<focus />
</hp>
</editor>
) as TestEditorFixture;
// Create a mock handler to verify it's called
const keyDownMock = jest.fn();
const TestPlugin = definePlugin('test', {
on: {
keyDown: keyDownMock,
},
});
const [, actions] = await createTestEditor({
initialValue: input.children,
initialSelection: input.selection,
plugins: [TestPlugin],
});
// Press Enter in the mounted editor
await actions.triggerKeyboardEvent('Enter');
// Verify the handler was called
expect(keyDownMock).toHaveBeenCalled();
});Testing complex scenarios
For complex plugins like tables, test various scenarios by directly applying transforms:
describe('Table plugin', () => {
it('should insert a table', () => {
const input = (
<editor>
<hp>
Test<cursor />
</hp>
</editor>
) as TestEditorFixture;
const output = (
<editor>
<hp>Test</hp>
<htable>
<htr>
<htd>
<hp>
<cursor />
</hp>
</htd>
<htd>
<hp><htext /></hp>
</htd>
</htr>
<htr>
<htd>
<hp><htext /></hp>
</htd>
<htd>
<hp><htext /></hp>
</htd>
</htr>
</htable>
</editor>
) as TestEditorFixture;
const editor = createEditor({
initialValue: input.children,
initialSelection: input.selection,
plugins: [TablePlugin],
});
editor.update((tx) => {
tx.table.insert({ columns: 2, rows: 2 }, { select: true });
});
expect(editor.read.children()).toEqual(output.children);
expect(editor.read.selection()).toEqual(
projectTestSelectionRange(output.selection)
);
});
});describe('Table plugin', () => {
it('should insert a table', () => {
const input = (
<editor>
<hp>
Test<cursor />
</hp>
</editor>
) as TestEditorFixture;
const output = (
<editor>
<hp>Test</hp>
<htable>
<htr>
<htd>
<hp>
<cursor />
</hp>
</htd>
<htd>
<hp><htext /></hp>
</htd>
</htr>
<htr>
<htd>
<hp><htext /></hp>
</htd>
<htd>
<hp><htext /></hp>
</htd>
</htr>
</htable>
</editor>
) as TestEditorFixture;
const editor = createEditor({
initialValue: input.children,
initialSelection: input.selection,
plugins: [TablePlugin],
});
editor.update((tx) => {
tx.table.insert({ columns: 2, rows: 2 }, { select: true });
});
expect(editor.read.children()).toEqual(output.children);
expect(editor.read.selection()).toEqual(
projectTestSelectionRange(output.selection)
);
});
});Testing plugin state
Test how different initial state affects behavior:
describe('when keepSelectedTextOnPaste is disabled', () => {
it('replaces the selected text with the pasted url', () => {
const input = (
<editor>
<hp>
start <anchor />
of regular text
<focus />
</hp>
</editor>
) as TestEditorFixture;
const output = (
<editor>
<hp>
start <ha url="https://google.com">https://google.com</ha>
<htext />
</hp>
</editor>
) as TestEditorFixture;
const editor = createEditor({
plugins: [
LinkPlugin.configure({
inputRules: [LinkRules.autolink({ variant: 'paste' })],
initialState: {
keepSelectedTextOnPaste: false,
},
}),
],
initialValue: input.children,
initialSelection: input.selection,
});
editor.api.dom.clipboard.insertData({
getData: (type: string) => (type === 'text/plain' ? 'https://google.com' : ''),
} as any);
expect(editor.read.children()).toEqual(output.children);
});
});describe('when keepSelectedTextOnPaste is disabled', () => {
it('replaces the selected text with the pasted url', () => {
const input = (
<editor>
<hp>
start <anchor />
of regular text
<focus />
</hp>
</editor>
) as TestEditorFixture;
const output = (
<editor>
<hp>
start <ha url="https://google.com">https://google.com</ha>
<htext />
</hp>
</editor>
) as TestEditorFixture;
const editor = createEditor({
plugins: [
LinkPlugin.configure({
inputRules: [LinkRules.autolink({ variant: 'paste' })],
initialState: {
keepSelectedTextOnPaste: false,
},
}),
],
initialValue: input.children,
initialSelection: input.selection,
});
editor.api.dom.clipboard.insertData({
getData: (type: string) => (type === 'text/plain' ? 'https://google.com' : ''),
} as any);
expect(editor.read.children()).toEqual(output.children);
});
});Mocking vs. real transforms
While mocking can be useful for isolating specific behaviors, Plate tests often assess actual editor children and selection after transforms. This approach ensures that plugins work correctly with the entire editor state.
Custom fixture vocabulary
Use createHyperscript from platejs/hyperscript for a small domain-specific
fixture factory. It creates ordinary node values and selection markers; keep it
in tests and fixtures.
import { createHyperscript } from 'platejs/hyperscript';
const h = createHyperscript({
elements: { paragraph: { type: 'paragraph' } },
});
const paragraph = h('paragraph', {}, 'Hello');import { createHyperscript } from 'platejs/hyperscript';
const h = createHyperscript({
elements: { paragraph: { type: 'paragraph' } },
});
const paragraph = h('paragraph', {}, 'Hello');The built-in fragment, element, and text tags construct nodes; cursor,
anchor, and focus place selection endpoints. The selection tag constructs
a range. Use @platejs/test when its Plate vocabulary and harnesses cover the
fixture.
Schema-derived document generation
Use createEditorArbitrary from @platejs/test with a compiled
EditorSchemaContract from platejs/compiler to generate deterministic,
schema-valid documents for property tests without extra dependencies.
import { createEditorArbitrary } from '@platejs/test';
import { BaseBoldPlugin, BaseParagraphPlugin, createEditor } from 'platejs';
import { compileEditor } from 'platejs/compiler';
const plugins = [BaseParagraphPlugin, BaseBoldPlugin] as const;
const { schema } = compileEditor({ plugins });
const arbitrary = createEditorArbitrary(schema, {
maxChildren: 4,
maxDepth: 3,
});
const editor = createEditor({ plugins });
const value = arbitrary.generate(42);
editor.read.schema.assertDocument({ children: value });import { createEditorArbitrary } from '@platejs/test';
import { BaseBoldPlugin, BaseParagraphPlugin, createEditor } from 'platejs';
import { compileEditor } from 'platejs/compiler';
const plugins = [BaseParagraphPlugin, BaseBoldPlugin] as const;
const { schema } = compileEditor({ plugins });
const arbitrary = createEditorArbitrary(schema, {
maxChildren: 4,
maxDepth: 3,
});
const editor = createEditor({ plugins });
const value = arbitrary.generate(42);
editor.read.schema.assertDocument({ children: value });Pass propertyValues in options when a custom plugin declares a required
property with a runtime validate function that cannot be inferred from the
JSON contract alone. Compose with fast-check using
fc.integer().map(arbitrary.generate).
Pro and Business
Plate Pro ships @plate-pro/playwright-suite, a customer-runnable Playwright browser proof suite covering typing, IME composition, slash commands, table navigation, two-tab sync, Ink draw-and-send, and mobile viewports. Plate Business adds @plate-business/security-proof adversarial canaries and plate verify CI for schema-derived fuzzing, shrunk counterexamples, and zero-PII PR verification. See Pricing.