In a test environment during test runtime, how can I assert values copied to the clipboard via the vscode.env.clipboard.writeText(value) method? #2797
Unanswered
jtroussard
asked this question in
Extension Development QnA
Replies: 1 comment
-
Hi @jtroussard , My Copy Word in Cursor extension plays with Clipboard API, and I do some tests as well, but instead of writing and reading from the clipboard, I do half in the clipboard and half in the VS Code Editor. Besides some delay issues (as you commented) when I started to add tests, it was working just fine, up until some recent issues in CI, which I must address. Feel free to take a look at my extension's source to see if something may help you. ...
// put some text in the clipboard
await vscode.env.clipboard.writeText('thank');
// put the cursor at the `taking'
const selDestiny = new vscode.Selection(new vscode.Position(2, 31), new vscode.Position(2, 31));
vscode.window.activeTextEditor.selection = selDestiny;
// runs the command
await vscode.workspace.getConfiguration('copyWord').update('pasteWordBehavior', 'original');
await vscode.commands.executeCommand('copy-word.paste');
await vscode.workspace.getConfiguration('copyWord').update('pasteWordBehavior', 'replaceWordAtCursor');
// get the word at the position of the paste
const text = vscode.window.activeTextEditor.document.getText(
vscode.window.activeTextEditor.document.getWordRangeAtPosition(vscode.window.activeTextEditor.selection.active));
await vscode.commands.executeCommand('undo');
// assert - the new select must be `tathankking`
assert.ok(text === 'tathankking'); More details in the repo: https://github.com/alefragnani/vscode-copy-word/blob/master/src/test/suite/commands.test.ts Hope this helps |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I'm testing a VSCode extension that uses
vscode.env.clipboard.writeText(...)
to copy a generated string to the system clipboard.In my test, I want to assert that the clipboard contains the expected value. However,
vscode.env.clipboard.readText()
always returns an empty string during test runs, even when retrying with delays.The clipboard write is confirmed to execute with valid content. No mocking or stubbing of the clipboard API is involved. This is using @vscode/test-electron with Mocha and running live extension code.
Goal
I need a way to perform a real clipboard assertion inside the test using VSCode’s API
Environment
macOS (M3)
VSCode 1.103.x
@vscode/test-electron 2.4.1
vscode 1.1.37
Node 18
More details:
https://stackoverflow.com/questions/79738104/vscode-extension-test-runner-how-to-assert-values-copied-to-the-clipboard
Repo and Branch
https://github.com/jtroussard/treematic/tree/output-check-tests
More context
https://stackoverflow.com/questions/79738104/vscode-extension-test-runner-how-to-assert-values-copied-to-the-clipboard
Any insights appreciated.
Beta Was this translation helpful? Give feedback.
All reactions