Skip to content

Add vscodeApi for mocking #1648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/cursorless-vscode/src/constructTestHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -74,5 +75,6 @@ export function constructTestHelpers(
},
hatTokenMap,
runIntegrationTests,
vscodeApi,
};
}
19 changes: 19 additions & 0 deletions packages/cursorless-vscode/src/vscodeApi.ts
Original file line number Diff line number Diff line change
@@ -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);
},
},
};
21 changes: 21 additions & 0 deletions packages/vscode-common/src/VscodeApi.ts
Original file line number Diff line number Diff line change
@@ -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<TextEditor["setDecorations"]>
): ReturnType<TextEditor["setDecorations"]>;
};
}
6 changes: 6 additions & 0 deletions packages/vscode-common/src/getExtensionApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,6 +43,11 @@ export interface TestHelpers {
): Promise<TestCaseSnapshot>;

runIntegrationTests(): Promise<void>;

/**
* A thin wrapper around the VSCode API that allows us to mock it for testing.
*/
vscodeApi: VscodeApi;
}

export interface CursorlessApi {
Expand Down
1 change: 1 addition & 0 deletions packages/vscode-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./notebook";
export * from "./testUtil/openNewEditor";
export * from "./vscodeUtil";
export * from "./runCommand";
export * from "./VscodeApi";