Testing Extensions
Visual Studio Code supports running and debugging tests for your extension. These tests will run inside a special instance of VS Code named the Extension Development Host, and have full access to the VS Code API. We refer to these tests as integration tests, because they go beyond unit tests that can run without a VS Code instance. This documentation focuses on VS Code integration tests.
Overview
If you are using the Yeoman Generator to scaffold an extension, integration tests are already created for you.
In the generated extension, you can use npm run test
or yarn test
to run the integration tests that:
- Downloads and unzips latest version of VS Code.
- Runs the Mocha tests specified by the extension test runner script.
Quick Setup: The test CLI
The VS Code team publishes a command-line tool to run extension tests. You can find an example in the extensions sample repo.
The test CLI provides quick setup, and also allows you to easily run and debug tests of the VS Code UI using the Extension Test Runner. The CLI exclusively uses Mocha under the hood.
To get started, you'll want to first install the @vscode/test-cli
module, as well as @vscode/test-electron
module that enables tests to be run in VS Code Desktop:
npm install --save-dev @vscode/test-cli @vscode/test-electron
After installing the modules, you'll have the vscode-test
command line, which you can add to the scripts
section in your package.json
:
{
"name": "my-cool-extension",
"scripts": {
+ "test": "vscode-test"
vscode-test
looks for a .vscode-test.js/mjs/cjs
file relative to the current working directory. This file provides the configuration for the test runner, and you can find the entire definition here.
Common options include:
- (required)
files
- A pattern, list of patterns, or absolute paths containing the tests to run. version
- The version of VS Code to use for running tests (defaults tostable
).workspaceFolder
- The path to a workspace to open during tests.extensionDevelopmentPath
- The path to your extension folder (defaults to the directory of the config file).mocha
- An object containing additional options to pass to Mocha.
The configuration might be as simple as:
// .vscode-test.js
const { defineConfig } = require('@vscode/test-cli');
module.exports = defineConfig({ files: 'out/test/**/*.test.js' });