Summary/Intent
I'm writing tests for a VS Code extension that uses vscode.env.clipboard.writeText() to copy a generated string to the system clipboard.
I want to assert that the clipboard contains the correct value inside a test. I want to do an actual comparison, 1 to 1, just a hardcoded expected value vs what is actually on the clipboard.
Source Code Context
The extension code generates a string and calls vscode.env.clipboard.writeText(...).
looks something like this (removed a bunch of code for focus/brevity):
function myAwesomeMethod(resource) {
try {
// some main function argument validations... then
let someOutput = stringGenerator(resource, {});
vscode.env.clipboard.writeText(someOutput);
vscode.window.showInformationMessage('Cool!');
return;
} catch (e) {
vscode.window.showErrorMessage(`Something went wrong: ${e.message}`);
console.error('Error:', e);
}
}
The logic works when run inside VS Code manually.
Tests are run using @vscode/test-electron and Mocha.
I am not trying to simulate clipboard behavior, I want to assert the real clipboard contents.
My test code was all over the place, but this is my last iteration...
const path = require('path');
const fs = require('fs');
const assert = require('assert');
const { stringGenerator } = require('../../src/coreLogic');
suite('test clip contents', function () {
test('should match expected output', async function () {
const samplePath = path.resolve(__dirname, '../../fixtures/sample-001_small-project');
const expectedPath = path.resolve(__dirname, '../../fixtures/expected/sample-001_small-project.txt');
const expectedOutput = fs.readFileSync(expectedPath, 'utf-8');
const testConfig = {
// bunch of configs for the stringGenerator...
// confirmed to be working live/and via console prints in test env
};
const actualOutput = stringGenerator(samplePath, testConfig);
console.log('\n--- ACTUAL OUTPUT ---\n' + actualOutput + '\n--- END ACTUAL OUTPUT ---\n');
assert.strictEqual(actualOutput.trim(), expectedOutput.trim());
});
});
What I've tried
- (thinking is was an async issue) Reading clipboard with retry logic, basically looped like 100000 times checking for contents before erroring out
- Ensuring clipboard write is called. I threw some logs before and after the generator script is called and confirmed the output is being created and passed to the clipboard API
- Double checked that there were no configs that were overriding or mocking the VS Code API, or my own functions
Question
What on earth is the proper setup to be able to assert/compare/verify/view the contents of the clipboard in the test runner during test run time?
Any feedback appreciated.
Dependencies
"vscode": ^1.1.37
"@vscode/test-electron": ^2.4.1
"mocha": ^10.8.2
Node 18
macOS (M3)
VS Code 1.103.x (test instance)