为什么这个测试是片状的,处理一个被拒绝的承诺?



运行此测试时:

const file = "exp.txt";
Deno.test("handle rejected promise", async (t) => {
try {
await Deno.stat(file);
} catch (e) {
if (e instanceof Deno.errors.NotFound) {
await Deno.writeTextFile(file, "some text");
} else {
throw e;
}
} finally {
await Deno.remove(file);
}
});

当像这样运行时:

deno test --allow-read --allow-write exp.test.ts

测试成功。

但是,当像这样运行它时(添加trace-ops选项):

deno test --allow-read --allow-write --trace-ops exp.test.ts

./exp.test.ts (uncaught error)
error: (in promise) NotFound: The system cannot find the file specified. (os error 2), stat 'exp.txt'
This error was not caught from a test and caused the test runner to fail on the referenced module.
It most likely originated from a dangling promise, event/timeout handler or top-level code.

我有更多涉及的代码做基本上上述失败,即使没有--trace-ops。这似乎更像是一个竞争条件或某种超时。

这是deno 1.28.3,我已经尝试了几个1.2x版本,结果不稳定。但是,在运行1.28.3之前,它从来没有咬过我。

注意,这种检查文件是否存在的方法是由演示文档建议的,例如here

这原来是一个bug,在提交了一个问题之后,它被修复了,应该包含在新版本的Deno(1.28.3+)中。

最新更新