Search code examples
avanode-streams

Readable stream `_read` not called in test


I have the following simplified ava test case. When I run it by ava _read() never get called (ONDATA is OK). On the other hand, when I run this test body (without assertions) as node script I always get _read() called as expected. Probably I miss some key feature, please advice.

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(...)
})

Solution

  • I can't immediately recall in what scenario _read() should get called, but most likely your test ends before that happens. You have an async test but you don't seem to await anything. Try returning an explicit promise or otherwise use test.cb() so you can end the test with t.end().