简体   繁体   中英

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?

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.

You can also use VSBrowser.instance.openResources(path) to open a workspace/folder in VSCode instance;

import { WebDriver, VSBrowser } from 'vscode-extension-tester';
import path = require('path');
...

suite('Sample Test suite XYZ', () => {
    const test_project_path = path.join(__dirname, "path", "to", "prj");
    
    setup(async () => {
        driver = VSBrowser.instance.driver;
        driver.manage().timeouts().implicitlyWait(1000);
    });

    test("Open a workspace in VSCode instance", async () => {
        await VSBrowser.instance.openResources(path.resolve(test_project_path));
    });

});

...

The accepted answer does not work for me in 2023. I could not get VS Code to open a workspace through modifying the launch.json .

Instead, @vscode/test-electron runTests takes a launchArgs argument. If the first argument is a directory this will be opened as workspace.

await runTests({extensionDevelopmentPath, 
                extensionTestsPath, 
                launchArgs:[path_to_directory] 
               });

For testing, I set this to a temporary directory created by tmp-promise . My runTests.ts is:

import * as path from "path";
import { runTests } from "@vscode/test-electron";
import { file, dir } from 'tmp-promise';

async function main() {
  try {
    const extensionDevelopmentPath = path.resolve(__dirname, "../../");
    const extensionTestsPath = path.resolve(__dirname, "./suite/index");

    const {path:tempdir, cleanup} = await dir({unsafeCleanup:true});
    await runTests({extensionDevelopmentPath, 
                    extensionTestsPath, 
                    launchArgs:[tempdir] });
    cleanup();
  } catch (err) {
    console.error("Failed to run tests");
    process.exit(1);
  }
}

main();

The sample in the VS Code test repository is good place to look to get a good, presumably up-to-date, example to work from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM