1import spawnAsync, { SpawnPromise, SpawnResult } from '@expo/spawn-async';
2
3/** The shared spawn child object, used to assert resolve values in tests */
4export const STUB_SPAWN_CHILD = { type: 'child' };
5/** The shared spawn result object, used to assert resolve values in tests */
6export const STUB_SPAWN_RESULT = { type: 'spawn' };
7
8/**
9 * The type-mocked spawn async to use.
10 * Do not use `mockedSpawnAsync.mock(Resolved|Rejected)value` directly.
11 * That method will not provide a `.child` property to the returned promise.
12 * Use `mockedSpawnAsync.mockImplementation(mockSpawnPromise(Promise))` instead.
13 */
14export const mockedSpawnAsync = spawnAsync as jest.MockedFunction<typeof spawnAsync>;
15
16/**
17 * Mock a spawn promise by adding a `child` property to the provided promise.
18 * If nothing is provided, it defaults to a promise resolving the spawn result stub.
19 * Note, this is used inside the root mock for `@expo/spawn-async`.
20 */
21export function mockSpawnPromise(
22  promise: Promise<any> = Promise.resolve(STUB_SPAWN_RESULT),
23  child: any = STUB_SPAWN_CHILD
24): SpawnPromise<SpawnResult> {
25  // @ts-expect-error We are modifying the promise, typescript doesnt know how to type it
26  promise.child = child;
27  return promise as SpawnPromise<SpawnResult>;
28}
29