This is a Hello World example extension that shows you how to set up and run simple UI tests for VS Code extensions using the ExTester framework.
Our example extension gives us the ability to call the Hello World command, one that shows a notification saying Hello World!. We would like to write automated regression tests for this feature.
In order to run the ExTester, the extension needs these packages as dev dependencies:
vscode-extension-tester— the extension testing frameworkmocha— required by ExTester for writing and running teststsx— optional, lets ExTester load the TypeScript tests directly, without compiling them first (ExTester 8.29.0+)
This example also uses chai as the assertion library, but any assertion package works.
Note: The
test-resourcesfolder (where VS Code binaries, ChromeDriver, logs and screenshots are stored) is excluded from the TypeScript compiler and ESLint. Add it to your.gitignoreas well.
npm install # install dependencies
npm run ui-test # download VS Code + package the extension + run tests
npm run ui-test:hidden # the same, with a VS Code window that never shows up (experimental)The ui-test script calls extest setup-and-run with no extra arguments — all options are read from extester.config.json at the project root.
- VS Code (
maxsupported version) and ChromeDriver are downloaded into the test storage folder ($TMPDIR/test-resources, or the folder in theTEST_RESOURCESenvironment variable) - The extension is packaged and installed into the test VS Code instance; packaging runs
vscode:prepublish, which compiles the extension toout/ - Mocha runs the TypeScript test files matched by
./src/ui-test/*.test.ts, which ExTester loads throughtsx— there is no separate compile step for the tests
tsx only transpiles, it does not type-check. Run npm run compile (tsc + ESLint) to check the tests, as the CI workflow does before it runs them.
Running with a hidden window (experimental)
With VS Code 1.147 or newer, npm run ui-test:hidden (extest setup-and-run --hidden_window) launches the test instance with a window that is never shown or focused, so you can keep working while the tests run. The same can be turned on with "hiddenWindow": true in the run section of extester.config.json, or with EXTESTER_HIDDEN_WINDOW=true.
VS Code then runs in extension test mode, which changes a few things a test can notice:
-
Dialogs are not shown. VS Code answers them on its own, so
modalDialog.test.tsskips itself:const dialogsShown = !VSBrowser.instance.isHiddenWindow; (dialogsShown ? describe : describe.skip)('Sample Modal Dialog Tests', () => { /* ... */ });
-
Opening a folder restarts VS Code.
VSBrowser.instance.openResources()handles that for you (as intreeView.test.ts), but elements found before go stale. -
Reloading the window ends the session. Use
VSBrowser.instance.reloadWindow()instead of runningDeveloper: Reload Window; it works with both a visible and a hidden window.
The hidden window is meant for local runs; the CI workflow keeps the regular window. See Running With a Hidden Window for every difference.
All ExTester options are centralised in extester.config.json, shared by the extest CLI and the ExTester Runner extension:
{
"$schema": "./node_modules/vscode-extension-tester/resources/extester.schema.json",
"setup": {
"vscodeVersion": "max",
"extensionsDir": ".test-extensions"
},
"run": {
"testFiles": ["./src/ui-test/*.test.ts"],
"settings": "./settings.json",
"extensionsDir": ".test-extensions",
"mochaConfig": "./.mocharc.js"
},
"runner": {
"testFileGlob": "src/ui-test/**/*.test.ts",
"ignorePathPart": "src/",
"runSources": true
}
}setup— download VS Code and ChromeDriver, package and install the extensionrun— run the tests;testFilespoints at the TypeScript sourcesrunner— read only by ExTester Runner; the CLI ignores it
The $schema field enables inline validation and autocomplete in VS Code. All path values are resolved relative to the config file's location.
CLI flags still work and always override the config file:
# run against a specific VS Code version instead of 'max'
npx extest setup-and-run --code_version 1.147.0ExTester takes a screenshot of every failed test, and (since 8.30.0) of every failed hook — before, beforeEach, afterEach and after — with Mocha's own hook functions, nothing to import. The screenshots are saved to <test storage>/screenshots/<run-timestamp>/.
The CI workflow sets TEST_RESOURCES to the runner's temp folder and uploads the screenshots, the VS Code logs and chromedriver.log as artifacts when a run fails.
ExTester starts VS Code in Mocha's global setup and shuts it down in the global teardown (8.30.0+). Your own fixtures go in the globalSetup and globalTeardown keys of a JavaScript .mocharc file; they run once per run, while the workbench is available:
// .mocharc.js
module.exports = {
timeout: 5000,
// runs after VS Code has started, before the first test file
globalSetup: async function () {
this.server = await startMockServer();
},
// runs after the last test, before ExTester shuts VS Code down
globalTeardown: async function () {
await this.server.stop();
},
};This example does not need any, as each test file prepares its own state. Fixtures exported as mochaGlobalSetup / mochaGlobalTeardown from a module loaded with Mocha's --require are not supported. See Global Setup and Teardown.
The ExTester Runner extension (1.0.0+) adds the UI tests to VS Code's built-in Testing view.
- Install the ExTester Runner extension — the workspace recommends it in
.vscode/extensions.json - Open this project in VS Code
- Open the Testing view and expand ExTester
The Runner reads its project settings from the runner section of extester.config.json, and the VS Code version from setup.vscodeVersion, so the project needs no .vscode/settings.json:
testFileGlob— the test files listed in the Testing viewignorePathPart— hidessrc/from the folder labels, so the folder shows asui-testrunSources— runs the TypeScript sources throughtsxinstead of the compiled JavaScript inout/, so there is nothing to compile before a run
.vscode/settings.json is git-ignored: use it, or your user settings, for personal overrides. Workspace settings win over extester.config.json.
| Action | How |
|---|---|
| Run all tests, a folder or a file | Click |
Run a single describe or it |
Click |
| Debug all tests, a folder, a file or a single test | Debug Test in the Testing view or the gutter menu; breakpoints in the .ts tests stop, and Mocha's timeouts are turned off |
| Re-run the last run | Test: Rerun Last Run from the Command Palette |
Each test shows whether it passed, failed or was skipped. A failure shows the error, a diff of expected and actual values, a stack trace into the test code and a thumbnail of the failure screenshot. Tests marked with .only or .skip are labelled in the tree.
The Screenshots and Logs views below the tests keep the files of each run, newest first.
These are personal VS Code settings, not project settings:
extesterRunner.hiddenWindow— experimental, runs the tests with a hidden window (VS Code 1.147+)extesterRunner.virtualDisplay— Linux only, runs the tests on an Xvfb virtual display that the Runner starts, without the hidden window's differences
See the ExTester Runner guide for all settings.
src/
└── ui-test/
├── activityBar.test.ts
├── bottomBar.test.ts
├── commands.test.ts
└── ...
Tests are written in TypeScript and import from vscode-extension-tester:
import { expect } from 'chai';
import { Workbench } from 'vscode-extension-tester';
describe('Hello World command', () => {
it('shows a notification', async () => {
const workbench = new Workbench();
await workbench.executeCommand('Hello World');
const notifications = await workbench.getNotifications();
expect(notifications).not.to.be.empty;
});
});See the ExTester documentation for the full page object API reference.
