VSCode:使用 executeCommand 打开示例项目后,测试不会继续 [英] VSCode: Tests won't proceed after using executeCommand to open a sample project

查看:45
本文介绍了VSCode:使用 executeCommand 打开示例项目后,测试不会继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向我正在开发的 Visual Studio Code 扩展添加一些单元测试.我遵循了此处描述的设置扩展测试的方法:https://code.visualstudio.com/api/working-with-extensions/testing-extension

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);
  });
});

如果我取出 executeCommand,两个测试都会运行并且测试套件会正确终止,测试窗口会关闭.

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

如果我保留 executeCommand ,有时两个测试都不会执行,有时只是第一个测试.测试套件不会终止——测试窗口保持打开状态,我必须手动停止测试.

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.

我尝试过的变体:

  • 使before函数async,并awaitexecuteCommand.
  • async 和非异步添加done 参数,并在 的末尾调用done()before 函数,或在 executeCommand 承诺的 then 子句中.
  • 返回或不返回 executeCommand 承诺.
  • 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.

很难找到如何正确执行此操作的示例.我查看了一个又一个扩展的存储库,似乎对扩展进行测试并不是很流行.VSCode 扩展编写者通常不会经常打扰测试.(也许我遇到的麻烦就是原因?)

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.

推荐答案

由于我不完全清楚的原因,在单元测试运行时使用 vscode.openFolder 命令只是 A坏事要做.我猜它以某种方式破坏了测试窗口和测试环境之间的连接.

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.

幸运的是,在这种特殊情况下,我可以只打开一个文件夹,这可以在启动测试代码之前在我的 `launch.json' 配置中完成.

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"
    },
  ]
}

一旦我用这个示例项目启动了我的测试环境,我就可以安全地使用 vscode.open 命令从示例项目文件夹中打开特定文档,并且然后监控和测试我的扩展程序对这些文档的处理.

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.

这篇关于VSCode:使用 executeCommand 打开示例项目后,测试不会继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