测试中未调用可读流“_read”

Readable stream `_read` not called in test

我有以下简化的 ava 测试用例。当我 运行 它被 ava _read() 调用时,它永远不会被调用(ONDATA 可以)。另一方面,当我 运行 这个测试主体(没有断言)作为节点脚本时,我总是按预期得到 _read() 调用。 可能我遗漏了一些关键功能,请指教。

test('...', async t => {
  class R extends stream.Readable {
    _read() { console.log('READ'); }
  }
  const rs = new R();

  rs.on('data', data => {
    console.log('ONDATA ', data.toString());
  });
  rs.push(Buffer.from('data'));
  // t.is(...)
})

我无法立即回忆起在什么情况下应该调用 _read(),但很可能您的测试会在此之前结束。你有一个 async 测试,但你似乎没有 await 任何东西。尝试返回一个明确的承诺或以其他方式使用 test.cb() 这样你就可以用 t.end().

结束测试

谢谢!本质上我搞砸了可读的流回调和 async test。所需的测试看起来像

test.cb('...', t => {
  class R extends stream.Readable {
    _read() {
      console.log('READ');
      // t.pass() or t.end() HERE
    }
  }
  const rs = new R();

  rs.on('data', data => {
    // t.pass() or t.end() HERE
    console.log('ONDATA ', data.toString());
  });
  rs.push(Buffer.from('data'));
})