Skip to content

Commit e96c0c6

Browse files
authored
Add vscodeApi for mocking (#1648)
- Required by #1649 ## Checklist - [ ] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [ ] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [ ] I have not broken the cheatsheet
1 parent d69c4a9 commit e96c0c6

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed

packages/cursorless-vscode/src/constructTestHelpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { TestHelpers } from "@cursorless/vscode-common";
2020
import * as vscode from "vscode";
2121
import { VscodeIDE } from "./ide/vscode/VscodeIDE";
2222
import { toVscodeEditor } from "./ide/vscode/toVscodeEditor";
23+
import { vscodeApi } from "./vscodeApi";
2324

2425
export function constructTestHelpers(
2526
commandServerApi: CommandServerApi | null,
@@ -74,5 +75,6 @@ export function constructTestHelpers(
7475
},
7576
hatTokenMap,
7677
runIntegrationTests,
78+
vscodeApi,
7779
};
7880
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { workspace, window } from "vscode";
2+
import { VscodeApi } from "@cursorless/vscode-common";
3+
4+
/**
5+
* A very thin wrapper around the VSCode API that allows us to mock it for
6+
* testing. This is necessary because the test harness gets bundled separately
7+
* from the extension code, so if we just import the VSCode API directly from
8+
* the extension code, and from the test harness, we'll end up with two copies
9+
* of the VSCode API, so the mocks won't work.
10+
*/
11+
export const vscodeApi: VscodeApi = {
12+
workspace,
13+
window,
14+
editor: {
15+
setDecorations(editor, ...args) {
16+
return editor.setDecorations(...args);
17+
},
18+
},
19+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { workspace, window, TextEditor } from "vscode";
2+
3+
/**
4+
* Subset of VSCode api that we need to be able to mock for testing
5+
*/
6+
export interface VscodeApi {
7+
workspace: typeof workspace;
8+
window: typeof window;
9+
10+
/**
11+
* Wrapper around editor api for easy mocking. Provides various
12+
* {@link TextEditor} methods as static functions which take a text editor as
13+
* their first argument.
14+
*/
15+
editor: {
16+
setDecorations(
17+
editor: TextEditor,
18+
...args: Parameters<TextEditor["setDecorations"]>
19+
): ReturnType<TextEditor["setDecorations"]>;
20+
};
21+
}

packages/vscode-common/src/getExtensionApi.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
} from "@cursorless/common";
1414
import * as vscode from "vscode";
1515
import type { Language, SyntaxNode, Tree } from "web-tree-sitter";
16+
import { VscodeApi } from "./VscodeApi";
1617

1718
export interface TestHelpers {
1819
ide: NormalizedIDE;
@@ -42,6 +43,11 @@ export interface TestHelpers {
4243
): Promise<TestCaseSnapshot>;
4344

4445
runIntegrationTests(): Promise<void>;
46+
47+
/**
48+
* A thin wrapper around the VSCode API that allows us to mock it for testing.
49+
*/
50+
vscodeApi: VscodeApi;
4551
}
4652

4753
export interface CursorlessApi {

packages/vscode-common/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from "./notebook";
33
export * from "./testUtil/openNewEditor";
44
export * from "./vscodeUtil";
55
export * from "./runCommand";
6+
export * from "./VscodeApi";

0 commit comments

Comments
 (0)