VSCode: Tests won't proceed after using executeCommand to open a sample project

蹲街弑〆低调 提交于 2021-01-28 05:26:56

问题


I'm trying to add some unit tests to a Visual Studio Code extension I'm developing. I've followed the recipe for setting up extension testing described here: https://code.visualstudio.com/api/working-with-extensions/testing-extension

This recipe, however, doesn't show anything useful being done to actually test an extension, just the skeleton for getting set up to do so.

For the testing I want to do, one of the first things I want to do is open a sample project. And that's where I get stuck. Here is just one of many variations I've tried for opening a project folder:

import assert from 'assert';
import { after, before, it } from 'mocha';
import path from 'path';
import { commands, Extension, extensions, Uri, window } from 'vscode';

suite('Extension Tests', () => {
  let extension: Extension<any>;
  const projectFolder = Uri.file(path.join(__dirname, '../../../test/suite/sample-project'));

  window.showInformationMessage('Start all tests.');

  before(() => {
    extension = extensions.getExtension('kshetline.ligatures-limited');
    const cmd = commands.executeCommand('vscode.openFolder', projectFolder).then(
      () => console.log('opened'),
      () => console.log('didn\'t open'));
    console.log('before');
    return cmd;
  });

  after(() => {
    console.log('after');
  });

  it('should load and activate extension', () => {
    assert.ok(extension);
    assert.ok(extension.isActive);
  });

  it('second test', () => {
    assert.ok(true);
  });
});

If I take out the executeCommand, both tests run and the test suite properly terminates, with test window closing.

If I leave the executeCommand in, sometimes neither test is executed, sometimes just the first test. The test suite doesn't terminate -- the test window is left open, and I have to manually stop the test.

Variations I've tried:

  • Making the before function async, and awaiting executeCommand.
  • Adding a done parameter, for both async and non-async, and calling done() at the end of the before function, or in the then clause of the executeCommand promise.
  • Returning or not returning the executeCommand promise.

Examples of how to do this correctly are hard to come by. I looked at the repositories for one extension after another, and it seems doing tests on extensions isn't very popular. VSCode extension writers in general don't apparently bother with tests very often. (And maybe the trouble I'm having is the reason why?)

I can see that the sample project does indeed open, and I see no console errors to indicate why the testing gets stuck and fails to proceed any further.


回答1:


For reasons that aren't fully clear to me, using the vscode.openFolder command while a unit test is running is simply A Bad Thing to Do. I guess it somehow disrupts the connections between the test window and the test environment.

Fortunately in this particular situation I can get by with opening just one folder, and that can be done in my `launch.json' config before the test code is launched.

{
  "version": "0.1.0",
  "configurations": [
    {
      ...
    },
    {
      "name": "Test Extension",
      "type": "extensionHost",
      "request": "launch",
      "runtimeExecutable": "${execPath}",
      "args": [
        "--extensionDevelopmentPath=${workspaceFolder}",
        "--extensionTestsPath=${workspaceFolder}/out/test/suite",
        "${workspaceFolder}/test/suite/sample-project"  // <-- project to open goes here
      ],
      "outFiles": [
        "${workspaceFolder}/out/test/**/*.js"
      ],
      "preLaunchTask": "npm"
    },
  ]
}

Once I've got my testing environment bootstrapped with this one sample project open to begin with, I can safely use the vscode.open command to open specific documents from within the sample project folder, and then monitor and test what my extension does with those documents.



来源:https://stackoverflow.com/questions/61889998/vscode-tests-wont-proceed-after-using-executecommand-to-open-a-sample-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!