Below is a test case example for Deno that I am using:
Deno.test("Run LS", () => {
const cmd = Deno.run({
cmd: ["ls"],
stdout: "piped",
stderr: "piped",
});
let status: Deno.ProcessStatus, stdout: string, stderr: string;
const f = async () => {
const [status_, stdout_, stderr_] = await Promise.all([
cmd.status(),
cmd.output(),
cmd.stderrOutput(),
]);
cmd.close();
status = status_;
stdout = new TextDecoder().decode(stdout_);
stderr = new TextDecoder().decode(stderr_);
};
f()
.then(
function (result) {
console.log(`My result: ${result}`);
console.log(`status: `, status);
console.log(`stdout: `, stdout);
console.log(`stderr: `, stderr);
},
function (error) {
console.log(`My error: ${error}`);
}
)
.catch(function (error) {
console.log(`My cought error: ${error}`);
})
.finally(function () {
console.log("In the finally clause...");
});
});
When running the test using
deno test --allow-read --allow-run --unstable <filename>
, sometimes the output is as follows:
running 1 tests
test fetch pending tasks ... My result: undefined
status: { success: true, code: 0 }
stdout: a.ts
<rest of files in the directory>
stderr:
In the finally clause...
ok (7ms)
Other times, an assertion error occurs:
running 1 tests
test Run LS ... FAILED (4ms)
failures:
Run LS
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:runtime/js/06_util.js:34:13)
at asyncOpSanitizer (deno:runtime/js/40_testing.js:50:7)
at async resourceSanitizer (deno:runtime/js/40_testing.js:74:7)
at async Object.exitSanitizer [as fn] (deno:runtime/js/40_testing.js:101:9)
at async TestRunner.[Symbol.asyncIterator] (deno:runtime/js/40_testing.js:275:13)
at async Object.runTests (deno:runtime/js/40_testing.js:352:22)
at async file:///home/berger/src/taskwarrior-deno/$deno$test.ts:3:1
failures:
Run LS
If I run the function as a regular script instead of using Deno.test()
, the assertion error does not occur.
Is this the correct way to use Deno.run
? If so, please advise if there are any issues with my implementation or if it is deno itself causing the problem.
I'm new to JavaScript, so any additional tips on improving the code are welcome.