VS Code supports running and debugging tests for your extension that require the VS Code API. These tests will run inside a special instance of VS Code, the
Extension Development Host, and have access to the full APIs. We refer to these tests as integration tests, because they go beyond unit tests that can run in isolation from a VS Code window. This documentation focuses on VS Code integration tests. For unit testing, you can use any popular testing framework, like Mocha or Jasmine.
The basic yo code generator extension project includes a sample test as well as the necessary infrastructure to run it.
After you’ve created a new extension and opened the project in VS Code, you can select the
Launch Tests configuration from the dropdown at the top of the Debug View.
With this configuration chosen, when you run
Debug: Start (F5), VS Code launches your extension in the
Extension Development Host instance and runs your tests. Test output goes to the Debug Console where you can see the test results.
The generated test uses the Mocha test framework for its test runner and library.
The extension project comes with a
test folder that includes an
index.ts file which defines the Mocha test runner configuration and an
extension.test.ts which has the example
Something 1 test. You can typically leave
index.ts untouched, but you can modify it to adjust the configuration of Mocha.
You can create more
test.ts files under the
test folder and they will automatically be built (to
out/test) and run. The test runner will only consider files matching the name pattern
Launch Tests configuration is defined in the project’s
.vscode\launch.json file. It is similar the
Launch Extension configuration with the addition of the
--extensionTestsPath argument which points to the compiled test files (assuming this is a TypeScript project).
You can set the file or folder that the test instance should open by inserting the path at the front of the argument list for the launch configuration.
This way you can run your tests with predictable content and folder structure.
If you decide to share your extension, you may not want to include the tests in your extension package. The
.vscodeignore file lets you exclude test files when you package and publish your extension with the
vsce publishing tool. By default, the
yo code generated extension project excludes the
You can run extension tests automatically on build machines like Travis CI.
In order to enable automated extension tests, the
vscode npm module provides a test command that will:
- download and unzip VS Code
- launch your extension tests inside VS Code
- print the results to the console and return with an exit code according to test success or failure
To enable this test command, open your
package.json and add the following entry to the
You can then enable Travis CI easily with a top-level
.travis.yml configuration like this:
The script above will run the tests on both Linux and Mac. Note that in order to run the tests on Linux, you need to have
before_install configuration as above to enable Linux to start VS Code from the build.
There are some optional environment variables to configure the test runner:
|Version of VS Code to run the tests against (e.g. |
|Full URL of a VS Code drop to use for running tests against|
|Location of the tests to execute|
|Location of a workspace to open for the test instance|