[SOLVED] How to write a test for window.showInformationMessage vscode extension API

Issue

This Content is from Stack Overflow. Question asked by gnikit

How would one unittest, with the default mocha framework for extensions, the action of selecting one of the items in an information/warning/error message that is part of a VSCode extension?

function foo() {
  const GoToHelp = 'Go to Help';
  vscode.window.showInformationMessage('Click for more Info', GoToHelp).then(selection => {
    if (selection === GoToHelp) {
      vscode.env.openExternal(vscode.Uri.parse('example.com'));
    }
  });
}

I know that you can test the individual items associate with an action, but doing so will not catch bugs arising from how the item selection integrates with the rest of the extension e.g. the action downloads an external tool, which is spawned as a process and needs to be awaited for the process to complete successfully before the next window action is performed.



Solution

A solution to this would be to use RedHat’s vscode-extension-tester, specifically https://github.com/redhat-developer/vscode-extension-tester/wiki/Notification. Although, this would not be ideal since you would have to rely on an additional package and not pure mocha.js.


This Question was asked in StackOverflow by gnikit and Answered by gnikit It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?