Skip to content

Simplify Autocomplete testing using MockTextEditor #1068

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

hassoncs
Copy link
Contributor

@hassoncs hassoncs commented Jul 3, 2025

Introduces MockTextDocument and MockTextEditor classes to make creating documents for tests easier!

The previous tests were tricky to maintain because it's hard to visualize where the cursor is in a given document. These new test utils should make that easier going forward!

From this:

		const mockDocument = {
			lineAt: vi.fn().mockReturnValue({
				text: "    ", // whitespace line
			}),
			getText: vi.fn().mockReturnValue(""),
		}
		const mockPosition = new vscode.Position(0, 3) // Cursor inside a whitespace line

To this (all in one!):

const mockEditor = MockTextEditor.create(` ␣ `)
const mockPosition = mockEditor.selection.active;
const mockDocument = mockEditor.document;

The AutocompleteProvider.test.ts file is updated to leverage these new mock classes, replacing manual mocking of vscode.Document and vscode.Position objects. This change specifically improves the clarity and maintainability of tests related to whitespace handling and cursor positioning.

Copy link

changeset-bot bot commented Jul 3, 2025

⚠️ No Changeset found

Latest commit: 5b65edb

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

… for simpler Autocomplete tests

Introduces `MockTextDocument` and `MockTextEditor` classes to provide a more robust and realistic testing environment for the autocomplete provider. This refactoring simplifies test setup and makes tests more readable by allowing direct representation of editor state (document content and cursor position).

The `AutocompleteProvider.test.ts` file is updated to leverage these new mock classes, replacing manual mocking of `vscode.Document` and `vscode.Position` objects. This change specifically improves the clarity and maintainability of tests related to whitespace handling and cursor positioning.

The previous tests were hard to maintain because it's hard to visualize where the cursor is in a given document. These new test utils should make that easier going forward!
@hassoncs hassoncs force-pushed the better-mock-vscode-for-tests branch from 33ec774 to f1b6677 Compare July 3, 2025 18:50
@markijbema
Copy link
Collaborator

I like it!

I have one option which might make it slightly more flexible:

MockTextEditor.create((cursor) => ` {cursor} `)

that way we might also mock selections and such by enabling more parameters; but it is slightly more noisy. Totally up to you and @catrielmuller

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants