1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-01 16:51:13 -05:00
denoland-deno/docs/testing/sanitizers.md
Casper Beyer 6aad9749d2
docs(manual): split testing into multiple chapters (#11067)
Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
2021-07-10 00:52:31 +02:00

1.7 KiB

Test Sanitizers

The test runner offers several sanitizers to ensure that the test behaves in a reasonable and expected way.

Resource sanitizer

Certain actions in Deno create resources in the resource table (learn more here).

These resources should be closed after you are done using them.

For each test definition, the test runner checks that all resources created in this test have been closed. This is to prevent resource 'leaks'. This is enabled by default for all tests, but can be disabled by setting the sanitizeResources boolean to false in the test definition.

Deno.test({
  name: "leaky resource test",
  async fn() {
    await Deno.open("hello.txt");
  },
  sanitizeResources: false,
});

Op sanitizer

The same is true for async operation like interacting with the filesystem. The test runner checks that each operation you start in the test is completed before the end of the test. This is enabled by default for all tests, but can be disabled by setting the sanitizeOps boolean to false in the test definition.

Deno.test({
  name: "leaky operation test",
  fn() {
    setTimeout(function () {}, 1000);
  },
  sanitizeOps: false,
});

Exit sanitizer

There's also the exit sanitizer which ensures that tested code doesn't call Deno.exit() signaling a false test success.

This is enabled by default for all tests, but can be disabled by setting the sanitizeExit boolean to false in the test definition.

Deno.test({
  name: "false success",
  fn() {
    Deno.exit(0);
  },
  sanitizeExit: false,
});

// This test never runs, because the process exits during "false success" test
Deno.test({
  name: "failing test",
  fn() {
    throw new Error("this test fails");
  },
});