There are several kinds of tests for the VS Code Extensions. This document describes them and gives pointers on how to run/debug them.
The test types from most preferred to least preferred are:
To run all tests, execute npm run compile && npm run test from the top-level
folder.
For packages that don't have any dependencies on vscode, you would write such tests using Mocha and Chai as you normally would for NPM modules.
For vscode extension packages, place your strict unit tests in the test/unit directory of your package and create an
npm script in your package.json like "test:unit": "./node_modules/.bin/_mocha --recursive out/test/unit"
for running the tests. Check out the "test:unit" scripts in extensions/analyticsdx-vscode-templates/package.json file
to see examples of how to configure code coverage reporting when running the tests.
These tests should not require a VS Code instance or a Salesforce server connection, and
can be run directly using npm run test:unit.
VS Code provides its own special way to run tests that require access to the extension development host. Basically, it launches your test in another instance of itself. You have control over what extensions are launched, the tests that are run, and also the workspace that it will run in. This gives you quite a bit of flexibility.
More information can be found at the doc site.
While the test runner is highly configurable, there are certain assumptions that will help make writing the tests easier.
- Ensure that your tests go into the
test/vscode-integrationfolder. - Ensure that you have an
index.tsfile in thetest/vscode-integrationfolder that follows what is in the standard configuration (copy from an existing one if you don't have it). - Ensure that your test files are named like
<something>.test.ts. The.test.in the middle is required. - Ensure that your .js test files are compiled into the
out/test/vscode-integrationdirectory.
To your package.json, you should add scripts like:
{
"scripts": {
"setup-vscode-integration": "node ../../scripts/link-vscode-integration-dependency analyticsdx-vscode-core analyticsdx-vscode-templates && node ../../scripts/download-vscode-for-tests && node ../../scripts/install-vsix-dependency salesforce.salesforcedx-vscode-core"
}
}where this setup task will:
- run
link-vscode-integration-dependencyfor your extension plus any of the other extension folders under extensions/ that your extension depends on - run
download-vscode-for-teststo setup a vscode installation for the tests - run
install-vsix-dependencyfor any other extensions required for your extensions
Those steps will create and update a .vscode-test/ folder, which will be used for running the tests later. Thus, the
.vscode-test/ folder should be put into your .gitignore and other .ignore files
such as .npmignore and .vscodeignore.
There are configurations already created for you at the top level .vscode/launch.json file.
If you make a new extension, you will need to edit .vscode/launch.json, e.g.:
{
"name": "Launch Salesforce <extension-name> Vscode-Integration Tests",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"${workspaceRoot}/test-assets/sfdx-simple",
"--extensionDevelopmentPath=${workspaceFolder}/extensions/analyticsdx-vscode-templates/.vscode-test/extensions",
"--extensionTestsPath=${workspaceRoot}/packages/analyticsdx-vscode-core/out/test/vscode-integration"
],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/extensions/*/out/**/*.js"],
"preLaunchTask": "analyticsdx-vscode-templates-setup-vscode-integration",
"internalConsoleOptions": "openOnSessionStart"
}The args are:
- The first parameter is a location to a folder that will serve as
the workspace to run the tests and should be set to match what your
test:vscode-integrationnpm script is going to use (normally,${workspaceRoot}/test-assets/sfdx-simple). --extensionDevelopmentPath- This governs what extensions are loaded, and should point to the.vscode-test/extensionsfolder under the extension, which is wherelink-vscode-integration-dependencyandinstall-vsix-dependencywill put stuff.--extensionTestsPath- This governs what tests are actually run. This must be an absolute path and cannot be a wildcard. This should be your extensionsout/test/vscode-integrationfolder.
For the preLaunchTask, this will also require a corresponding entry in .vscode/tasks.json to first compile and
run setup-vscode-integration, e.g.:
{
"label": "analyticsdx-vscode-templates-setup-vscode-integration",
"command": "npm",
"type": "shell",
"presentation": {
"focus": false,
"panel": "dedicated"
},
"options": {
"cwd": "${workspaceFolder}/extensions/analyticsdx-vscode-templates"
},
"args": ["run", "setup-vscode-integration"],
"isBackground": false,
"dependsOn": ["Compile"]
}You can then launch and debug the vscode-integration tests by going to the Debug view, selecting the appropriate launch target from the drop-down, and hitting the start button (or F5).
To you package.json, you should add a script like:
{
"scripts": {
"test:vscode-integration": "npm run setup-vscode-integration && node ../../scripts/run-vscode-integration-tests"
}
}There are some optional environment variables to configure the test runner:
| Name | Description |
|---|---|
CODE_VERSION |
Either 'insiders' or 'stable' |
If you are running this from the top-level root folder, you can issue npm run test:vscode-integration.
See VS Code's doc site for more information.
To test your extension with the upcoming version of VS Code, add another entry to your
package.json scripts to run your vscode-integration tests in VS Code - Insiders. The entry
should look like:
"test:vscode-insiders-integration": "cross-env CODE_VERSION=insiders npm run test:vscode-integration"
The methods above run the test against the extensions source folders. To run the tests against the generated
.vsix bundles, you can run npm run test:vsix-integration from the top-level folder. This will do a
build-all-vsixs first, then a regular build, then run test:vsix-integrations in
the extensions. (Note: build-all-vsixes will do a git clean and delete any untracked files, so don't have
any uncommitted or unstashed work when you run it.)
In the extension source folders, you can reuse the download-vscode-for-tests and install-vsix-dependency scripts to
setup the test vscode instance to point to the generated .vsix files, and the run-vscode-integration-tests script to
then run the same vscode integration tests, with script entries in the package.json like:
"setup-vsix-integration": "node ../../scripts/download-vscode-for-tests && node ../../scripts/install-vsix-dependency salesforce.salesforcedx-vscode-core ../analyticsdx-vscode-core/*.vsix ./*.vsix ./analyticsdx-vscode/*.vsix",
"test:vsix-integration": "npm run setup-vsix-integration && node ../../scripts/run-vscode-integration-tests"Some testing won't be possible or straight-foward to automate as above, including testing direct UI interaction or interactions with servers, and will require manual testing.
If the changes have already been published to the marketplace, then just either install or upgrade the extensions from within VS Code.
Otherwise, you can install the .vsix files from the GitHub build into VS Code:
-
If you have the extensions already installed, either from .vsix files or the marketplace, you should uninstall them first:
- In VSCode, go to the Extensions tab (View > Extensions in the menu)
- In the search bar there, enter @installed analyticsdx-vscode
- For each extension found, click the small gear icon on it and select Uninstall
- Exit VS Code
- In the
.vscode/extensionsin your home directory, remove anyanalyticsdx-vscode-*directories:- On Mac/Linux, you can do:
rm -r ~/.vscode/extensions/salesforce.analyticsdx-vscode-* - On Windows, you can do:
del /s/q %USERPROFILE%\.vscode\extensions\salesforce.analyticsdx-vscode-*
- On Mac/Linux, you can do:
- Restart VS Code
-
Go to the most recent build that has the changes you want, find the
extensionsartifact and download it. It should be a zip file of the .vsix files that are the various extensions. -
Unzip it and install each of the .vsix files; see here for instructions.
-
Restart VS Code and test things out.
If you want to go back to the published extensions, you will need to uninstall the extensions as per step 1 before installing from the marketplace.