Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide public API to subscribe to test results. #625

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Bug-fixes within the same version aren't needed

## Master

* add public api to expose interface for other vscode extensions to subscribe to test results updates.

-->

Expand Down
3 changes: 3 additions & 0 deletions __mocks__/supports-color.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
supportsColor: () => false,
};
1 change: 1 addition & 0 deletions __mocks__/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const window = {
showWorkspaceFolderPick: jest.fn(),
onDidChangeActiveTextEditor: jest.fn(),
showInformationMessage: jest.fn(),
visibleTextEditors: [],
}

const workspace = {
Expand Down
7 changes: 6 additions & 1 deletion src/JestExt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ export class JestExt {
debugConfigurationProvider: DebugConfigurationProvider,
failDiagnostics: vscode.DiagnosticCollection,
instanceSettings: InstanceSettings,
coverageCodeLensProvider: CoverageCodeLensProvider
coverageCodeLensProvider: CoverageCodeLensProvider,
private onTestResultsChanged: (results: JestTotalResults) => void
) {
this.workspaceFolder = workspaceFolder;
this.jestWorkspace = jestWorkspace;
Expand Down Expand Up @@ -515,6 +516,10 @@ export class JestExt {
private updateWithData(data: JestTotalResults): void {
const noAnsiData = resultsWithoutAnsiEscapeSequence(data);
const normalizedData = resultsWithLowerCaseWindowsDriveLetters(noAnsiData);

// notify that there are new test results.
this.onTestResultsChanged(normalizedData);

this._updateCoverageMap(normalizedData.coverageMap);

const statusList = this.testResultProvider.updateTestResults(normalizedData);
Expand Down
6 changes: 5 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { registerSnapshotCodeLens, registerSnapshotPreview } from './SnapshotCod

let extensionManager: ExtensionManager;

export function activate(context: vscode.ExtensionContext): void {
export function activate(
context: vscode.ExtensionContext
): ReturnType<typeof ExtensionManager.prototype.getPublicApi> {
extensionManager = new ExtensionManager(context);

const languages = [
Expand Down Expand Up @@ -82,6 +84,8 @@ export function activate(context: vscode.ExtensionContext): void {
extensionManager
)
);

return extensionManager.getPublicApi();
}

export function deactivate(): void {
Expand Down
72 changes: 57 additions & 15 deletions src/extensionManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from 'vscode';
import * as path from 'path';
import { ProjectWorkspace } from 'jest-editor-support';
import { JestTotalResults, ProjectWorkspace } from 'jest-editor-support';
import { pathToJest, pathToConfig } from './helpers';
import { JestExt } from './JestExt';
import { DebugCodeLensProvider, TestState } from './DebugCodeLens';
Expand Down Expand Up @@ -48,6 +48,8 @@ export class ExtensionManager {
private extByWorkspace: Map<string, JestExt> = new Map();
private context: vscode.ExtensionContext;
private commonPluginSettings: PluginWindowSettings;
// keep track of any subscribers that want to be notified when there are new test results.
private subscribers: Array<(results: JestTotalResults) => void> = [];

constructor(context: vscode.ExtensionContext) {
this.context = context;
Expand Down Expand Up @@ -97,21 +99,21 @@ export class ExtensionManager {
`Jest (${workspaceFolder.name})`
);

this.extByWorkspace.set(
workspaceFolder.name,
new JestExt(
this.context,
workspaceFolder,
jestWorkspace,
channel,
pluginSettings,
this.debugCodeLensProvider,
this.debugConfigurationProvider,
failDiagnostics,
instanceSettings,
this.coverageCodeLensProvider
)
const jestExt = new JestExt(
this.context,
workspaceFolder,
jestWorkspace,
channel,
pluginSettings,
this.debugCodeLensProvider,
this.debugConfigurationProvider,
failDiagnostics,
instanceSettings,
this.coverageCodeLensProvider,
this.onTestResultsChanged.bind(this)
);

this.extByWorkspace.set(workspaceFolder.name, jestExt);
}
registerAll(): void {
vscode.workspace.workspaceFolders.forEach(this.register, this);
Expand All @@ -131,6 +133,8 @@ export class ExtensionManager {
for (const key of keys) {
this.unregisterByName(key);
}

this.subscribers = [];
}
shouldStart(workspaceFolderName: string): boolean {
const {
Expand Down Expand Up @@ -166,6 +170,30 @@ export class ExtensionManager {
throw new Error(`No Jest instance in ${workspace.name} workspace`);
}
}

/**
* Provides the public API for this extension.
*/
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
public getPublicApi() {
return {
/**
* Provides a means to subscribe to test results events.
*
* @param callback method to be called when there are new test results.
*/
subscribeToTestResults: (callback: (results: JestTotalResults) => void): (() => void) => {
this.subscribers.push(callback);

// return an unsubscribe function.
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
return () => {
this.subscribers = this.subscribers.filter((s) => s !== callback);
};
},
};
}

registerCommand(
command: string,
callback: (extension: JestExt, ...args: unknown[]) => unknown,
Expand Down Expand Up @@ -216,4 +244,18 @@ export class ExtensionManager {
ext.onDidChangeTextDocument(event);
}
}

/**
* A handler for when there are new test results from one of the JestExt instances.
* @param results the new test results
*/
private onTestResultsChanged(results: JestTotalResults): void {
this.subscribers.forEach((subscriber) => {
try {
subscriber(results);
} catch (error) {
// swallow the error as we are not responsible for it.
}
});
}
}
Loading