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
beforefunctionasync, andawaitingexecuteCommand. - Adding a
doneparameter, for bothasyncand non-async, and callingdone()at the end of thebeforefunction, or in thethenclause of theexecuteCommandpromise. - Returning or not returning the
executeCommandpromise.
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.