diff --git a/packages/cursorless-vscode/src/constructTestHelpers.ts b/packages/cursorless-vscode/src/constructTestHelpers.ts index 3d2941dd2a..a68539edb0 100644 --- a/packages/cursorless-vscode/src/constructTestHelpers.ts +++ b/packages/cursorless-vscode/src/constructTestHelpers.ts @@ -20,6 +20,7 @@ import { TestHelpers } from "@cursorless/vscode-common"; import * as vscode from "vscode"; import { VscodeIDE } from "./ide/vscode/VscodeIDE"; import { toVscodeEditor } from "./ide/vscode/toVscodeEditor"; +import { vscodeApi } from "./vscodeApi"; export function constructTestHelpers( commandServerApi: CommandServerApi | null, @@ -74,5 +75,6 @@ export function constructTestHelpers( }, hatTokenMap, runIntegrationTests, + vscodeApi, }; } diff --git a/packages/cursorless-vscode/src/vscodeApi.ts b/packages/cursorless-vscode/src/vscodeApi.ts new file mode 100644 index 0000000000..8275a485c3 --- /dev/null +++ b/packages/cursorless-vscode/src/vscodeApi.ts @@ -0,0 +1,19 @@ +import { workspace, window } from "vscode"; +import { VscodeApi } from "@cursorless/vscode-common"; + +/** + * A very thin wrapper around the VSCode API that allows us to mock it for + * testing. This is necessary because the test harness gets bundled separately + * from the extension code, so if we just import the VSCode API directly from + * the extension code, and from the test harness, we'll end up with two copies + * of the VSCode API, so the mocks won't work. + */ +export const vscodeApi: VscodeApi = { + workspace, + window, + editor: { + setDecorations(editor, ...args) { + return editor.setDecorations(...args); + }, + }, +}; diff --git a/packages/vscode-common/src/VscodeApi.ts b/packages/vscode-common/src/VscodeApi.ts new file mode 100644 index 0000000000..34048bb2b0 --- /dev/null +++ b/packages/vscode-common/src/VscodeApi.ts @@ -0,0 +1,21 @@ +import { workspace, window, TextEditor } from "vscode"; + +/** + * Subset of VSCode api that we need to be able to mock for testing + */ +export interface VscodeApi { + workspace: typeof workspace; + window: typeof window; + + /** + * Wrapper around editor api for easy mocking. Provides various + * {@link TextEditor} methods as static functions which take a text editor as + * their first argument. + */ + editor: { + setDecorations( + editor: TextEditor, + ...args: Parameters + ): ReturnType; + }; +} diff --git a/packages/vscode-common/src/getExtensionApi.ts b/packages/vscode-common/src/getExtensionApi.ts index 6c083ed85b..079f85b704 100644 --- a/packages/vscode-common/src/getExtensionApi.ts +++ b/packages/vscode-common/src/getExtensionApi.ts @@ -13,6 +13,7 @@ import type { } from "@cursorless/common"; import * as vscode from "vscode"; import type { Language, SyntaxNode, Tree } from "web-tree-sitter"; +import { VscodeApi } from "./VscodeApi"; export interface TestHelpers { ide: NormalizedIDE; @@ -42,6 +43,11 @@ export interface TestHelpers { ): Promise; runIntegrationTests(): Promise; + + /** + * A thin wrapper around the VSCode API that allows us to mock it for testing. + */ + vscodeApi: VscodeApi; } export interface CursorlessApi { diff --git a/packages/vscode-common/src/index.ts b/packages/vscode-common/src/index.ts index 15435d5cc6..de3a299986 100644 --- a/packages/vscode-common/src/index.ts +++ b/packages/vscode-common/src/index.ts @@ -3,3 +3,4 @@ export * from "./notebook"; export * from "./testUtil/openNewEditor"; export * from "./vscodeUtil"; export * from "./runCommand"; +export * from "./VscodeApi";