Registry / testing / vscode-test-adapter-util

vscode-test-adapter-util

JSON →
library0.7.1jsnpmunverified

vscode-test-adapter-util is a foundational utility library designed to streamline the development of test adapters for the VS Code Test Explorer extension. It provides common functionalities such as logging, registering test adapters with the Test Explorer, and sending test-related events. While `vscode-test-explorer` (the extension this utility supports) itself is now deprecated in favor of VS Code's native testing API since version 1.59, this library remains relevant for maintaining existing adapters or for developers who still prefer the `vscode-test-explorer` API by setting `testExplorer.useNativeTesting: false`. The current stable version is 0.7.1. Its release cadence is typically tied to updates in the `vscode-test-adapter-api` or `vscode-test-explorer` and is not on a fixed schedule. Key differentiators include simplifying boilerplate for test adapter authors and providing robust utilities for interacting with the Test Explorer UI, making it easier to integrate various testing frameworks into VS Code.

npm install vscode-test-adapter-util
INSTALL
IMPORT
SIG · VSCODE-TEST-ADAPTE
V
vscode-test-adapter-util
testingjavascriptv0.7.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TestAdapterRegistrar
import { TestAdapterRegistrar } from 'vscode-test-adapter-util';
const { TestAdapterRegistrar } = require('vscode-test-adapter-util');
Primarily designed for ESM usage within VS Code extensions. `TestAdapterRegistrar` is crucial for linking your custom TestAdapter to the Test Explorer.
Log
import { Log } from 'vscode-test-adapter-util';
const Log = require('vscode-test-adapter-util').Log;
Used for logging diagnostic information from your test adapter to the VS Code output channel. Useful for debugging and user feedback.
sendTestEvent
import { sendTestEvent } from 'vscode-test-adapter-util';
import sendTestEvent from 'vscode-test-adapter-util/dist/sendTestEvent';
A utility function to send `TestEvent` objects to the Test Explorer, indicating test progress and results. Avoid direct imports from internal paths like `dist`.

This quickstart demonstrates how to create a basic VS Code Test Adapter using `vscode-test-adapter-util`. It shows registering a custom `MyTestAdapter` with the Test Explorer using `TestAdapterRegistrar`, initializing a `Log` instance for output, and simulating test discovery and execution using `sendTestEvent` to update test states in the UI.

