Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
chore(skills): refresh ckeditor5-testing for the editor-kit, aggregate coverage scope, and glue stubbing
Elian Doran37fce569bb40
diff --git a/.claude/skills/ckeditor5-testing/SKILL.md b/.claude/skills/ckeditor5-testing/SKILL.mdindex f4ccaade0d..ce9a40fe8e 100644--- a/.claude/skills/ckeditor5-testing/SKILL.md+++ b/.claude/skills/ckeditor5-testing/SKILL.md@@ -55,7 +55,9 @@ Trilium testing (Preact components, jQuery widgets, server routes), use `writing`include: ['src/**/*.spec.ts']` (as on `feature/collapsible_experiment`). The existing standalonepackages instead use a `tests/` dir (`include: ['tests/**/*.[jt]s']`, no `.spec` suffix) — leavethem; new standalone packages should use co-located `.spec.ts` too. `globals: true`. Coverage- provider `v8`, `include: src/**` (test files themselves excluded from coverage).+ provider `v8`, `include: src/**` (test files themselves excluded from coverage). The aggregate+ (`packages/ckeditor5`) must also `exclude: ['**/ckeditor5-*/**']` + `allowExternal: false` or the+ imported sibling packages bleed in (see `references/running-and-config.md`).- **Imports** from `'ckeditor5'`; in-package source imports use a file extension.- **License key:** tests pass `licenseKey: 'GPL'` in the editor config.- Some packages (`admonition`, `footnotes`, `keyboard-marker`) have a vitest config but **no@@ -82,28 +84,23 @@ test:sequential` runs `math` and `mermaid` **sequentially** (browser resource li## Anatomy of a test+In the aggregate (`packages/ckeditor5`), use the shared **editor kit** —+`createTestEditor()` from `test/editor-kit.ts` builds a real `ClassicEditor` (`licenseKey: 'GPL'`,+auto-tracked) and the global `afterEach` in `test/setup.ts` (wired via `setupFiles`) destroys every+tracked editor, so specs **don't** write their own editor-teardown `afterEach`:+```tsimport { ClassicEditor, Essentials, Paragraph, _setModelData } from 'ckeditor5';-import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';-import MyPlugin from '../src/myplugin.js';+import { describe, it, expect, beforeEach } from 'vitest';++import { createTestEditor } from '../../test/editor-kit.js';+import MyPlugin from './myplugin.js';describe( 'MyPlugin', () => {- let editorElement: HTMLDivElement, editor: ClassicEditor, model;+ let editor: ClassicEditor;beforeEach( async () => {- editorElement = document.createElement( 'div' );- document.body.appendChild( editorElement );-- editor = await ClassicEditor.create( editorElement, {- licenseKey: 'GPL',- plugins: [ Essentials, Paragraph, MyPlugin ]- } );- model = editor.model;- } );-- afterEach( () => {- editorElement.remove();- return editor.destroy();+ editor = await createTestEditor( [ Essentials, Paragraph, MyPlugin ] );} );it( 'loads the plugin', () => {@@ -111,19 +108,22 @@ describe( 'MyPlugin', () => {} );it( 'keeps the selection in a paragraph', () => {- _setModelData( model, '<paragraph>foo[]bar</paragraph>' );- expect( model.document.getRoot().getChild( 0 ).name ).toBe( 'paragraph' );+ _setModelData( editor.model, '<paragraph>foo[]bar</paragraph>' );+ expect( editor.model.document.getRoot().getChild( 0 ).name ).toBe( 'paragraph' );} );} );```+Need the host element? It's `editor.sourceElement` (or `getEditorElement( editor )` from the kit).+Some legacy specs still hand-roll the create/destroy scaffold (`document.createElement('div')` ++`ClassicEditor.create(...)` + a teardown `afterEach`) — those are being migrated to `createTestEditor`.+Conventions visible here and across the suite:- One top-level `describe` named after the unit, nested `describe`s for areas (`isEnabled`,`execute()`, …), small focused `it`s.-- Create the editor in `beforeEach` (return the Promise or use `async`/`await` — Vitest awaits- it), and **always tear down** in `afterEach`: `editor.destroy()` plus `editorElement.remove()`.-- Pass `licenseKey: 'GPL'`. List only the plugins the test needs (commands can also be- instantiated directly, e.g. `new InsertMermaidCommand( editor )`).+- Create the editor in `beforeEach` (return the Promise or use `async`/`await` — Vitest awaits it).+- Pass `licenseKey: 'GPL'` (the kit does this for you). List only the plugins the test needs+ (commands can also be instantiated directly, e.g. `new InsertMermaidCommand( editor )`).## Model/view test data@@ -156,12 +156,22 @@ button.fire( 'execute' );expect( spy ).toHaveBeenCalledWith( 'insertMermaid' );```+## Stubbing the Trilium glue (`glob` / clipboard / jQuery `$`)++Many in-aggregate plugins reference a global `glob` (the Trilium bridge typed in+`src/augmentation.ts`), some hit `navigator.clipboard`, and some converters call jQuery `$(...)`.+Use the globals kit (`test/globals-test-kit.ts`): `installGlobMock({…})` and `mockClipboard({…})`+install the stub **and register their own teardown** (run by the global `afterEach` in+`test/setup.ts`), and `$` is a global passthrough from `setup.ts` — so specs **don't** hand-roll+`globalThis.glob` or delete anything. (Browser mode shares one page, so a leaked global would bleed+into later specs.) See `references/patterns.md` for the recipe.+## Reference map| File | Use it for ||------|-----------|| `references/test-utilities.md` | Testing against a real `ClassicEditor` (lifecycle, `licenseKey: 'GPL'`), and the `_setModelData`/`_getModelData`/`_getViewData` helpers from `'ckeditor5'` + the `[]`/`{}` selection syntax. |-| `references/patterns.md` | Idiomatic recipes per concern (schema, conversion round-trips, commands, UI, keystrokes, events, async), all against a real editor; note on the 100% coverage gate for browser-mode packages. |+| `references/patterns.md` | Idiomatic recipes per concern (schema, conversion round-trips, commands, UI, keystrokes, events, async), all against a real editor; the `glob`/clipboard/jQuery-`$` stubbing recipe (via the globals kit's `installGlobMock`/`mockClipboard`); note on the 100% coverage gate for browser-mode packages. || `references/running-and-config.md` | Per-package `vitest.config.ts` (happy-dom shape and WebdriverIO browser shape), `pnpm --filter` commands, the debug command, `pnpm test:parallel`/`test:sequential` (math+mermaid sequential), coverage thresholds. || `references/test-conventions.md` | Trilium test **conventions & gotchas**: choosing happy-dom vs. browser mode, real-editor teardown, the both-assertion-styles note, sequential math/mermaid, and the pointer to `writing-unit-tests`. |diff --git a/.claude/skills/ckeditor5-testing/references/patterns.md b/.claude/skills/ckeditor5-testing/references/patterns.mdindex 096a37097b..c7a1ed9045 100644--- a/.claude/skills/ckeditor5-testing/references/patterns.md+++ b/.claude/skills/ckeditor5-testing/references/patterns.md@@ -1,37 +1,69 @@# Test patterns (Trilium, Vitest)Idiomatic recipes per concern. All examples assume a real `ClassicEditor` created in `beforeEach`-and destroyed in `afterEach` (see `test-utilities.md`). Mirror the structure of the feature: an-`*editing` test for schema/conversion/command, a `*ui` test for buttons/dropdowns. Assertions may-be **Jest-style** (`toBe`/`toEqual`) or **Chai-style** (`to.equal`/`to.be.false`) — both work in-Vitest; the examples use Jest-style.+(see `test-utilities.md`). Mirror the structure of the feature: an `*editing` test for+schema/conversion/command, a `*ui` test for buttons/dropdowns. Assertions may be **Jest-style**+(`toBe`/`toEqual`) or **Chai-style** (`to.equal`/`to.be.false`) — both work in Vitest; the examples+use Jest-style.## Setup / teardown+In the aggregate (`packages/ckeditor5`), use the shared editor kit — `createTestEditor()` from+`test/editor-kit.ts` builds the editor (`licenseKey: 'GPL'`, auto-tracked) and the global+`afterEach` in `test/setup.ts` destroys it, so a spec needs **no** editor-teardown `afterEach`:+```tsimport { ClassicEditor, Paragraph } from 'ckeditor5';-import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';-import MyFeature from '../src/myfeature.js';+import { describe, it, expect, beforeEach, vi } from 'vitest';++import { createTestEditor } from '../../test/editor-kit.js';+import MyFeature from './myfeature.js';-let editorElement: HTMLDivElement, editor: ClassicEditor, model;+let editor: ClassicEditor, model;beforeEach( async () => {- editorElement = document.createElement( 'div' );- document.body.appendChild( editorElement );- editor = await ClassicEditor.create( editorElement, {- licenseKey: 'GPL',- plugins: [ Paragraph, MyFeature ]- } );+ editor = await createTestEditor( [ Paragraph, MyFeature ] );model = editor.model;} );+```++Return the Promise (or use `async`/`await`) so Vitest waits. Default `testTimeout` is 5000 ms. Need+the host element? It's `editor.sourceElement` (or `getEditorElement( editor )` from the kit). The+standalone packages (and legacy specs mid-migration) still hand-roll the scaffold —+`document.createElement('div')` + `ClassicEditor.create(...)` + an `afterEach` that calls+`editor.destroy()` and `editorElement.remove()`.++## Stubbing the Trilium glue (`glob` / `navigator.clipboard` / jQuery `$`)-afterEach( () => {- editorElement.remove();- return editor.destroy();+Many in-aggregate plugins reach for a global `glob` (the Trilium bridge — typed in+`packages/ckeditor5/src/augmentation.ts` with `getComponentByEl`, `getReferenceLinkTitle`,+`getReferenceLinkTitleSync`, `getActiveContextNote`, `getHeaders`), some buttons hit+`navigator.clipboard`, and some converters call jQuery `$(...)` (e.g. the `editingDowncast`+converter in `packages/ckeditor5/src/plugins/referencelink.ts`). Use the globals kit+(`test/globals-test-kit.ts`) so each stub **tears itself down** after the test — never hand-roll+`globalThis.glob = …` plus a manual `delete`:++```ts+import { installGlobMock, mockClipboard } from '../../test/globals-test-kit.js';++beforeEach( async () => {+ const triggerCommand = vi.fn();+ installGlobMock( { getComponentByEl: () => ( { triggerCommand } ) } ); // removed after the test+ editor = await createTestEditor( [ Essentials, Paragraph, InternalLinkPlugin ] );} );+// no afterEach: setup.ts runs the kit's cleanups (and destroys the editor)```-Return the Promise (or use `async`/`await`) so Vitest waits. Default `testTimeout` is 5000 ms.+- `installGlobMock( obj )` casts to the global `glob` type for you and returns `obj` for assertions; its teardown is queued and run by the global `afterEach` in `test/setup.ts`.+- `mockClipboard( obj )` swaps `navigator.clipboard` and restores the original afterwards.+- jQuery `$` is already a global passthrough installed by `test/setup.ts` — specs don't set it.++**Why a kit and not hand-rolled globals:** browser mode runs every spec in **one shared page**, so a+leaked `glob`/`$`/clipboard survives into later specs and causes test-order-dependent flakiness — a+review caught exactly this missing cleanup. The kit makes teardown automatic. (Existing specs still+hand-roll `globalThis.glob = … as unknown as typeof glob` + a manual `delete` in `afterEach`; that's+the legacy pattern being migrated to `installGlobMock` — see+`packages/ckeditor5/src/plugins/internallink.spec.ts`.)## Schemadiff --git a/.claude/skills/ckeditor5-testing/references/running-and-config.md b/.claude/skills/ckeditor5-testing/references/running-and-config.mdindex b50a7e09d4..df5176a0ce 100644--- a/.claude/skills/ckeditor5-testing/references/running-and-config.md+++ b/.claude/skills/ckeditor5-testing/references/running-and-config.md@@ -97,6 +97,31 @@ This is the repo-wide convention (see the `writing-unit-tests` skill) and what`feature/collapsible_experiment` uses. New standalone packages should also adopt co-located`.spec.ts`.+## Coverage scope for the aggregate (`packages/ckeditor5`)++The aggregate **imports** the sibling `@triliumnext/ckeditor5-*` workspace packages, so a plain+`--coverage` run instruments their loaded `src/` too — and the `include: ['src/**']` glob matches+those sibling sources. The report then reads a misleading **~48%** instead of the aggregate's real+number. Those siblings carry their own 100% gates in their own packages, so scope the aggregate's+report to its own sources only — `packages/ckeditor5/vitest.config.ts` does this with:++```ts+coverage: {+ provider: 'v8',+ allowExternal: false, // don't reach outside the package root+ include: [ 'src/**/*.{ts,tsx}' ],+ exclude: [+ '**/*.{test,spec}.{ts,mts,cts,tsx,js,jsx}', '**/*.d.ts',+ '**/node_modules/**', '**/ckeditor5-*/**' // <- keeps imported siblings out+ ],+ reporter: [ 'text', 'lcov' ],+ reportsDirectory: './test-output/vitest/coverage'+}+```++`reporter: ['text', 'lcov']` + that `reportsDirectory` are what the `analyzing-coverage`+analyzer (`lcov.info`) and Codecov consume — keep them when adding coverage to a package.+## DebuggingBrowser-mode packages support an inspector + visible browser:diff --git a/.claude/skills/ckeditor5-testing/references/test-conventions.md b/.claude/skills/ckeditor5-testing/references/test-conventions.mdindex c64126a38a..a470b9283a 100644--- a/.claude/skills/ckeditor5-testing/references/test-conventions.md+++ b/.claude/skills/ckeditor5-testing/references/test-conventions.md@@ -23,26 +23,32 @@ general `writing-unit-tests` skill reserves `@vitest/browser` for exactly these## Real-editor lifecycle & teardown-There are **no** test-editor factories in Trilium. Create a real `ClassicEditor` over a real-element with `licenseKey: 'GPL'`, and always tear it down:+There are **no** upstream test-editor factories (`ModelTestEditor` etc.) in Trilium — tests build a+real `ClassicEditor` over a real element with `licenseKey: 'GPL'`. In the aggregate+(`packages/ckeditor5`), the shared kit does this for you: `createTestEditor()` from+`test/editor-kit.ts` creates and **tracks** the editor, and the global `afterEach` in+`test/setup.ts` (wired via `setupFiles`) destroys every tracked editor and removes its host element.+So aggregate specs no longer write an editor-teardown `afterEach`:```ts-beforeEach( async () => {- editorElement = document.createElement( 'div' );- document.body.appendChild( editorElement );- editor = await ClassicEditor.create( editorElement, {- licenseKey: 'GPL', plugins: [ Paragraph, MyPlugin ]- } );-} );+import { createTestEditor } from '../../test/editor-kit.js';-afterEach( () => {- editorElement.remove();- return editor.destroy();+beforeEach( async () => {+ editor = await createTestEditor( [ Paragraph, MyPlugin ] );} );+// no afterEach for the editor — setup.ts tears it down```-Forgetting `editor.destroy()` or `editorElement.remove()` leaks editor DOM / body wrappers across-tests and causes flakiness.+The host element is `editor.sourceElement` (or `getEditorElement( editor )` from the kit). The+standalone packages (and legacy specs mid-migration to the kit) still hand-roll create + an+`afterEach` calling `editor.destroy()` and `editorElement.remove()`; forgetting either there leaks+editor DOM / body wrappers across tests and causes flakiness.++**Globals: use the kit's installers.** Stub the Trilium `glob` / `navigator.clipboard` via+`installGlobMock()` / `mockClipboard()` from `test/globals-test-kit.ts` — they register their own+teardown (run by `setup.ts`), and `$` is a global passthrough from `setup.ts`. Don't hand-roll+`globalThis.glob` + a manual `delete`; browser mode's shared page leaks a forgotten global into+later specs (see `patterns.md`).## Assertion styles — both workdiff --git a/.claude/skills/ckeditor5-testing/references/test-utilities.md b/.claude/skills/ckeditor5-testing/references/test-utilities.mdindex b1c5fc8647..c63c8a780c 100644--- a/.claude/skills/ckeditor5-testing/references/test-utilities.md+++ b/.claude/skills/ckeditor5-testing/references/test-utilities.md@@ -1,50 +1,51 @@# Test utilities & data helpers (Trilium)Trilium plugin tests run against a **real editor** and assert with the model/view stringify-parse-helpers. There are no test-editor factories in Trilium.+helpers. There are no upstream test-editor factories in Trilium — but the aggregate ships a thin+shared kit that wraps the real `ClassicEditor`.## Test against a real `ClassicEditor`-Create a real editor over a real DOM element. Pass `licenseKey: 'GPL'` and only the plugins the-test needs. Tear down in `afterEach`: destroy the editor and remove the element.+In the aggregate (`packages/ckeditor5`), use `createTestEditor()` from `test/editor-kit.ts`: it+creates a real editor (`licenseKey: 'GPL'`, only the plugins you pass) over a fresh host element and+**tracks** it, and the global `afterEach` in `test/setup.ts` destroys every tracked editor — so no+per-spec editor teardown:```tsimport { ClassicEditor, Paragraph } from 'ckeditor5';-import { describe, beforeEach, afterEach, it, expect } from 'vitest';-import MyEditing from '../src/myediting.js';+import { describe, beforeEach, it, expect } from 'vitest';++import { createTestEditor } from '../../test/editor-kit.js';+import MyEditing from './myediting.js';describe( 'MyEditing', () => {- let editorElement: HTMLDivElement, editor: ClassicEditor, model;+ let editor: ClassicEditor, model;beforeEach( async () => {- editorElement = document.createElement( 'div' );- document.body.appendChild( editorElement );-- editor = await ClassicEditor.create( editorElement, {- licenseKey: 'GPL',- plugins: [ Paragraph, MyEditing ]- } );+ editor = await createTestEditor( [ Paragraph, MyEditing ] );model = editor.model;} );-- afterEach( () => {- editorElement.remove();- return editor.destroy();- } );+ // no afterEach: setup.ts destroys tracked editors} );```+Pass extra editor config (toolbar, balloon, etc.) as the second arg:+`createTestEditor( [ Paragraph, MyEditing ], { toolbar: [ 'myButton' ] } )`. Need the host element?+`editor.sourceElement`, or `getEditorElement( editor )` from the kit.+Notes:- `editor.model`, `editor.editing.view`, `editor.ui`, `editor.commands`, `editor.plugins` are allavailable — it's a real editor, not a stripped-down test editor.- Commands can be instantiated directly for focused tests:`command = new InsertMermaidCommand( editor )` after the editor is created.-- Teardown matters: always `editor.destroy()` **and** `editorElement.remove()`, or leftover- editor DOM/body wrappers will leak between tests.+- **Standalone packages and legacy specs** (mid-migration to the kit) still hand-roll the scaffold:+ `document.createElement('div')` + `ClassicEditor.create(...)` + an `afterEach` that calls+ `editor.destroy()` **and** `editorElement.remove()`. There, forgetting either leaks editor+ DOM/body wrappers between tests.> The upstream ckeditor5 monorepo ships `ModelTestEditor` / `VirtualTestEditor` /> `ClassicTestEditor` in `@ckeditor/ckeditor5-core/tests/_utils`, but those are monorepo-internal-> and are **not** available in Trilium. Use a real `ClassicEditor` as above.+> and are **not** available in Trilium. Use a real `ClassicEditor` (or the kit) as above.## Model & view data helpers