VS 代码扩展 - 编写包含内联输入文档的测试

VS Code Extensions - writing tests that include inline input documents

跟随 this guide 为 VS 代码编写简单的扩展,并对其进行自动化测试。我需要在当前文档中包含一些预期的文本,以便测试 运行 against.

我找到了 this example 的脚本,可以打开现有的文本文件并将其用作输入。

相反,我正在尝试 运行 测试针对测试代码本身中保存的输入和预期结果。这似乎更健壮和独立。

这是我的尝试:

import * as assert from 'assert';

// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
import * as vscode from 'vscode';
import * as taxi from '../../extension';

suite('Extension Test Suite', () => {
    vscode.window.showInformationMessage('Start all tests.');


    test('displayDiagnostics - empty, no summary', async () => {

        let dcoll = vscode.languages.createDiagnosticCollection('taxi');
        const startTime = new Date().getTime();
        const showSummary = false;
        const result: taxi.Result = {
            // eslint-disable-next-line @typescript-eslint/naming-convention
            total_errors: 0,
            // eslint-disable-next-line @typescript-eslint/naming-convention
            total_warnings: 0,
            errors: [],
            warnings: [],
        };
        vscode.workspace.openTextDocument({
            content: 'The quick brown fox'
        })
            .then((doc) => {
                const diags = taxi.displayDiagnostics(result, doc, startTime, showSummary);
                assert.strictEqual(diags.length, 0);
            }, (error) => {
                assert.fail(error);
            });
    });

: : 等等等等

openTextDocument 似乎几乎可以正常工作,但我看到警告说“没有活动的文本编辑器”。

我在设置测试时做错了什么吗?

即使我包含了一条注定失败的行,测试似乎也通过了,例如

                assert.strictEqual(1, 2);
(node:79286) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `Electron --trace-deprecation ...` to show where the warning was created)
Congratulations, your extension 'highlight-trailing-white-spaces' is now active!
updateDecorations(): no active text editor.

  Extension Test Suite
    ✔ displayDiagnostics - empty, no summary
    ✔ displayDiagnostics - empty with summary
    ✔ displayDiagnostics - errors and warnings, no summary
    ✔ displayDiagnostics
  4 passing (24ms)
The extension 'highlight-trailing-white-spaces' is now deactivated.
FAILED to register filesystem provider of vscode.git-extension for the scheme git
Canceled: Canceled
    at Object.F [as canceled] (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:5:1172)
    at /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:97:9076
    at Array.forEach (<anonymous>)
    at i.dispose (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:97:9024)
    at p.terminate (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:102:694)
    at s (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:112:36448)
    at Socket.<anonymous> (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:112:34061)
    at Socket.emit (<node_internals>/events.js:327:22)
    at Pipe.<anonymous> (<node_internals>/net.js:673:12)
    at Pipe.callbackTrampoline (internal/async_hooks.js:131:14) {name: 'Canceled', stack: 'Canceled: Canceled
    at Object.F [as canceled]…ckTrampoline (internal/async_hooks.js:131:14)', message: 'Canceled'}

这是我使用 await 的简单解决方案 .. 诀窍是调用 showTextDocument。这会弹出一个编辑器 window,其中包含 one-line 文本。Copied from here.

import * as assert from 'assert';

// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
import * as vscode from 'vscode';
import * as taxi from '../../extension';

suite('Taxi for Email Validation Extension Test Suite', () => {
    vscode.window.showInformationMessage('Start all tests.');

    test('displayDiagnostics - empty, no summary', async () => {

        let dcoll = vscode.languages.createDiagnosticCollection('taxi');
        const startTime = new Date().getTime();
        const showSummary = false;
        const result: taxi.Result = {
            // eslint-disable-next-line @typescript-eslint/naming-convention
            total_errors: 0,
            // eslint-disable-next-line @typescript-eslint/naming-convention
            total_warnings: 0,
            errors: [],
            warnings: [],
        };
        const doc = await vscode.workspace.openTextDocument({
            content: 'The quick brown fox'
        });
        await vscode.window.showTextDocument(doc);
        const diags = taxi.displayDiagnostics(result, doc, startTime, showSummary);
        assert.strictEqual(diags.length, 0);

        // See 
        await vscode.commands.executeCommand('workbench.action.closeActiveEditor');

    });
});