My Visual Studio Code extension relies on a workspace configuration. So, in extension test, I would like to set the workspace configuration, and then test if the extension works properly.
Extension contribution definition looks like this:
"contributes": {
"configuration": {
"properties": {
"assistant": {
"type": "object",
"properties": {
"modifiers": {
"type": "string",
"default": "g"
},
"rules": {
"type": "array",
"default": [],
"items": {
"properties": {
"regex": {
"type": "string"
},
"message": {
"type": "string"
},
"modifiers": {
"type": "string",
"default": "g"
}
}
}
}
}
}
}
}
},
An example configuration looks like this:
{
"folders": [
{
"path": "code"
}
],
"settings": {
"assistant": {
"rules": [
{
"regex": "^::ng-deep.*$",
"message": "Prepend with :host, so that styles won't leak",
"modifiers": "gm"
},...
When I call:
Among other approaches i tried:
const assistant = vscode.workspace.getConfiguration("settings");
assistant.update("assistant", "", ConfigurationTarget.Global).then(
(value) => {
console.log(assistant.get("assistant"));
},
(value) => {
console.log(value);
}
);
It returns the error:
Error: Unable to write to User Settings because settings.assistant is not a registered configuration.
I have tried also with ConfigurationTarget. Workspace, and various ways to access settings but nothing seem to work properly.
The extension seems to be installed properly in the test vscode application.
How to change vscode extension properties programmatically?
You need to treat your test environment as if it were any other vscode project. How do you keep your settings per vscode project? By keeping them in .vscode/settings.json
. So the solution is to create workspace for your test vscode, with your settings as mentioned.
In the vscode-test docs you can see how to open a specific workspace when running the tests.
// test/runTest.ts
async function go() {
try {
const extensionDevelopmentPath = path.resolve(__dirname, '../../../')
const extensionTestsPath = path.resolve(__dirname, './suite')
const testWorkspace = path.resolve(__dirname, '../../../test-fixtures/')
/**
* Running another test suite on a specific workspace
*/
await runTests({
extensionDevelopmentPath,
extensionTestsPath: extensionTestsPath,
launchArgs: [testWorkspace]
})
// [...]
Write your settings in test-fixtures/.vscode/settings.json
and you'll be able to access test specific configuration in your tests.
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