English

テスト

vimee インテグレーションのテスト方法

vimee は純粋関数で構築されているため、テストは簡単です — モックなし、DOM なし、入力と出力だけ。

@vimee/testkit を使う

npm install -D @vimee/testkit
import { vim } from "@vimee/testkit";
import { expect, test } from "vitest";

test("x deletes character under cursor", () => {
  const v = vim("hello", { cursor: [0, 0] });
  v.type("x");

  expect(v.content()).toBe("ello");
  expect(v.cursor()).toEqual({ line: 0, col: 0 });
});

testkit なしでテスト

コア API を使ったプレーンなアサーションでもテストできます:

import { createInitialContext, processKeystroke, TextBuffer } from "@vimee/core";
import { expect, test } from "vitest";

test("enter insert mode with i", () => {
  const buffer = new TextBuffer("hello");
  const ctx = createInitialContext({ line: 0, col: 0 });
  const { newCtx } = processKeystroke("i", ctx, buffer);
  expect(newCtx.mode).toBe("insert");
});

一般的なテストパターン

モーションのテスト

test("w moves to next word", () => {
  const v = vim("hello world");
  v.type("w");

  expect(v.cursor()).toEqual({ line: 0, col: 6 });
});

オペレーターのテスト

test("dw deletes a word", () => {
  const v = vim("hello world");
  v.type("dw");

  expect(v.content()).toBe("world");
});

モード変更のテスト

test("i enters insert mode, Esc returns to normal", () => {
  const v = vim("hello");
  v.type("i");
  expect(v.mode()).toBe("insert");

  v.type("<Esc>");
  expect(v.mode()).toBe("normal");
});

キーストロークのチェイン

test("ci\" changes inside quotes", () => {
  const v = vim('say "hello" please', { cursor: [0, 5] });
  v.type('ci"new text<Esc>');

  expect(v.content()).toBe('say "new text" please');
});