Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get extensionContext in vscode extension unit test?

Currently I am writing unit tests about vscode extension. But some functions are using extensionContext and I can't get extensionContext in unit tests. Any way to get it?

like image 941
user12087834 Avatar asked May 27 '20 06:05

user12087834


People also ask

How do I get a list of VS Code extensions?

Bring up the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of VS Code or the View: Extensions command (Ctrl+Shift+X). This will show you a list of the most popular VS Code extensions on the VS Code Marketplace.

Does VS Code use LSP?

Language Server Protocol (LSP) is a protocol implemented by VSCode to solve some of the language extension pain points. LSP is a JSON RPC-based protocol.


1 Answers

Just came across this question, because I had exactly the same problem.

It looks like you can do the following in a test:

const ext = vscode.extensions.getExtension("publisher.extensionName");

And you can return anything from your activate function, so you could decide to return the extension context (or anything else that you need) there:

export async function activate(
  context: vscode.ExtensionContext
): Promise<vscode.ExtensionContext> {
  // Your activation code...

  return context;
}

And then you can access the context in the test:

const ext = vscode.extensions.getExtension("publisher.extensionName");
const myExtensionContext = await ext.activate();


You find publisher.extensionName information in package.json of the extension:

{
    "publisher": "myself",
    "name": "myextension",
    "displayName": "My Extension",
    "description": "",
    "version": "1.0.0",
  ...
like image 72
Wilhelm Klopp Avatar answered Dec 13 '22 07:12

Wilhelm Klopp