Skip to content

Commit

Permalink
chore(node): make tests more explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
dpchamps committed Sep 23, 2021
1 parent 4bc76d0 commit 3e6afa8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
9 changes: 8 additions & 1 deletion packages/node/src/executors/execute/execute.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ describe('NodeExecuteBuilder', () => {
beforeEach(() => {
(devkit.runExecutor as any).mockImplementation(async function* () {
yield { success: true, outfile: 'outfile.js' };
await new Promise((resolve) => setTimeout(resolve, 1));
yield { success: true, outfile: 'outfile.js' };
});
});
Expand Down Expand Up @@ -546,6 +547,12 @@ describe('NodeExecuteBuilder', () => {
});

it('Yield build and process events cooperatively', async () => {
const expectedEvents = [
{ outfile: 'outfile.js', success: true },
{ exitCode: 1 },
{ outfile: 'outfile.js', success: true },
{ exitCode: 1 },
];
const events = [];
for await (const event of executeExecutor(
{
Expand All @@ -562,7 +569,7 @@ describe('NodeExecuteBuilder', () => {
events.push(event);
}

expect(events).toHaveLength(4);
expect(events).toEqual(expectedEvents);
});
});
});
Expand Down
65 changes: 37 additions & 28 deletions packages/node/src/executors/execute/execute.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const enum InspectType {

export { NodeExecuteBuilderOptions } from '../../utils/types';

type ScheduledEvent = NodeBuildEvent|SubProcessEvent
type ScheduledEvent = NodeBuildEvent | SubProcessEvent;

type TypedIterator =
| {
Expand All @@ -37,10 +37,14 @@ type TypedIterator =
iterator: AsyncIterator<SubProcessEvent, undefined>;
};

type MappedIteratorValue<T extends TypedIterator> =
T extends Extract<T, { type: 'build' }> ? T & IteratorResult<NodeBuildEvent, undefined> :
T extends Extract<T, {type: 'process'}> ? T & IteratorResult<SubProcessEvent, undefined> :
never;
type MappedIteratorValue<T extends TypedIterator> = T extends Extract<
T,
{ type: 'build' }
>
? T & IteratorResult<NodeBuildEvent, undefined>
: T extends Extract<T, { type: 'process' }>
? T & IteratorResult<SubProcessEvent, undefined>
: never;

export async function* executeExecutor(
options: NodeExecuteBuilderOptions,
Expand Down Expand Up @@ -69,19 +73,21 @@ export async function* executeExecutor(
}
}

yield * scheduleBuildAndProcessEvents(
startBuild(options, context),
processRunner,
options
)
yield* scheduleBuildAndProcessEvents(
startBuild(options, context),
processRunner,
options
);
}

const mapNextIterValue = <T extends TypedIterator>(typedIter: T): Promise<MappedIteratorValue<T>> =>
typedIter.iterator.next().then((result) => ({
iterator: typedIter.iterator,
type: typedIter.type,
...result,
}))
const mapNextIterValue = <T extends TypedIterator>(
typedIter: T
): Promise<MappedIteratorValue<T>> =>
typedIter.iterator.next().then((result) => ({
iterator: typedIter.iterator,
type: typedIter.type,
...result,
}));

async function* scheduleBuildAndProcessEvents(
buildIterator: AsyncIterator<NodeBuildEvent>,
Expand All @@ -92,7 +98,10 @@ async function* scheduleBuildAndProcessEvents(
AsyncIterator<ScheduledEvent>,
ReturnType<typeof mapNextIterValue>
>([
[buildIterator, mapNextIterValue({ type: 'build', iterator: buildIterator })],
[
buildIterator,
mapNextIterValue({ type: 'build', iterator: buildIterator }),
],
]);

while (iteratorMap.size > 0) {
Expand All @@ -104,17 +113,17 @@ async function* scheduleBuildAndProcessEvents(
}

if (result.type === 'build') {
if(!result.value.success){
logger.error('There was an error with the build. See above.');
logger.info(`${result.value.outfile} was not restarted.`);
} else {
await subProcessRunner.handleBuildEvent(result.value);

iteratorMap.set(
subProcessRunner,
mapNextIterValue({ type: 'process', iterator: subProcessRunner })
);
}
if (!result.value.success) {
logger.error('There was an error with the build. See above.');
logger.info(`${result.value.outfile} was not restarted.`);
} else {
await subProcessRunner.handleBuildEvent(result.value);

iteratorMap.set(
subProcessRunner,
mapNextIterValue({ type: 'process', iterator: subProcessRunner })
);
}
}

iteratorMap.set(result.iterator, mapNextIterValue(result));
Expand Down

0 comments on commit 3e6afa8

Please sign in to comment.