From 9c09f844dd89f028685cca0e7b06d4ca87c81013 Mon Sep 17 00:00:00 2001 From: Anton Evzhakov Date: Mon, 11 Sep 2023 13:38:15 +0300 Subject: [PATCH] fix(babel): actions' runner should process parallel throws --- .../babel/src/transform/actions/BaseAction.ts | 4 +++ .../actions/__tests__/BaseAction.test.ts | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/packages/babel/src/transform/actions/BaseAction.ts b/packages/babel/src/transform/actions/BaseAction.ts index f62317ab7..5c8b1d1aa 100644 --- a/packages/babel/src/transform/actions/BaseAction.ts +++ b/packages/babel/src/transform/actions/BaseAction.ts @@ -148,6 +148,10 @@ export class BaseAction }; const processError = (e: unknown) => { + if (this.activeScenarioNextResults.length > nextIdx) { + return; + } + try { const nextResult = throwFn(e); processNextResult(nextResult as IterationResult, processError); diff --git a/packages/babel/src/transform/actions/__tests__/BaseAction.test.ts b/packages/babel/src/transform/actions/__tests__/BaseAction.test.ts index c411d153c..8a6005bb6 100644 --- a/packages/babel/src/transform/actions/__tests__/BaseAction.test.ts +++ b/packages/babel/src/transform/actions/__tests__/BaseAction.test.ts @@ -183,5 +183,36 @@ describe('BaseAction', () => { expect(() => generator1.next()).toThrow(error); expect(() => generator2.next()).toThrow(error); }); + + it('should process parallel throws', () => { + const handler: Handler<'sync', ITransformAction> = function* handler() { + try { + yield ['resolveImports', entrypoint, { imports: new Map() }, null]; + } catch (e) { + onError(e); + } + + return null; + }; + + const generator1 = action.run(handler); + const generator2 = action.run(handler); + + expect(generator1.next()).toEqual({ + done: false, + value: ['resolveImports', entrypoint, { imports: new Map() }, null], + }); + expect(generator2.next()).toEqual({ + done: false, + value: ['resolveImports', entrypoint, { imports: new Map() }, null], + }); + + const error1 = new Error('foo'); + const error2 = new Error('bar'); + expect(generator1.throw(error1)).toEqual({ done: true, value: null }); + expect(generator2.throw(error2)).toEqual({ done: true, value: null }); + expect(onError).toHaveBeenCalledTimes(1); + expect(onError).toHaveBeenCalledWith(error1); + }); }); });