typescriptvisual-studio-codevscode-extensions

Testing a VSCode extension that involves opening a folder/workspace


I'm working on a VSCode extension that takes the path of the file currently opened in the workspace into account.

So to have a reproducible test, I'm trying to open the test folder itself in VSCode and then open the test file in it, like this:

import * as vscode from "vscode";

test("whatever", async function() {
    let workspaceUri = vscode.Uri.file(__dirname);
    // the tests stop here...
    await vscode.commands.executeCommand("vscode.openFolder", workspaceUri);
    await vscode.workspace.openTextDocument(__filename);
})

Problem is when I do that, as probably referred to here, the tests just stop running before I actually test my code.

Is there a way I can safely open a workspace and use it during tests?


Solution

  • Have a look at the docs for testing extensions: Testing-extensions-docs

    In the .vscode/launch.json file of your extension in development, you can pass arguments like so (from the docs):

    "args": ["file or folder name", "--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ]
    

    So you could create a testing directory-structure with all the files that you care for in that folder you specify in the config and add those directories/files to your .vscodeignore (the default test-directory is in there already).

    the alternative that i have been using so far is using a bash script with the following content:

    #!/bin/bash
    
    # $0 = command itself
    # $1 = extension-directory(relative path)
    # $2 = process-id of code to kill
    
    [ -z "$1" ] && { echo "path-argument not supplied"; exit 1; }
    [ -z "$2" ] || { xkill -id $2; }
    
    cd $1
    vsce package
    file=$(find . -name "*.vsix")
    code --install-extension $file
    code &
    echo $!
    

    All that remains is opening a folder in the launched code-instance and executing any commands in question. Setting up an appropriate testing-environment seems more profitable to me in the longterm, at least when it can be predicted that many tests will have to be made.