Testing
@adecore/editor-react/testing holds a fake language service and a manual clock, so the features run in bun test without a server or browser.
FakeLanguageService
FakeLanguageService implements LanguageService in memory. respond(method, answer, provider?) registers an answer for an LSP method, with its provider options such as triggerCharacters. A method without an answer is unsupported and rejects with code -32601.
import { expect, test } from 'bun:test';
import { FakeEditorEngine } from '@adecore/editor/fake';
import { EditorLanguage, ProjectLanguage } from '@adecore/editor-react';
import { FakeLanguageService, ManualTimers } from '@adecore/editor-react/testing';
test('the editor reports its edits and counts problems', async () => {
const service = new FakeLanguageService();
const project = new ProjectLanguage(service);
const editor = new FakeEditorEngine().mount({} as HTMLElement, { text: 'one', theme: 'light' });
const language = new EditorLanguage(project, editor, 'file:///work/example.ts', 'typescript', new ManualTimers());
await language.document.ready;
editor.type('two');
expect(service.documents.get(language.uri)?.text).toBe('two');
service.report({
uri: language.uri,
source: 'fake',
diagnostics: [{ range: { start: { line: 0, character: 0 }, end: { line: 0, character: 3 } }, message: 'A problem', severity: 1 }]
});
expect(language.diagnostics.counts().error).toBe(1);
language.dispose();
project.dispose();
});| Member | What it is |
|---|---|
documents | The open documents with their text and version |
calls | Every request and document notification, as LanguageCalls (method, uri, params, options) |
report(report) | Sends a diagnostics report |
refresh(uri) | Fires onProvidersChanged, as a server that came up |
respond('open', answer) | Runs when a document opens; an answer that returns a promise delays the open |
An answer for a document whose text changed while pending rejects with StaleResultError, as a real service must. The fake models neither several clients of one document nor failing servers; test those in the LSP layer.
ManualTimers
The features wait before they ask: 80 ms before suggestions, 300 ms before a hover card. Pass ManualTimers as the timers argument of EditorLanguage to control the clock; realTimers, on setTimeout, is the default. advance(ms) runs what is due, and the test awaits the requests that started.
Keys and turns
keyOf(shortcut, apple?) is the keydown a person pressing a shortcut sends, in the shape FakeEditor.press takes. It reads a KeyShortcut, a key chord of the editor keymap or of @adecore/ui, and maps Space and the punctuation keys to their code. mod becomes Meta on an Apple platform and Ctrl elsewhere: the platform the shortcut names, else apple, else the page's. A null shortcut throws, so a test fails when the keymap has no key for a command.
settle() waits six turns of the event loop, which drains the promise chains of the fake service. Await it after a keypress or a refresh before asserting on what the features did.
import { keyOf, settle } from '@adecore/editor-react/testing';
editor.press(keyOf(language.shortcuts.rename));
await settle();