diff --git a/packages/iter-fest/src/asyncGeneratorWithLastValue.spec.ts b/packages/iter-fest/src/asyncGeneratorWithLastValue.spec.ts index 88bfb84..cbb1c8c 100644 --- a/packages/iter-fest/src/asyncGeneratorWithLastValue.spec.ts +++ b/packages/iter-fest/src/asyncGeneratorWithLastValue.spec.ts @@ -160,3 +160,34 @@ test('passthrough throw', async () => { expect(throw_).toHaveBeenNthCalledWith(1, 'artificial'); expect(shouldNotCall).not.toHaveBeenCalled(); }); + +test('return in try-finally', async () => { + const generator = asyncGeneratorWithLastValue( + (async function* () { + try { + await undefined; + + yield 1; + await undefined; + + return 'end' as const; + } finally { + await undefined; + + // eslint-disable-next-line no-unsafe-finally + return 'finally' as const; + } + })() + ); + + await expect(generator.next()).resolves.toEqual({ done: false, value: 1 }); + + await expect(generator.next()).resolves.toEqual({ done: true, value: 'finally' }); + expect(generator.lastValue()).toEqual('finally'); + + await expect(generator.next()).resolves.toEqual({ done: true, value: undefined }); + expect(generator.lastValue()).toEqual(undefined); + + await expect(generator.next()).resolves.toEqual({ done: true, value: undefined }); + expect(generator.lastValue()).toEqual(undefined); +}); diff --git a/packages/iter-fest/src/generatorWithLastValue.spec.ts b/packages/iter-fest/src/generatorWithLastValue.spec.ts index ac0943f..058f55c 100644 --- a/packages/iter-fest/src/generatorWithLastValue.spec.ts +++ b/packages/iter-fest/src/generatorWithLastValue.spec.ts @@ -136,3 +136,29 @@ test('passthrough throw', () => { expect(throw_).toHaveBeenNthCalledWith(1, 'artificial'); expect(shouldNotCall).not.toHaveBeenCalled(); }); + +test('return in try-finally', () => { + const generator = generatorWithLastValue( + (function* () { + try { + yield 1; + + return 'end' as const; + } finally { + // eslint-disable-next-line no-unsafe-finally + return 'finally' as const; + } + })() + ); + + expect(generator.next()).toEqual({ done: false, value: 1 }); + + expect(generator.next()).toEqual({ done: true, value: 'finally' }); + expect(generator.lastValue()).toEqual('finally'); + + expect(generator.next()).toEqual({ done: true, value: undefined }); + expect(generator.lastValue()).toEqual(undefined); + + expect(generator.next()).toEqual({ done: true, value: undefined }); + expect(generator.lastValue()).toEqual(undefined); +});