Skip to content

Commit

Permalink
Add try-finally tests
Browse files Browse the repository at this point in the history
  • Loading branch information
compulim committed Jun 13, 2024
1 parent e6ff8fb commit 8a99409
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/iter-fest/src/asyncGeneratorWithLastValue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number, 'end' | 'finally', void>(
(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);
});
26 changes: 26 additions & 0 deletions packages/iter-fest/src/generatorWithLastValue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,29 @@ test('passthrough throw', () => {
expect(throw_).toHaveBeenNthCalledWith(1, 'artificial');
expect(shouldNotCall).not.toHaveBeenCalled();
});

test('return in try-finally', () => {
const generator = generatorWithLastValue<number, 'end' | 'finally', void>(
(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);
});

0 comments on commit 8a99409

Please sign in to comment.