import * as vscode from 'vscode'; import { TestAdapter, TestLoadFinishedEvent, TestLoadStartedEvent, TestRunFinishedEvent, TestRunStartedEvent, TestSuiteEvent, TestEvent, TestController } from 'vscode-test-adapter-api'; import { TestAdapterRegistrar, Log, sendTestEvent } from 'vscode-test-adapter-util'; class MyTestAdapter implements TestAdapter { private disposables: vscode.Disposable[] = []; private readonly testsEmitter = new vscode.EventEmitter<TestLoadStartedEvent | TestLoadFinishedEvent>(); private readonly testStatesEmitter = new vscode.EventEmitter<TestRunStartedEvent | TestRunFinishedEvent | TestSuiteEvent | TestEvent>(); private readonly retireEmitter = new vscode.EventEmitter<void>(); get tests(): vscode.Event<TestLoadStartedEvent | TestLoadFinishedEvent> { return this.testsEmitter.event; } get testStates(): vscode.Event<TestRunStartedEvent | TestRunFinishedEvent | TestSuiteEvent | TestEvent> { return this.testStatesEmitter.event; } get retire(): vscode.Event<void> { return this.retireEmitter.event; } constructor(public readonly workspaceFolder: vscode.WorkspaceFolder, private readonly log: Log) { this.log.info('Initializing MyTestAdapter for workspace ' + workspaceFolder.name); } async load(): Promise<void> { this.testsEmitter.fire({ type: 'started' }); this.log.debug('Starting test load...'); const suite = { type: 'suite' as const, id: 'root', label: 'My Tests', children: [ { type: 'test' as const, id: 'test1', label: 'Example Test 1' }, { type: 'test' as const, id: 'test2', label: 'Example Test 2' } ] }; this.testsEmitter.fire({ type: 'finished', suite }); this.log.debug('Test load finished.'); } async run(testIds: string[]): Promise<void> { this.testStatesEmitter.fire({ type: 'started', tests: testIds }); this.log.info('Running tests: ' + JSON.stringify(testIds)); for (const id of testIds) { sendTestEvent(this.testStatesEmitter, { type: 'test', test: id, state: 'running' }); await new Promise(resolve => setTimeout(resolve, Math.random() * 500 + 100)); // Simulate async work const passed = Math.random() > 0.3; sendTestEvent(this.testStatesEmitter, { type: 'test', test: id, state: passed ? 'passed' : 'failed', message: passed ? undefined : 'Simulated failure' }); this.log.debug(`Test ${id} ${passed ? 'passed' : 'failed'}`); } this.testStatesEmitter.fire({ type: 'finished' }); this.log.info('Test run finished.'); } async debug(testIds: string[]): Promise<void> { this.log.warn('Debug not yet implemented for MyTestAdapter. Running normally instead.'); return this.run(testIds); } cancel(): void { this.log.info('Test run cancelled.'); } dispose(): void { this.cancel(); for (const disposable of this.disposables) { disposable.dispose(); } this.disposables = []; } } export function activate(context: vscode.ExtensionContext) { const log = new Log('myTestAdapter', context.outputChannel, 'MyTestAdapter Log'); context.subscriptions.push(log); const testExplorerExtension = vscode.extensions.getExtension<TestController>('hbenl.vscode-test-explorer'); if (testExplorerExtension) { log.info('Test Explorer extension found. Registering adapter.'); const testHub = testExplorerExtension.exports; context.subscriptions.push(new TestAdapterRegistrar( testHub, workspaceFolder => new MyTestAdapter(workspaceFolder, log) )); } else { log.warn('Test Explorer extension not found. MyTestAdapter will not be registered.'); } }
Debug
Known issues
deprecatedThe `vscode-test-explorer` extension, for which this utility is primarily built, has been deprecated in favor of VS Code's native Testing API since VS Code 1.59. While `vscode-test-explorer` remains usable, new test extensions are encouraged to use the native API.
fix
Consider migrating your test adapter to use the native VS Code Testing API (`vscode.tests.createTestController`) for new development or to leverage the latest VS Code features. A migration guide is available in the `vscode-test-explorer` repository.
affects: >=0.1.0
breakingMajor version updates to `vscode-test-adapter-api` (the core API for test adapters) can introduce breaking changes that require updates to `vscode-test-adapter-util` and your custom adapters. These typically involve changes to interfaces or event structures.
fix
Always check the release notes for `vscode-test-adapter-api` and `vscode-test-adapter-util` when upgrading. Update your adapter's implementation to conform to the new API contracts.
affects: >=0.1.0
gotchaAsynchronous operations within `load()` or `run()` methods of your `TestAdapter` can lead to unexpected behavior or an unresponsive UI if not handled correctly. Ensure all promises are awaited, and events are fired at appropriate times.
fix
Use `async`/`await` consistently. Ensure `TestLoadStartedEvent`/`TestRunStartedEvent` are fired at the beginning and `TestLoadFinishedEvent`/`TestRunFinishedEvent` are fired at the end of their respective operations, even in case of errors. Implement proper error handling.
affects: >=0.1.0
gotchaThe `engines.vscode` field specifies the minimum required VS Code version. Using the utility in an older VS Code version might lead to runtime errors or unexpected behavior due to API inconsistencies.
fix
Ensure your `package.json`'s `engines.vscode` matches or exceeds the requirement of `vscode-test-adapter-util`. Update your VS Code installation if necessary.
affects: <1.24.0
Errors
Common errors & fixes
Error: Cannot find module 'vscode-test-adapter-util'
The package is not correctly installed or linked in the extension's `node_modules`.
fix
Run `npm install vscode-test-adapter-util` or `yarn add vscode-test-adapter-util` in your extension project. For a multi-root workspace, ensure it's installed in the correct workspace folder.
TypeError: Cannot read properties of undefined (reading 'exports') when getting Test Explorer extension
The `hbenl.vscode-test-explorer` extension is not installed or not activated in the VS Code instance running your extension, or its identifier is incorrect.
fix
Verify that `hbenl.vscode-test-explorer` is installed and enabled in your VS Code environment. Double-check the extension ID string in `vscode.extensions.getExtension('hbenl.vscode-test-explorer')`. Handle the case where the extension might not be found gracefully.
TypeScript error: Argument of type 'TestAdapterRegistrar' is not assignable to parameter of type 'Disposable'
Older versions of `TestAdapterRegistrar` might not correctly implement `vscode.Disposable` or there's a type mismatch in your `tsconfig.json` or `vscode` types.
fix
Ensure `vscode-test-adapter-util` and `@types/vscode` are up-to-date. Ensure your `TestAdapterRegistrar` instance is properly initialized and its `dispose()` method is correctly defined and called during your extension's deactivation.
Upgrade
Version history
0.7.1latest on npm
Audit
Dependencies
vscoderequiredPeer dependency as it's a VS Code extension utility. Specified in engines field.
vscode-test-adapter-apirequiredProvides the core interfaces (like TestAdapter, TestHub) that this utility library assists in implementing and interacting with.
vscode-test-exploreroptionalThe VS Code extension for which this utility provides helpers. Conceptual dependency as this library's primary purpose is to integrate with it.
Agent activity
10 hits · last 30 days
node
10
Resources
vscode-test-adapter-util — npm install vscode-test-adapter-util · libregistry