Skip to content

Assertion on 'finish' of a Writeable stream #2872

Answered by sindresorhus
Sparticuz asked this question in Q&A
Discussion options

You must be logged in to vote

Your question is about JavaScript, not AVA specifically. The async test function will finish as soon as all the await's are resolved and the following synchronous code executed. The .on handler will never have time to fire. Instead of using .pipe, use stream.pipeline. Something like this:

const test = require("ava");
const fs = require("fs");
const {pipeline: streamPipeline} = require('stream/promises');
const invoice = require("./invoice_payload.json");

test("Make Invoice PDF", async (t) => {
  // Creates a Readable stream
  const invoiceFile = await fillInvoice(invoice[0]);
  const output = fs.createWriteStream("test/filledInvoice.pdf");
  await streamPipeline(invoiceFile, output);
});

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Sparticuz
Comment options

Answer selected by Sparticuz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants