diff --git a/src/esmock.js b/src/esmock.js index 9fb7bf9a..5115ed68 100644 --- a/src/esmock.js +++ b/src/esmock.js @@ -37,8 +37,12 @@ const esmock = Object.assign(esmockGo(), { export {esmock as default, strict, strictest} +// for older node versions, to support "--loader=esmock" rather than +// "--loader=esmock/loader", esmock.js exported loader hook definitions here +// +// for newer node versions 20.6+, exported hook definitions here cause +// problems when --loader called and esmock initializes newer message channels const isMessageChannel = Boolean(module.register && threads.MessageChannel) const hooksFinal = isMessageChannel ? {} : hooks const { load, resolve, getSource, initialize, globalPreload } = hooksFinal export { load, resolve, getSource, initialize, globalPreload } -// export * from './esmockLoader.js' diff --git a/tests/tests-no-loader/esmock.noloader.test.js b/tests/tests-no-loader/esmock.noloader.test.js index 5fbb5ba6..df0dfc28 100644 --- a/tests/tests-no-loader/esmock.noloader.test.js +++ b/tests/tests-no-loader/esmock.noloader.test.js @@ -9,6 +9,14 @@ const isLT206 = (([major, minor]) => ( process.versions.node.split('.').map(it => +it)) if (isLT206) { + test('should throw error if !esmockloader', async () => { + await assert.rejects(() => esmock('./to/module.js'), { + message: esmockErr.errMissingLoader().message + }) + }) +} + +if (!isLT206) { test('should mock a module', async () => { const main = await esmock('../local/mainUtil.js', { 'form-urlencoded': () => 'mock encode' @@ -17,10 +25,4 @@ if (isLT206) { assert.strictEqual(typeof main, 'function') assert.strictEqual(main.createString(), 'mock encode') }) -} else { - test('should throw error if !esmockloader', async () => { - await assert.rejects(() => esmock('./to/module.js'), { - message: esmockErr.errMissingLoader().message - }) - }) }