Quantcast
Channel: Active questions tagged visual-studio-code - Stack Overflow
Viewing all articles
Browse latest Browse all 99437

VSCode/Mocha exits prematurely when running extension tests

$
0
0

I am trying to UI test the command in my extension. When I run the test, it quits without any error message before reaching assert.equal() function.

The code of my test is as follows:

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

});

Output of the test:

$ 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.`);
        }
    });

};

I have also tried using done() function and waiting a specific amount of time, but it did not help:

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

});

The test exits at random places. Sometimes the tests passes correctly, but more often it quits before assertion. I believe this has something to with the code being asynchronous, but I have seen similar code in other tests, like here.


Viewing all articles
Browse latest Browse all 99437

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>