From b76eb17458dc9d186f4e7458fb93eb6aa52b971b Mon Sep 17 00:00:00 2001 From: Denis Golovin Date: Sun, 2 Jun 2024 21:53:19 -0700 Subject: [PATCH] fix: define @podman-desktop/api mock implementation in __mocks__ (#149) Related to #56. Signed-off-by: Denis Golovin --- __mocks__/@podman-desktop/api.js | 54 ++++++++++++++++++++++- src/authentication-service.spec.ts | 21 +-------- src/extension.spec.ts | 69 +----------------------------- src/podman-cli.spec.ts | 28 +----------- vitest.config.js | 2 +- 5 files changed, 58 insertions(+), 116 deletions(-) diff --git a/__mocks__/@podman-desktop/api.js b/__mocks__/@podman-desktop/api.js index 5f6f87d..538735a 100644 --- a/__mocks__/@podman-desktop/api.js +++ b/__mocks__/@podman-desktop/api.js @@ -20,5 +20,57 @@ * Mock the extension API for vitest. * This file is referenced from vitest.config.js file. */ -const plugin = {}; +const plugin = { + EventEmitter: vi.fn().mockImplementation(() => { + return { + fire: vi.fn(), + }; + }), + registry: { + suggestRegistry: vi.fn(), + unregisterRegistry: vi.fn(), + }, + authentication: { + registerAuthenticationProvider: vi.fn(), + getSession: vi.fn(), + onDidChangeSessions: vi.fn(), + }, + commands: { + registerCommand: vi.fn(), + executeCommand: vi.fn(), + }, + window: { + createStatusBarItem: () => ({ + show: vi.fn(), + iconClass: '', + }), + withProgress: (options, callback) => { + return callback({ + report: () => {}, + }); + }, + showInformationMessage: vi.fn(), + }, + env: { + createTelemetryLogger: vi.fn().mockImplementation(() => ({ + logUsage: vi.fn(), + logError: vi.fn(), + })), + }, + StatusBarAlignLeft: 'LEFT', + ProgressLocation: { + TASK_WIDGET: 2, + }, + process: { + exec: vi.fn(), + }, + configuration: { + getConfiguration: () => { + return { + get: vi.fn(), + }; + }, + }, +}; + module.exports = plugin; diff --git a/src/authentication-service.spec.ts b/src/authentication-service.spec.ts index 9156579..0fa85d9 100644 --- a/src/authentication-service.spec.ts +++ b/src/authentication-service.spec.ts @@ -25,26 +25,7 @@ import { beforeEach, expect, test, vi } from 'vitest'; import { convertToSession, RedHatAuthenticationService } from './authentication-service'; import { getAuthConfig } from './configuration'; -vi.mock('@podman-desktop/api', async () => { - return { - EventEmitter: vi.fn().mockImplementation(() => { - return { - fire: vi.fn(), - }; - }), - registry: { - suggestRegistry: vi.fn(), - }, - authentication: { - registerAuthenticationProvider: vi.fn(), - onDidChangeSessions: vi.fn(), - getSession: vi.fn(), - }, - commands: { - registerCommand: vi.fn(), - }, - }; -}); +vi.mock('@podman-desktop/api'); beforeEach(() => { vi.restoreAllMocks(); diff --git a/src/extension.spec.ts b/src/extension.spec.ts index ad02faf..067cd18 100644 --- a/src/extension.spec.ts +++ b/src/extension.spec.ts @@ -18,13 +18,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/explicit-function-return-type */ -import type { - AuthenticationGetSessionOptions, - AuthenticationSession, - ExtensionContext, - ProgressLocation, - TelemetryLogger, -} from '@podman-desktop/api'; +import type { AuthenticationGetSessionOptions, AuthenticationSession, ExtensionContext } from '@podman-desktop/api'; import { authentication, commands } from '@podman-desktop/api'; import { OrganizationService } from '@redhat-developer/rhsm-client'; import { afterEach, beforeEach, expect, suite, test, vi } from 'vitest'; @@ -35,66 +29,7 @@ import * as subscription from './subscription'; import { ExtensionTelemetryLogger } from './telemetry'; import * as util from './util'; -vi.mock('@podman-desktop/api', async () => { - return { - EventEmitter: vi.fn().mockImplementation(() => { - return { - fire: vi.fn(), - }; - }), - registry: { - suggestRegistry: vi.fn(), - unregisterRegistry: vi.fn(), - }, - authentication: { - registerAuthenticationProvider: vi.fn(), - getSession: vi.fn(), - onDidChangeSessions: vi.fn(), - }, - commands: { - registerCommand: vi.fn(), - executeCommand: vi.fn(), - }, - window: { - createStatusBarItem: () => ({ - show: vi.fn(), - iconClass: '', - }), - withProgress: ( - options: { location: ProgressLocation; title: string }, - callback: (progress: { report: (m: string) => void }) => void, - ) => { - return callback({ - report: () => {}, - }); - }, - showInformationMessage: vi.fn(), - }, - env: { - createTelemetryLogger: vi.fn().mockImplementation( - () => - ({ - logUsage: vi.fn(), - logError: vi.fn(), - }) as unknown as TelemetryLogger, - ), - }, - StatusBarAlignLeft: 'LEFT', - ProgressLocation: { - TASK_WIDGET: 2, - }, - process: { - exec: vi.fn(), - }, - configuration: { - getConfiguration: () => { - return { - get: vi.fn(), - }; - }, - }, - }; -}); +vi.mock('@podman-desktop/api'); function createExtContext(): ExtensionContext { return { diff --git a/src/podman-cli.spec.ts b/src/podman-cli.spec.ts index fc71664..6216105 100644 --- a/src/podman-cli.spec.ts +++ b/src/podman-cli.spec.ts @@ -18,7 +18,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/explicit-function-return-type */ -import type { TelemetryLogger } from '@podman-desktop/api'; import { process as podmanProcess } from '@podman-desktop/api'; import { beforeEach, expect, test, vi } from 'vitest'; @@ -35,32 +34,7 @@ import { } from './podman-cli'; import { ExtensionTelemetryLogger } from './telemetry'; -vi.mock('@podman-desktop/api', async () => { - return { - env: { - createTelemetryLogger: vi.fn().mockImplementation( - () => - ({ - logUsage: vi.fn(), - logError: vi.fn(), - }) as unknown as TelemetryLogger, - ), - }, - ProgressLocation: { - TASK_WIDGET: 2, - }, - process: { - exec: vi.fn(), - }, - configuration: { - getConfiguration: () => { - return { - get: vi.fn(), - }; - }, - }, - }; -}); +vi.mock('@podman-desktop/api'); const runResult = { command: 'command line', stdout: 'stdout output', stderr: 'stderr output' }; const runError = { diff --git a/vitest.config.js b/vitest.config.js index ba8a521..ed2cdc6 100644 --- a/vitest.config.js +++ b/vitest.config.js @@ -111,7 +111,7 @@ const config = { }, resolve: { alias: { - '@podman-desktop/api': path.resolve(PACKAGE_ROOT, '__mocks__/@podman-desktop/api.js'), + '@podman-desktop/api': path.resolve(PACKAGE_ROOT, '__mocks__/@podman-desktop/api.ts'), }, }, };