提问人:user686483 提问时间:11/10/2023 更新时间:11/15/2023 访问量:88
在单元测试中使用 asyncIterator 永远不会使用值进行解析
Using an asyncIterator inside a unit test never resolves with values
问:
我有一个来自 chatGPT 的可读流,我试图使用 asyncIterator 断言,然后读取这些值并断言它们。开玩笑测试运行良好,但我的逻辑中存在一个错误,导致它在每次迭代后都无法解决。
我错过了什么?
谢谢吉米
var util = require("util");
describe("stream", () => {
it("should work", async () => {
let i = -1;
const completionStream: ChatCompletionStreamParams = {
toReadableStream: () => {
const mockReadable = {
async *[Symbol.asyncIterator]() {
const encoder = new util.TextEncoder("utf-8");
yield encoder.encode(
JSON.stringify({
id: "1",
object: "text",
created: 1629159835,
model: "gpt4",
choices: [
{
index: 0,
delta: { content: "Hello" },
finish_reason: null,
},
],
})
).buffer;
yield encoder.encode(
JSON.stringify({
id: "1",
object: "text",
created: 1629159835,
model: "gpt4",
choices: [
{
index: 0,
delta: { content: "there" },
finish_reason: null,
},
],
})
).buffer;
yield null;
},
getReader: () => {
return {
read: async () => {
const result = await mockReadable[
Symbol.asyncIterator
]().next();
i++;
if (i >= 2) {
return Promise.resolve({ done: true, value: null });
}
return Promise.resolve({ done: false, value: result?.value });
},
};
},
};
return mockReadable;
},
};
const readable = completionStream.toReadableStream().getReader();
while (true) {
const { done, value } = await readable.read();
expect(done).toBe(false); // assert here
}
});
});
答:
2赞
hackape
11/15/2023
#1
你在零件上犯了一个错误。每次调用时都会调用它,从头开始创建一个新的异步迭代器,因此它会不断产生“Hello”,并且永远不会到达“那里”。getReader
mockReadable[Symbol.asyncIterator]()
readable.read()
您需要保存迭代器并重用它。
getReader: () => {
const iterator = mockReadable[Symbol.asyncIterator]();
return {
read: async () => {
const result = await iterator.next();
return result;
}
// in fact you can just re-expose `iterator.next` as `read` like
// read: iterator.next.bind(iterator),
};
}
评论
0赞
user686483
11/21/2023
非常感谢您解释我做错了什么并提供解决方案。
-1赞
ali azam
11/15/2023
#2
它将工作文件:
it("should work", async () => {
let i = -1;
const completionStream: ChatCompletionStreamParams = {
toReadableStream: () => {
const mockReadable = {
async *[Symbol.asyncIterator]() {
const encoder = new util.TextEncoder("utf-8");
yield encoder.encode(
JSON.stringify({
id: "1",
object: "text",
created: 1629159835,
model: "gpt4",
choices: [
{
index: 0,
delta: { content: "Hello" },
finish_reason: null,
},
],
})
).buffer;
yield encoder.encode(
JSON.stringify({
id: "1",
object: "text",
created: 1629159835,
model: "gpt4",
choices: [
{
index: 0,
delta: { content: "there" },
finish_reason: null,
},
],
})
).buffer;
yield null;
},
getReader: () => {
return {
read: async () => {
const result = await mockReadable[
Symbol.asyncIterator
]().next();
i++;
if (i >= 2) {
return Promise.resolve({ done: true, value: null });
}
return Promise.resolve({ done: false, value: result?.value });
},
};
},
};
return mockReadable;
},
};
const readable = completionStream.toReadableStream().getReader();
while (true) {
const { done, value } = await readable.read();
expect(done).toBe(false); // assert here
if (done) {
break; // exit the loop when done is true
}
}
});
评论
while(true)