UNPKG

@shopify/cli-kit

Version:

A set of utilities, interfaces, and models that are common across all the platform features

163 lines (162 loc) 5.51 kB
import type { DoctorContext, TestResult } from './types.js'; /** * Result from running a CLI command. */ interface CommandResult { /** The full command that was run */ command: string; /** Exit code (0 = success) */ exitCode: number; /** Standard output */ stdout: string; /** Standard error */ stderr: string; /** Combined output (stdout + stderr) */ output: string; /** Whether the command succeeded (exitCode === 0) */ success: boolean; } /** * Base class for doctor test suites. * * Write tests using the test() method:. * * ```typescript * export default class MyTests extends DoctorSuite { * static description = 'My test suite' * * tests() { * this.test('basic case', async () => { * const result = await this.run('shopify theme init') * this.assertSuccess(result) * }) * * this.test('error case', async () => { * const result = await this.run('shopify theme init --invalid') * this.assertError(result, /unknown flag/) * }) * } * } * ``` */ export declare abstract class DoctorSuite<TContext extends DoctorContext = DoctorContext> { static description: string; protected context: TContext; private assertions; private registeredTests; /** * Run the entire test suite. * * @param context - The doctor context for this suite run. */ runSuite(context: TContext): Promise<TestResult[]>; /** * Register a test with a name and function. * * @param name - The test name. * @param fn - The async test function. */ protected test(name: string, fn: () => Promise<void>): void; /** * Override this method to register tests using this.test(). */ protected tests(): void; /** * Run a CLI command and return the result. * * @param command - The CLI command to run. * @param options - Optional cwd and env overrides. * @example * const result = await this.run('shopify theme init my-theme') * const result = await this.run('shopify theme push --json') */ protected run(command: string, options?: { cwd?: string; env?: { [key: string]: string; }; }): Promise<CommandResult>; /** * Run a command without capturing output (for interactive commands). * Returns only success/failure. * * @param command - The CLI command to run. * @param options - Optional cwd and env overrides. */ protected runInteractive(command: string, options?: { cwd?: string; env?: { [key: string]: string; }; }): Promise<CommandResult>; /** * Assert that a command succeeded (exit code 0). * * @param result - The command result to check. * @param message - Optional custom assertion message. */ protected assertSuccess(result: CommandResult, message?: string): void; /** * Assert that a command failed with an error matching the pattern. * * @param result - The command result to check. * @param pattern - Optional regex or string pattern to match against output. * @param message - Optional custom assertion message. */ protected assertError(result: CommandResult, pattern?: RegExp | string, message?: string): void; /** * Assert that a file exists and optionally matches content. * * @param path - The file path to check. * @param contentPattern - Optional regex or string to match file content. * @param message - Optional custom assertion message. */ protected assertFile(path: string, contentPattern?: RegExp | string, message?: string): Promise<void>; /** * Assert that a file does not exist. * * @param path - The file path to check. * @param message - Optional custom assertion message. */ protected assertNoFile(path: string, message?: string): Promise<void>; /** * Assert that a directory exists. * * @param path - The directory path to check. * @param message - Optional custom assertion message. */ protected assertDirectory(path: string, message?: string): Promise<void>; /** * Assert that output contains a pattern. * * @param result - The command result to check. * @param pattern - Regex or string pattern to match against output. * @param message - Optional custom assertion message. */ protected assertOutput(result: CommandResult, pattern: RegExp | string, message?: string): void; /** * Assert that output contains valid JSON and optionally validate it. * * @param result - The command result to parse. * @param validator - Optional function to validate the parsed JSON. * @param message - Optional custom assertion message. */ protected assertJson<T = unknown>(result: CommandResult, validator?: (json: T) => boolean, message?: string): T | undefined; /** * Assert a boolean condition. * * @param condition - The boolean condition to assert. * @param message - The assertion description. */ protected assert(condition: boolean, message: string): void; /** * Assert two values are equal. * * @param actual - The actual value. * @param expected - The expected value. * @param message - The assertion description. */ protected assertEqual<T>(actual: T, expected: T, message: string): void; private hasFailures; } export {};