3

The vscode API documentation includes a page on how to test extensions. This works well when you have a single extension with no dependencies. However, in our case, we extensionDependencies since our extension depends on hbenl.vscode-test-explorer.

On our CI server, we have a problem that we cannot run the tests until this dependency is installed, but there is no API exposed by vscode's test API to install extensions.

We could download our own copy of vscode and use the --install-extension command to install this dependency, but that complicates things since now we are managing the download that the API used to manage.

There are two reasonable possibilities that I would like to explore:

  1. How can I disable the extensionDependencies when activating my extension?
  2. How can I download the dependency as part of the vscode-test run?
Andrew Eisenberg
  • 28,387
  • 9
  • 92
  • 148

3 Answers3

3

Somehow, I missed this in the documentation. Custom setup with vscode-test.

The code is available at the link, so I won't copy all of it. Here's the most relevant part:

    const vscodeExecutablePath = await downloadAndUnzipVSCode('stable');
    const cliPath = resolveCliPathFromVSCodeExecutablePath(vscodeExecutablePath);

    // Use cp.spawn / cp.exec for custom setup
    cp.spawnSync(cliPath, ['--install-extension', '<EXTENSION-ID-OR-PATH-TO-VSIX>'], {
      encoding: 'utf-8',
      stdio: 'inherit'
    });
Andrew Eisenberg
  • 28,387
  • 9
  • 92
  • 148
  • Didi this solve the issue for you? I would like to write a test for an extension that has another one(ms-python.python) as dependency but whenever I run the test I'm prompted with: Cannot activate the 'xxx' extension because it depends on the 'Python' extension, which is not installed. Would you like to install the extension and reload the window? Even with your solution :( – B3th4 Dec 15 '21 at 16:06
  • 1
    Yes, it did. I just forgot to accept the answer. – Andrew Eisenberg Jan 04 '22 at 20:45
0

Some additional information when trying to apply the solution of Andrew.

Add test-electro to your project

Inpackage.json
under "devDependencies":
add "@vscode/test-electron": "^1.6.2", ( choose an appropriate version, that suits you)

Find extension ID

Open the extension information page (like when you install an extension). You can copy the extension ID when you click on the cogwheel (see below)

DarkTrick
  • 2,447
  • 1
  • 21
  • 39
0

I took Andrew E.'s answer and built on it. Rather than hard-code one or more extension dependencies in the runTest.ts file, I leverage the package.json file itself so that any changes to extension dependencies are automatically accounted for in the integration tests. Here is my entire file:

import * as cp from 'child_process'
import * as path from 'path'
const packageJson = require('../../package.json')

import { downloadAndUnzipVSCode, resolveCliArgsFromVSCodeExecutablePath, runTests } from '@vscode/test-electron'

async function main() {
    try {
        // The folder containing the Extension Manifest package.json
        // Passed to `--extensionDevelopmentPath`
        const extensionDevelopmentPath = path.resolve(__dirname, '../../')

        // The path to test runner
        // Passed to --extensionTestsPath
        const extensionTestsPath = path.resolve(__dirname, './suite/index')

        const vscodeExecutablePath = await downloadAndUnzipVSCode()
        const [cli, ...args] = resolveCliArgsFromVSCodeExecutablePath(vscodeExecutablePath)

        if (packageJson.extensionDependencies) {
            for (const extensionId of packageJson.extensionDependencies) {
                cp.spawnSync(cli, [...args, '--install-extension', extensionId], {
                    encoding: 'utf-8',
                    stdio: 'inherit',
                })
            }
        }

        // Download VS Code, unzip it and run the integration test
        await runTests({ vscodeExecutablePath, extensionDevelopmentPath, extensionTestsPath })
    } catch (err) {
        console.error('Failed to run tests')
        process.exit(1)
    }
}

main()
Andrew Arnott
  • 80,040
  • 26
  • 132
  • 171