我正在尝试对扩展程序中的命令进行UI测试。当我运行测试时,在退出assert.equal()
函数之前,它退出并没有任何错误消息。
我的测试代码如下:
suite("Extension Tests", () => {
const fixtureFolderLocation = "../fixture/";
test("Wrap in parentheses", async () => {
const uri = vscode.Uri.file(
path.join(__dirname, fixtureFolderLocation, "fixture2.html"),
);
const document = await vscode.workspace.openTextDocument(uri);
const editor = await vscode.window.showTextDocument(document);
editor.selection = new vscode.Selection(new vscode.Position(0, 0), new vscode.Position(0, 4));
await vscode.commands.executeCommand("nkatex.wrapInParentheses");
const text = document.getText(new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 21)));
assert.equal(text, "\\left ( word\\right ) ");
});
});
测试结果:
$ node ./out/test/runTest.js
Found .vscode-test/vscode-1.42.1. Skipping download.
[main 2020-02-16T15:06:03.708Z] update#setState idle
Extension Tests
Exit code: 0
Done
Done in 17.30s.
runTest.ts
:
async function main() {
try {
// Download VS Code, unzip it and run the integration test
await runTests({
extensionDevelopmentPath: path.resolve(__dirname, "../../"),
extensionTestsPath: path.resolve(__dirname, "./suite"),
launchArgs: [
"--disable-extensions",
],
});
} catch (err) {
console.error("Failed to run tests");
process.exit(1);
}
}
main();
index.ts
:
export const run = async (): Promise<void> => {
// Create the mocha test
const mocha = new Mocha({
ui: "tdd",
});
mocha.useColors(true);
const testsRoot = path.resolve(__dirname, "..");
const files = await async.glob("**/**.test.js", { cwd: testsRoot });
for (const file of files) {
mocha.addFile(path.resolve(testsRoot, file));
}
mocha.run((failureNumber) => {
if (failureNumber > 0) {
throw new Error(`${failureNumber} tests failed.`);
}
});
};
我也尝试使用done()
函数并等待特定时间,但这没有帮助:
const sleep = (ms = 1000) => new Promise((resolve) => setTimeout(resolve, ms));
suite("Extension Tests", () => {
const fixtureFolderLocation = "../fixture/";
test("Wrap in parentheses", async (done) => {
const uri = vscode.Uri.file(
path.join(__dirname, fixtureFolderLocation, "fixture2.html"),
);
const document = await vscode.workspace.openTextDocument(uri);
const editor = await vscode.window.showTextDocument(document);
await sleep(2000);
editor.selection = new vscode.Selection(new vscode.Position(0, 0), new vscode.Position(0, 4));
await vscode.commands.executeCommand("nkatex.wrapInParentheses");
await sleep();
const text = document.getText(new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 21)));
assert.equal(text, "\\left ( word\\right ) ");
done();
});
});
测试在随机位置退出。有时测试可以正确通过,但更多情况是在声明之前退出。我认为这与异步代码有关,但是我在其他测试中也见过类似的代码,例如here。