<p dir="auto">When you execute a subprocess there's no guarantees of what it can do - all security bets are off. <code class="notranslate">--allow-run</code> masks this and is an extra command line flag. Therefore I suggest removing it entirely and requiring users to supply <code class="notranslate">--allow-all</code>.</p>
<p dir="auto">Both of the tests below have the same async function. They should both fail for leaking async ops. The first creates 2 timeouts, but the failure shows there is only 1 more dispatched than completed async ops. When the next test starts, it starts with there being one more dispatched op than completed ops. The setTimeout calls in the second test do not result in any change to the number of dispatched and completed async ops. Because of that, the second test does not fail.</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import { delay } from &quot;https://deno.land/std@0.80.0/async/delay.ts&quot;;

Deno.test(&quot;example 1&quot;, async () =&gt; {
  let metrics = Deno.metrics();
  console.log(
    `
Before:
- dispatched: ${metrics.opsDispatchedAsync}
- completed: ${metrics.opsCompletedAsync}`,
  );

  setTimeout(() =&gt; {}, 10000);
  setTimeout(() =&gt; {}, 10001);

  metrics = Deno.metrics();
  console.log(
    `After:
- dispatched: ${metrics.opsDispatchedAsync}
- completed: ${metrics.opsCompletedAsync}`,
  );
  await delay(0);
});

