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