'Testing VSCode extensions - how to verify that decorations are set

I have an extension which adds text decorations at the end of some of the lines. I'd like to write a test which verifies that the text decorations are added and also to assert in the test that their value is correct.

suite('Extension Tests', () => {
  test('Should work', async() => {
    const fixturePath = path.join(__dirname, '..', '..', 'test', 'fixtures');
    const uri = vscode.Uri.file(path.join(fixturePath, 'a.js'));
    const document = await vscode.workspace.openTextDocument(uri);
    await vscode.window.showTextDocument(document);
    window.activeTextEditor.getDecorations(); //???
  });
});

It seems that the getDecorations API is missing from editor. What can I do to resolve this?



Solution 1:[1]

As of VSCode 1.37.1 (2019-08-30), the answer is no, there is no way to write an automated test for decorations using the decorations extensions API.

There are automated tests of decorations in the vscode sources, particularly modelDecorations.test.ts, but they use private APIs.

Microsoft has an example extension that uses decorators, decorator-sample, but it has no tests.

There are a few open issues related to this, although none directly addresses the inability to enumerate existing decorations:

  • 50346: Lacking decorations-related APIs (mainly about incremental update for better performance); closed as duplicate of:

  • 585: Provide an API for advanced/semantic source highlighting

  • 50415: Missing API for knowing when an extension's decorations get trashed

  • 54938: Make decoration provider API public

Therefore I recommend filing a new issue.


2022-04-27: As noted in a comment, issue 136164: Provide Access to a TextEditors Decorations for Testing Purposes was filed on 2021-10-30. It was then closed as unnecessary because you can test that your extension calls the extension API as intended. I find this response inadequate because there's no way to test that the effect is as intended (for example, TextEditor.setDecorations removes existing decorations that have the same type, and that's a subtle interaction that would only be revealed by examining the effects), but the issue is locked so yet another issue would have to be filed to raise an objection.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1