【问题标题】:Non deterministic AssertionError during deno-testdeno 测试期间的非确定性 AssertionError
【发布时间】:2021-06-10 07:53:01
【问题描述】:

我有以下 deno 虚拟测试用例:

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...");
    });
});

当我使用deno test --allow-read --allow-run --unstable <filename> 运行它时,有时我会得到以下正确输出:

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)

有时我会发出以下断言错误:

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

如果我不将函数包装在 Deno.test(...) 中,而是将其作为常规脚本运行(即 deno run ... 而不是 deno.test),那么我永远不会收到此断言错误。

这是调用和使用Deno.run 的正确方式吗?如果是这样,还有什么我做错了吗? deno 本身运行我的测试用例的方式是否有错误?

顺便说一句,我对整个 JavaScript 生态系统非常陌生,所以如果您对代码有任何进一步的建议,请随时提出。

【问题讨论】:

    标签: javascript typescript async-await deno


    【解决方案1】:

    我问了与 deno github 问题相同的问题,并得到了以下工作版本:

    链接:https://github.com/denoland/deno/issues/9767#issuecomment-797466192

    Deno.test("Run LS", async () => {
      const cmd = Deno.run({
        cmd: ["ls"],
        stdout: "piped",
        stderr: "piped",
      });
      const f = async () => {
        const [status_, stdout_, stderr_] = await Promise.all([
          cmd.status(),
          cmd.output(),
          cmd.stderrOutput(),
        ]);
        cmd.close();
    
        return {
          status: status_,
          stdout: new TextDecoder().decode(stdout_),
          stderr: new TextDecoder().decode(stderr_),
        };
      };
      try {
        const { status, stderr, stdout } = await f();
        console.log("status:", status);
        console.log("stdout:", stdout);
        console.log("stderr:", stderr);
      } catch (error) {
        console.log("Caught error:", error);
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-10
      • 2014-03-22
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-13
      相关资源
      最近更新 更多