I have a vscode extension (Typescript) which uses configuration settings. I'd like to unit test the behavior of my extension based on specific configuration settings.
My issue is that it looks like the configuration changes are only written after the test executes, which means the test runs without the changed settings being applied. Here is what I have in my test:
test("LoadConfiguration - Shows error when configuration can't be loaded", () => {
let settings = vscode.workspace.getConfiguration("my-extension");
settings.update("MySettingCollection", [{ "param1": "", "param2": "someValue" }], true);
ConfigurationLoader.LoadConfiguration();
//asserts ommitted
});
In ConfigurationLoader.LoadConfiguration
, I have:
public LoadConfiguration() : boolean {
let configuration = vscode.workspace.getConfiguration('my-extension');
if (configuration.has('MySettingCollection')) {
//logic to read MySettingCollection...
}
}
Is there some way I can say "set up the test with these values" and have that run and write the file before the test is executed? Perhaps I need to run something asynchronously? I've looked at Mocha's before
and beforeEach
but I'm not sure how that would fit in with what I'm trying to do.
The answer is to make everything async
! The update
function returns a Thenable
which is awaitable. This needs to be awaited to make sure the configuration is written before the execution of the calling method proceeds.
The test callback is now marked async
which is automatically handled by the mocha framework. ConfigurationLoader.LoadConfiguration
is also made async.
test("LoadConfiguration - Shows error when configuration can't be loaded", async () => {
let settings = vscode.workspace.getConfiguration("my-extension");
await settings.update("MySettingCollection", [{ "param1": "", "param2": "someValue" }], true);
await ConfigurationLoader.LoadConfiguration()
//asserts ommitted
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With