From 76469146e7ec33e1443e271c9fb2b495d22a06f9 Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Thu, 25 Jul 2024 15:54:55 +0200 Subject: [PATCH] fix: no early return in `loadTransportStreamBuilder` (#2014) --- lib/transport-stream.js | 3 +-- test/transport-stream.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 test/transport-stream.test.js diff --git a/lib/transport-stream.js b/lib/transport-stream.js index d1f1fd15f..22cb37e0a 100644 --- a/lib/transport-stream.js +++ b/lib/transport-stream.js @@ -7,7 +7,7 @@ module.exports = loadTransportStreamBuilder /** * Loads & returns a function to build transport streams * @param {string} target - * @returns {function(object): Promise} + * @returns {Promise>} * @throws {Error} In case the target module does not export a function */ async function loadTransportStreamBuilder (target) { @@ -31,7 +31,6 @@ async function loadTransportStreamBuilder (target) { // See this PR for details: https://github.com/pinojs/thread-stream/pull/34 if ((error.code === 'ENOTDIR' || error.code === 'ERR_MODULE_NOT_FOUND')) { fn = realRequire(target) - return } else if (error.code === undefined || error.code === 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING') { // When bundled with pkg, an undefined error is thrown when called with realImport // When bundled with pkg and using node v20, an ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING error is thrown when called with realImport diff --git a/test/transport-stream.test.js b/test/transport-stream.test.js new file mode 100644 index 000000000..488aceb77 --- /dev/null +++ b/test/transport-stream.test.js @@ -0,0 +1,26 @@ +'use strict' + +const { test } = require('tap') + +test('should import', async (t) => { + t.plan(2) + const mockRealRequire = (target) => { + return { + default: { + default: () => { + t.equal(target, 'pino-pretty') + return Promise.resolve() + } + } + } + } + const mockRealImport = async () => { await Promise.resolve(); throw Object.assign(new Error(), { code: 'ERR_MODULE_NOT_FOUND' }) } + + /** @type {typeof import('../lib/transport-stream.js')} */ + const loadTransportStreamBuilder = t.mock('../lib/transport-stream.js', { 'real-require': { realRequire: mockRealRequire, realImport: mockRealImport } }) + + const fn = await loadTransportStreamBuilder('pino-pretty') + + t.resolves(fn()) + t.end() +})