Deno.test(&quot;example 2&quot;, async () =&gt; {
  let metrics = Deno.metrics();
  console.log(
    `
Before:
- dispatched: ${metrics.opsDispatchedAsync}
- completed: ${metrics.opsCompletedAsync}`,
  );

  setTimeout(() =&gt; {}, 10000);
  setTimeout(() =&gt; {}, 10001);

  metrics = Deno.metrics();
  console.log(
    `After:
- dispatched: ${metrics.opsDispatchedAsync}
- completed: ${metrics.opsCompletedAsync}`,
  );
  await delay(0);
});"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-kos">{</span> <span class="pl-s1">delay</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">"https://deno.land/std@0.80.0/async/delay.ts"</span><span class="pl-kos">;</span>

<span class="pl-smi">Deno</span><span class="pl-kos">.</span><span class="pl-en">test</span><span class="pl-kos">(</span><span class="pl-s">"example 1"</span><span class="pl-kos">,</span> <span class="pl-k">async</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
  <span class="pl-k">let</span> <span class="pl-s1">metrics</span> <span class="pl-c1">=</span> <span class="pl-smi">Deno</span><span class="pl-kos">.</span><span class="pl-en">metrics</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span>
    <span class="pl-s">`</span>
<span class="pl-s">Before:</span>
<span class="pl-s">- dispatched: <span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">metrics</span><span class="pl-kos">.</span><span class="pl-c1">opsDispatchedAsync</span><span class="pl-kos">}</span></span></span>
<span class="pl-s">- completed: <span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">metrics</span><span class="pl-kos">.</span><span class="pl-c1">opsCompletedAsync</span><span class="pl-kos">}</span></span>`</span><span class="pl-kos">,</span>
  <span class="pl-kos">)</span><span class="pl-kos">;</span>

  <span class="pl-en">setTimeout</span><span class="pl-kos">(</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-c1">10000</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-en">setTimeout</span><span class="pl-kos">(</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-c1">10001</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

  <span class="pl-s1">metrics</span> <span class="pl-c1">=</span> <span class="pl-smi">Deno</span><span class="pl-kos">.</span><span class="pl-en">metrics</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span>
    <span class="pl-s">`After:</span>
<span class="pl-s">- dispatched: <span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">metrics</span><span class="pl-kos">.</span><span class="pl-c1">opsDispatchedAsync</span><span class="pl-kos">}</span></span></span>
<span class="pl-s">- completed: <span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">metrics</span><span class="pl-kos">.</span><span class="pl-c1">opsCompletedAsync</span><span class="pl-kos">}</span></span>`</span><span class="pl-kos">,</span>
  <span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-k">await</span> <span class="pl-en">delay</span><span class="pl-kos">(</span><span class="pl-c1">0</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-smi">Deno</span><span class="pl-kos">.</span><span class="pl-en">test</span><span class="pl-kos">(</span><span class="pl-s">"example 2"</span><span class="pl-kos">,</span> <span class="pl-k">async</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
  <span class="pl-k">let</span> <span class="pl-s1">metrics</span> <span class="pl-c1">=</span> <span class="pl-smi">Deno</span><span class="pl-kos">.</span><span class="pl-en">metrics</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span>
    <span class="pl-s">`</span>
<span class="pl-s">Before:</span>
<span class="pl-s">- dispatched: <span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">metrics</span><span class="pl-kos">.</span><span class="pl-c1">opsDispatchedAsync</span><span class="pl-kos">}</span></span></span>
<span class="pl-s">- completed: <span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">metrics</span><span class="pl-kos">.</span><span class="pl-c1">opsCompletedAsync</span><span class="pl-kos">}</span></span>`</span><span class="pl-kos">,</span>
  <span class="pl-kos">)</span><span class="pl-kos">;</span>

  <span class="pl-en">setTimeout</span><span class="pl-kos">(</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-c1">10000</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-en">setTimeout</span><span class="pl-kos">(</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-c1">10001</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

  <span class="pl-s1">metrics</span> <span class="pl-c1">=</span> <span class="pl-smi">Deno</span><span class="pl-kos">.</span><span class="pl-en">metrics</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span>
    <span class="pl-s">`After:</span>
<span class="pl-s">- dispatched: <span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">metrics</span><span class="pl-kos">.</span><span class="pl-c1">opsDispatchedAsync</span><span class="pl-kos">}</span></span></span>
<span class="pl-s">- completed: <span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">metrics</span><span class="pl-kos">.</span><span class="pl-c1">opsCompletedAsync</span><span class="pl-kos">}</span></span>`</span><span class="pl-kos">,</span>
  <span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-k">await</span> <span class="pl-en">delay</span><span class="pl-kos">(</span><span class="pl-c1">0</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="deno --version
deno 1.6.0 (release, x86_64-unknown-linux-gnu)
v8 8.8.278.2
typescript 4.1.2
deno test example_test.ts
Check file:///mnt/c/Users/kylej/projects/deno/test_suite/$deno$test.ts
running 2 tests
test example 1 ... 
Before:
- dispatched: 0
- completed: 0
After:
- dispatched: 1
- completed: 0
FAILED (5ms)
test example 2 ... 
Before:
- dispatched: 5
- completed: 4
After:
- dispatched: 5
- completed: 4
ok (3ms)

failures:

example 1
AssertionError: Test case is leaking async ops.
Before:
  - dispatched: 0
  - completed: 0
After:
  - dispatched: 5
  - completed: 4

Make sure to await all promises returned from Deno APIs before
finishing test case.
    at assert (deno:cli/rt/06_util.js:33:13)
    at asyncOpSanitizer (deno:cli/rt/40_testing.js:46:7)
    at async Object.resourceSanitizer [as fn] (deno:cli/rt/40_testing.js:70:7)
    at async TestRunner.[Symbol.asyncIterator] (deno:cli/rt/40_testing.js:245:13)
    at async Object.runTests (deno:cli/rt/40_testing.js:322:22)
    at async $deno$test.ts:3:1

failures:

        example 1

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out (9ms)"><pre class="notranslate">deno --version
deno 1.6.0 (release, x86_64-unknown-linux-gnu)
v8 8.8.278.2
typescript 4.1.2
deno <span class="pl-c1">test</span> example_test.ts
Check file:///mnt/c/Users/kylej/projects/deno/test_suite/<span class="pl-smi">$deno$test</span>.ts
running 2 tests
<span class="pl-c1">test</span> example 1 ... 
Before:
- dispatched: 0
- completed: 0
After:
- dispatched: 1
- completed: 0
FAILED (5ms)
<span class="pl-c1">test</span> example 2 ... 
Before:
- dispatched: 5
- completed: 4
After:
- dispatched: 5
- completed: 4
ok (3ms)

failures:

example 1
AssertionError: Test <span class="pl-k">case</span> is leaking async ops.
Before:
  - dispatched: 0
  - completed: 0
After:
  - dispatched: 5
  - completed: 4

Make sure to await all promises returned from Deno APIs before
finishing <span class="pl-c1">test</span> case.
    at assert (deno:cli/rt/06_util.js:33:13)
    at asyncOpSanitizer (deno:cli/rt/40_testing.js:46:7)
    at async Object.resourceSanitizer [as fn] (deno:cli/rt/40_testing.js:70:7)
    at async TestRunner.[Symbol.asyncIterator] (deno:cli/rt/40_testing.js:245:13)
    at async Object.runTests (deno:cli/rt/40_testing.js:322:22)
    at async <span class="pl-smi">$deno$test</span>.ts:3:1

failures:

        example 1

<span class="pl-c1">test</span> result: FAILED. 1 passed<span class="pl-k">;</span> 1 failed<span class="pl-k">;</span> 0 ignored<span class="pl-k">;</span> 0 measured<span class="pl-k">;</span> 0 filtered out (9ms)</pre></div>
<p dir="auto">The same issue applies if I make the test function syncronous by removing async key word and the await delay(0) from the end.</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Deno.test(&quot;example 1&quot;, () =&gt; {
  let metrics = Deno.metrics();
  console.log(
    `
Before:
- dispatched: ${metrics.opsDispatchedAsync}
- completed: ${metrics.opsCompletedAsync}`,
  );

  setTimeout(() =&gt; {}, 10000);
  setTimeout(() =&gt; {}, 10001);

  metrics = Deno.metrics();
  console.log(
    `After:
- dispatched: ${metrics.opsDispatchedAsync}
- completed: ${metrics.opsCompletedAsync}`,
  );
});

Deno.test(&quot;example 2&quot;, () =&gt; {
  let metrics = Deno.metrics();
  console.log(
    `
Before:
- dispatched: ${metrics.opsDispatchedAsync}
- completed: ${metrics.opsCompletedAsync}`,
  );

  setTimeout(() =&gt; {}, 10000);
  setTimeout(() =&gt; {}, 10001);

  metrics = Deno.metrics();
  console.log(
    `After:
- dispatched: ${metrics.opsDispatchedAsync}
- completed: ${metrics.opsCompletedAsync}`,
  );
});"><pre class="notranslate"><span class="pl-smi">Deno</span><span class="pl-kos">.</span><span class="pl-en">test</span><span class="pl-kos">(</span><span class="pl-s">"example 1"</span><span class="pl-kos">,</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
  <span class="pl-k">let</span> <span class="pl-s1">metrics</span> <span class="pl-c1">=</span> <span class="pl-smi">Deno</span><span class="pl-kos">.</span><span class="pl-en">metrics</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span>
    <span class="pl-s">`</span>
<span class="pl-s">Before:</span>
<span class="pl-s">- dispatched: <span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">metrics</span><span class="pl-kos">.</span><span class="pl-c1">opsDispatchedAsync</span><span class="pl-kos">}</span></span></span>
<span class="pl-s">- completed: <span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">metrics</span><span class="pl-kos">.</span><span class="pl-c1">opsCompletedAsync</span><span class="pl-kos">}</span></span>`</span><span class="pl-kos">,</span>
  <span class="pl-kos">)</span><span class="pl-kos">;</span>

  <span class="pl-en">setTimeout</span><span class="pl-kos">(</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-c1">10000</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-en">setTimeout</span><span class="pl-kos">(</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-c1">10001</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

  <span class="pl-s1">metrics</span> <span class="pl-c1">=</span> <span class="pl-smi">Deno</span><span class="pl-kos">.</span><span class="pl-en">metrics</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span>
    <span class="pl-s">`After:</span>
<span class="pl-s">- dispatched: <span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">metrics</span><span class="pl-kos">.</span><span class="pl-c1">opsDispatchedAsync</span><span class="pl-kos">}</span></span></span>
<span class="pl-s">- completed: <span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">metrics</span><span class="pl-kos">.</span><span class="pl-c1">opsCompletedAsync</span><span class="pl-kos">}</span></span>`</span><span class="pl-kos">,</span>
  <span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-smi">Deno</span><span class="pl-kos">.</span><span class="pl-en">test</span><span class="pl-kos">(</span><span class="pl-s">"example 2"</span><span class="pl-kos">,</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
  <span class="pl-k">let</span> <span class="pl-s1">metrics</span> <span class="pl-c1">=</span> <span class="pl-smi">Deno</span><span class="pl-kos">.</span><span class="pl-en">metrics</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span>
    <span class="pl-s">`</span>
<span class="pl-s">Before:</span>
<span class="pl-s">- dispatched: <span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">metrics</span><span class="pl-kos">.</span><span class="pl-c1">opsDispatchedAsync</span><span class="pl-kos">}</span></span></span>
<span class="pl-s">- completed: <span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">metrics</span><span class="pl-kos">.</span><span class="pl-c1">opsCompletedAsync</span><span class="pl-kos">}</span></span>`</span><span class="pl-kos">,</span>
  <span class="pl-kos">)</span><span class="pl-kos">;</span>

  <span class="pl-en">setTimeout</span><span class="pl-kos">(</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-c1">10000</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-en">setTimeout</span><span class="pl-kos">(</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-c1">10001</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

  <span class="pl-s1">metrics</span> <span class="pl-c1">=</span> <span class="pl-smi">Deno</span><span class="pl-kos">.</span><span class="pl-en">metrics</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span>
    <span class="pl-s">`After:</span>
<span class="pl-s">- dispatched: <span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">metrics</span><span class="pl-kos">.</span><span class="pl-c1">opsDispatchedAsync</span><span class="pl-kos">}</span></span></span>
<span class="pl-s">- completed: <span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">metrics</span><span class="pl-kos">.</span><span class="pl-c1">opsCompletedAsync</span><span class="pl-kos">}</span></span>`</span><span class="pl-kos">,</span>
  <span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="deno test example_test.ts
Check file:///mnt/c/Users/kylej/projects/deno/test_suite/$deno$test.ts
running 2 tests
test example 1 ... 
Before:
- dispatched: 0
- completed: 0
After:
- dispatched: 1
- completed: 0
FAILED (3ms)
test example 2 ... 
Before:
- dispatched: 3
- completed: 2
After:
- dispatched: 3
- completed: 2
ok (2ms)

failures:

example 1
AssertionError: Test case is leaking async ops.
Before:
  - dispatched: 0
  - completed: 0
After:
  - dispatched: 3
  - completed: 2

Make sure to await all promises returned from Deno APIs before
finishing test case.
    at assert (deno:cli/rt/06_util.js:33:13)
    at asyncOpSanitizer (deno:cli/rt/40_testing.js:46:7)
    at async Object.resourceSanitizer [as fn] (deno:cli/rt/40_testing.js:70:7)
    at async TestRunner.[Symbol.asyncIterator] (deno:cli/rt/40_testing.js:245:13)
    at async Object.runTests (deno:cli/rt/40_testing.js:322:22)
    at async $deno$test.ts:3:1

failures:

        example 1

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out (5ms)"><pre class="notranslate">deno <span class="pl-c1">test</span> example_test.ts
Check file:///mnt/c/Users/kylej/projects/deno/test_suite/<span class="pl-smi">$deno$test</span>.ts
running 2 tests
<span class="pl-c1">test</span> example 1 ... 
Before:
- dispatched: 0
- completed: 0
After:
- dispatched: 1
- completed: 0
FAILED (3ms)
<span class="pl-c1">test</span> example 2 ... 
Before:
- dispatched: 3
- completed: 2
After:
- dispatched: 3
- completed: 2
ok (2ms)

failures:

example 1
AssertionError: Test <span class="pl-k">case</span> is leaking async ops.
Before:
  - dispatched: 0
  - completed: 0
After:
  - dispatched: 3
  - completed: 2

Make sure to await all promises returned from Deno APIs before
finishing <span class="pl-c1">test</span> case.
    at assert (deno:cli/rt/06_util.js:33:13)
    at asyncOpSanitizer (deno:cli/rt/40_testing.js:46:7)
    at async Object.resourceSanitizer [as fn] (deno:cli/rt/40_testing.js:70:7)
    at async TestRunner.[Symbol.asyncIterator] (deno:cli/rt/40_testing.js:245:13)
    at async Object.runTests (deno:cli/rt/40_testing.js:322:22)
    at async <span class="pl-smi">$deno$test</span>.ts:3:1

failures:

        example 1

<span class="pl-c1">test</span> result: FAILED. 1 passed<span class="pl-k">;</span> 1 failed<span class="pl-k">;</span> 0 ignored<span class="pl-k">;</span> 0 measured<span class="pl-k">;</span> 0 filtered out (5ms)</pre></div>