import { CliConfig } from "@sanity/cli-core/types"; import { Command } from "@oclif/core"; import { Config } from "@oclif/core"; import { Errors } from "@oclif/core"; import { Hook } from "@oclif/core/hooks"; import { Hooks } from "@oclif/core/hooks"; import { ProjectRootResult } from "@sanity/cli-core/types"; import { SanityCommand } from "@sanity/cli-core/SanityCommand"; /** @public */ export declare interface CaptureOptions { /** * Whether to print the output to the console */ print?: boolean; /** * Whether to strip ANSI escape codes from the output */ stripAnsi?: boolean; testNodeEnv?: string; } /** * Capture the output of a command and return the result * * @param fn - The function to capture the output of * @param opts - The options for the capture * @returns The result of the command * @public * * Credits to oclif for the original implementation: * https://github.com/oclif/test/blob/2a5407e6fc80d388043d10f6b7b8eaa586483015/src/index.ts * * We are not using the library directly since it does not support mocking code inside of the command * possibly because the commands run in a different thread */ export declare function captureOutput( fn: () => Promise, opts?: CaptureOptions, ): Promise>; /** @public */ export declare interface CaptureResult { stderr: string; stdout: string; error?: Error & Partial; result?: T; } declare type CommandClass = (new (argv: string[], config: Config) => Command) & typeof Command; /** * Creates a testable subclass of a command with mocked SanityCommand dependencies. * * @public * * @example * ```ts * // Basic config mocking * const TestAdd = mockSanityCommand(Add, { * cliConfig: { api: { projectId: 'test-project' } } * }) * * // With mock API client * const mockClient = { * getDocument: vi.fn().mockResolvedValue({ _id: 'doc1', title: 'Test' }), * fetch: vi.fn().mockResolvedValue([]), * } * const TestGet = mockSanityCommand(GetDocumentCommand, { * cliConfig: { api: { projectId: 'test-project', dataset: 'production' } }, * projectApiClient: mockClient, * }) * * const {stdout} = await testCommand(TestGet, ['doc1']) * expect(mockClient.getDocument).toHaveBeenCalledWith('doc1') * ``` */ export declare function mockSanityCommand< T extends typeof SanityCommand, >(CommandClass: T, options?: MockSanityCommandOptions): T; /** * @public */ export declare interface MockSanityCommandOptions { /** * Mock CLI config (required if command uses getCliConfig or getProjectId) */ cliConfig?: CliConfig; /** * When provided, getCliConfig() will throw this error instead of returning a config. * Useful for simulating running outside a project directory. */ cliConfigError?: Error; /** * Mock whether the terminal is interactive (used by isUnattended) */ isInteractive?: boolean; /** * Mock project root result (required if command uses getProjectRoot) */ projectRoot?: ProjectRootResult; /** * Mock authentication token (passed to API clients, bypasses getCliToken) */ token?: string; } /** * @public */ declare interface MockTelemetryOptions { trace?: () => void; updateUserProperties?: () => void; } declare interface Options { config: Config; Command?: Command.Class; context?: Hook.Context; } /** * @public */ export declare function testCommand( command: CommandClass, args?: string[], options?: TestCommandOptions, ): Promise>; /** * @public */ export declare interface TestCommandOptions { /** * Options for capturing output */ capture?: CaptureOptions; /** * Partial oclif config overrides */ config?: Partial; /** * Mock options for SanityCommand dependencies (config, project root, API clients). * When provided, the command is automatically wrapped with mockSanityCommand. */ mocks?: MockSanityCommandOptions & MockTelemetryOptions; } /** * Test an oclif hook * * @public * * @example * ```ts * const result = await testHook(hook, { * Command: Command.loadable, * context: { * config: Config.load({ * // CLI root is the directory of the package that contains the hook. * root: path.resolve(fileURLToPath(import.meta.url), '../../../root'), * }), * }, * }) * ``` * * @param hook - The hook to test * @param options - The options for the hook * @returns The result of the hook */ export declare function testHook( hook: Hook, options: Options, ): Promise>; export {};