Open1
Deno テストで Leaks detected というエラーが発生したとき原因と解決例
test.ts
import { assertRejects } from "jsr:@std/assert";
import { delay } from "jsr:@std/async";
Deno.test("async function test", () => {
assertRejects(async () => {
await delay(3000);
throw new Error("error");
});
});
というエラーが発生することを確認するテストを作成したとき
deno test
でテストを実行すると
running 1 test from ./test.ts
async function test ... FAILED (2ms)
ERRORS
async function test => ./test.ts:4:6
error: Leaks detected:
- A timer was started in this test, but never completed. This is often caused by not calling `clearTimeout`.
To get more details where leaks occurred, run again with the --trace-leaks flag.
FAILURES
async function test => ./test.ts:4:6
FAILED | 0 passed | 1 failed (13ms)
error: Test failed
のようにエラーが発生してしまいます
解決策は
test.ts
import { assertRejects } from "jsr:@std/assert";
import { delay } from "jsr:@std/async";
Deno.test("async function test", async () => {
await assertRejects(async () => {
await delay(3000);
throw new Error("error");
});
});
のように await
を付与すると直ります