Skip to content

Commit

Permalink
Add onReturn tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Nov 4, 2024
1 parent 244932b commit f66b27d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions sdks/node-sdk/test/AsyncStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const testError = new Error("test");
describe("AsyncStream", () => {
it("should return values from callbacks", async () => {
const stream = new AsyncStream<number>();
let onReturnCalled = false;
stream.onReturn = () => {
onReturnCalled = true;
};
stream.callback(null, 1);
stream.callback(null, 2);
stream.callback(null, 3);
Expand All @@ -25,10 +29,15 @@ describe("AsyncStream", () => {
}
count++;
}
expect(onReturnCalled).toBe(true);
});

it("should catch an error thrown in the for..await loop", async () => {
const stream = new AsyncStream<number>();
let onReturnCalled = false;
stream.onReturn = () => {
onReturnCalled = true;
};
stream.callback(null, 1);

try {
Expand All @@ -40,11 +49,16 @@ describe("AsyncStream", () => {
expect(error).toBe(testError);
expect((error as Error).message).toBe("test");
}
expect(onReturnCalled).toBe(true);
});

it("should catch an error passed to callback", async () => {
const runTest = async () => {
const stream = new AsyncStream<number>();
let onReturnCalled = false;
stream.onReturn = () => {
onReturnCalled = true;
};
stream.callback(testError, 1);
try {
for await (const _value of stream) {
Expand All @@ -54,6 +68,7 @@ describe("AsyncStream", () => {
expect(error).toBe(testError);
expect((error as Error).message).toBe("test");
}
expect(onReturnCalled).toBe(true);
};

await expect(runTest()).rejects.toThrow(testError);
Expand Down

0 comments on commit f66b27d

Please sign in to comment.