Skip to content

Commit

Permalink
fix(babel): actions reruns return unexpected undefined instead of thr…
Browse files Browse the repository at this point in the history
…owing error when first run fails
  • Loading branch information
Anber committed Sep 11, 2023
1 parent 2438ed3 commit 2ee2b26
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/babel/src/transform/actions/BaseAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export class BaseAction<TAction extends ActionQueueItem>
| AsyncScenarioForAction<TAction>
| null = null;

private activeScenarioError?: unknown;

private activeScenarioNextResults: AnyIteratorResult<
'async' | 'sync',
TypeOfResult<TAction>
Expand Down Expand Up @@ -161,6 +163,7 @@ export class BaseAction<TAction extends ActionQueueItem>
return;
}

this.activeScenarioError = errorInGenerator;
throw errorInGenerator;
}
};
Expand All @@ -180,10 +183,20 @@ export class BaseAction<TAction extends ActionQueueItem>

return {
next: (arg: YieldResult): IterationResult => {
if (this.activeScenarioError) {
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw this.activeScenarioError;
}

processNext(arg);
return this.activeScenarioNextResults[nextIdx++] as IterationResult;
},
throw: (e: unknown): IterationResult => {
if (this.activeScenarioError) {
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw this.activeScenarioError;
}

processError(e);
return this.activeScenarioNextResults[nextIdx++] as IterationResult;
},
Expand Down
14 changes: 14 additions & 0 deletions packages/babel/src/transform/actions/__tests__/BaseAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,19 @@ describe('BaseAction', () => {
expect(generator2.next()).toEqual({ done: true, value: null });
expect(onError).toHaveBeenCalledTimes(1);
});

it("should rethrow error from every run if the first one didn't catch it", () => {
const error = new Error('foo');

const handler: Handler<'sync', ITransformAction> = function* handler() {
throw error;
};

const generator1 = action.run(handler);
const generator2 = action.run(handler);

expect(() => generator1.next()).toThrow(error);
expect(() => generator2.next()).toThrow(error);
});
});
});

0 comments on commit 2ee2b26

Please sign in to comment.