Skip to content

Commit

Permalink
change condition for noloader test
Browse files Browse the repository at this point in the history
  • Loading branch information
bumblehead committed Sep 9, 2023
1 parent 264b981 commit 07af9d6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/esmock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
14 changes: 8 additions & 6 deletions tests/tests-no-loader/esmock.noloader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
})
})
}

0 comments on commit 07af9d6

Please sign in to comment.