From cb87b292d40dc0b5fb88c4c002dd444470dc2b1d Mon Sep 17 00:00:00 2001 From: YDarma Date: Wed, 25 May 2022 16:50:57 +0200 Subject: [PATCH] stream locking --- index.js | 16 +++++++++++++--- test/wait-run-object-stream.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 test/wait-run-object-stream.js diff --git a/index.js b/index.js index 7a7211e8..48a35138 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ var createDefaultStream = require('./lib/default_stream'); var Test = require('./lib/test'); var createResult = require('./lib/results'); var through = require('@ljharb/through'); +var EventEmitter = require('events').EventEmitter; var canEmitExit = typeof process !== 'undefined' && process && typeof process.on === 'function' && process.browser !== true; @@ -53,6 +54,10 @@ module.exports = (function () { return harness.createStream(options); }; + lazyLoad.async = function () { + return getHarness().async.apply(this, arguments); + }; + lazyLoad.onFinish = function () { return getHarness().onFinish.apply(this, arguments); }; @@ -142,10 +147,15 @@ function createExitHarness(conf, wait) { stream.on('end', function () { ended = true; }); } + run(); + if (wait) { - harness.run = run; - } else { - run(); + var waiter = new EventEmitter(); + waiter.run = function () {}; + harness._results.push(waiter); + harness.run = function () { + waiter.emit('end'); + }; } if (config.exit === false) { return harness; } diff --git a/test/wait-run-object-stream.js b/test/wait-run-object-stream.js new file mode 100644 index 00000000..c195a259 --- /dev/null +++ b/test/wait-run-object-stream.js @@ -0,0 +1,33 @@ +'use strict'; + +var tap = require('tap'); +var tape = require('../'); + +tap.test('Create stream then import async tests', function (t) { + t.plan(3); + + var actualTests = function () { + tape('This one should pass', function (t1) { + t1.pass('This one should pass'); + t1.end(); + }); + }; + + var simulateAsyncEsmImport = function () { + return new Promise(function (resolve) { + setTimeout(function () { + actualTests(); + resolve(); + }); + }); + }; + + tape.createStream({ objectMode: true }).on('data', function (res) { + t.pass(res.type); + }); + + tape.wait(); + simulateAsyncEsmImport().then(function () { + tape.run(); + }); +});