Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to ignore file leaks with deno test #27454

Open
abdurahmanshiine opened this issue Dec 23, 2024 · 3 comments
Open

How to ignore file leaks with deno test #27454

abdurahmanshiine opened this issue Dec 23, 2024 · 3 comments
Labels
question a question about the use of Deno

Comments

@abdurahmanshiine
Copy link

abdurahmanshiine commented Dec 23, 2024

Hey guys,

I keep getting this error

error: Leaks detected:
  - A file was opened during the test, but not closed during the test. Close the file handle by calling `file.close()`.

The issue is it's not telling me which file it is, or which file object it's referring to, so I was wondering how can I disable leaks tracing in testing, I honestly don't even need that at this point.

Version: Deno 2.1.4

@kt3k
Copy link
Member

kt3k commented Dec 25, 2024

You can disable resource leak detection by passing sanitizeResources: false option to Deno.test

https://docs.deno.com/runtime/fundamentals/testing/#resource-sanitizer

Deno.test("my test", { sanitizeResources: false }, async () => {
  const file = await Deno.open("README.md");
  // now it's fine to leave the file open
});

@kt3k kt3k added the question a question about the use of Deno label Dec 25, 2024
@abdurahmanshiine
Copy link
Author

Hey @kt3k,
Thanks for your response. I'm using the @std/testing package so I can write the tests in jest style. Can I pass the same option to it?

@kt3k
Copy link
Member

kt3k commented Dec 25, 2024

Yes, you can pass sanitizeResources: false to describe or it:

import { describe, it } from "jsr:@std/testing/bdd";

describe("foo", { sanitizeResources: false }, () => {
  it("bar", async () => {
    const file = await Deno.open("README.md");
  });
});

Alternatively, there's unstable API for disabling leak detection for the entire file:

import { describe, it } from "jsr:@std/testing/bdd";
import { configureGlobalSanitizers } from "jsr:@std/testing/unstable-bdd";

configureGlobalSanitizers({
  sanitizeResources: false,
});

describe("foo", () => {
  it("bar", async () => {
    const file = await Deno.open("README.md");
  });
});

This might look cleaner if there's many test cases in a single file.

FYI there's also a similar option sanitizeOps: false which disables the detection of async operation leaks such as setTimeout. You might be also interested in disabling this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question a question about the use of Deno
Projects
None yet
Development

No branches or pull requests

2 participants