|
| 1 | +// Mock for the 'execa' package |
| 2 | + |
| 3 | +// Mock ExecaError class for instanceof checks |
| 4 | +class ExecaError extends Error { |
| 5 | + constructor(message, options = {}) { |
| 6 | + super(message) |
| 7 | + this.name = "ExecaError" |
| 8 | + this.exitCode = options.exitCode ?? 1 |
| 9 | + this.signal = options.signal ?? undefined |
| 10 | + this.stdout = options.stdout ?? "" |
| 11 | + this.stderr = options.stderr ?? "" |
| 12 | + this.all = options.all ?? "" |
| 13 | + this.failed = options.failed ?? true |
| 14 | + this.timedOut = options.timedOut ?? false |
| 15 | + this.isCanceled = options.isCanceled ?? false |
| 16 | + this.isKilled = options.isKilled ?? false |
| 17 | + // Add any other properties accessed in tests if needed |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +// Mock the main execa function (handling tagged template literal usage) |
| 22 | +const mockExeca = (_options) => { |
| 23 | + // Prefix unused parameter with _ |
| 24 | + // The tagged template literal part is ignored in this simple mock |
| 25 | + // We just return an object simulating the subprocess |
| 26 | + const subprocess = (async function* () { |
| 27 | + // Yield some mock output lines |
| 28 | + yield "Mock execa output line 1" |
| 29 | + yield "Mock execa output line 2" |
| 30 | + // Simulate command completion (or potential error throwing if needed for tests) |
| 31 | + })() |
| 32 | + |
| 33 | + // Add properties/methods expected on the subprocess object if needed by tests |
| 34 | + // For now, just making it async iterable is the main requirement from ExecaTerminalProcess.ts |
| 35 | + subprocess.stdout = { pipe: () => {} } // Mock minimal stream properties if needed |
| 36 | + subprocess.stderr = { pipe: () => {} } |
| 37 | + subprocess.all = { pipe: () => {} } // If combined output stream is used |
| 38 | + |
| 39 | + // Mock the promise interface if needed (e.g., if .then() is called on the result) |
| 40 | + subprocess.then = (resolve, reject) => { |
| 41 | + // Simulate successful completion after iteration |
| 42 | + Promise.resolve().then(async () => { |
| 43 | + try { |
| 44 | + // eslint-disable-next-line no-empty,@typescript-eslint/no-unused-vars |
| 45 | + for await (const _ of subprocess) { |
| 46 | + } // Consume the generator |
| 47 | + resolve({ exitCode: 0, stdout: "Mock stdout", stderr: "Mock stderr" }) |
| 48 | + } catch (error) { |
| 49 | + reject(error) |
| 50 | + } |
| 51 | + }) |
| 52 | + } |
| 53 | + subprocess.catch = (reject) => { |
| 54 | + // Simulate successful completion by not calling reject |
| 55 | + // Modify this if tests require catching specific errors |
| 56 | + Promise.resolve().then(async () => { |
| 57 | + try { |
| 58 | + // eslint-disable-next-line no-empty,@typescript-eslint/no-unused-vars |
| 59 | + for await (const _ of subprocess) { |
| 60 | + } // Consume the generator |
| 61 | + } catch (error) { |
| 62 | + reject(error) // Pass through errors from the generator if any |
| 63 | + } |
| 64 | + }) |
| 65 | + } |
| 66 | + subprocess.finally = (callback) => { |
| 67 | + Promise.resolve(subprocess).finally(callback) |
| 68 | + } |
| 69 | + |
| 70 | + return subprocess |
| 71 | +} |
| 72 | + |
| 73 | +module.exports = { |
| 74 | + execa: mockExeca, |
| 75 | + ExecaError: ExecaError, |
| 76 | +} |
0 commit comments