Skip to content

Commit

Permalink
replace regexp with more explicit test
Browse files Browse the repository at this point in the history
  • Loading branch information
bumblehead committed Sep 7, 2023
1 parent b0c3280 commit 289cb62
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/esmockLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const exportNamesRe = /.*exportNames=(.*)/
const withHashRe = /.*#-#/
const isesmRe = /isesm=true/
const isnotfoundRe = /isfound=false/
const isnullundefinedRe = /^(null|undefined)$/
const iscommonjsmoduleRe = /^(commonjs|module)$/
const isstrict3 = /strict=3/
const hashbangRe = /^(#![^\n]*\n)/
Expand Down Expand Up @@ -149,7 +148,9 @@ const load = async (url, context, nextLoad) => {
return nextLoad(url, context)

// nextLoadRes.source sometimes 'undefined' and other times 'null' :(
const source = isnullundefinedRe.test(typeof nextLoadRes.source)
const sourceIsNullLike = (
nextLoadRes.source === null || nextLoadRes.source === undefined)
const source = sourceIsNullLike
? String(await fs.readFile(new URL(url)))
: String(nextLoadRes.source)
const hbang = (source.match(hashbangRe) || [])[0] || ''
Expand Down
6 changes: 3 additions & 3 deletions tests/tests-node/esmock.node.global.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ test('should mock files with hashbangs', async () => {
await esmock('../local/hashbang.js', {
'../local/env.js': { TESTCONSTANT: 'foo' },
import: {
console: { log: (...args) => logs.push(...args) }
console: { log: () => logs.push('foo') }
}
})

Expand All @@ -69,9 +69,9 @@ test('should work when modules have CJS imports', async () => {

await esmock('../local/usesModuleWithCJSDependency.js', {}, {
import: {
console: { log: (...args) => logs.push(...args) }
console: { log: () => logs.push('foo') }
}
})

assert.ok(logs.some(n => n === '\nfoo\n'))
assert.ok(logs.some(n => n === 'foo'))
})
2 changes: 1 addition & 1 deletion tests/tests-nodets/esmock.node-ts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('should mock import global at import tree w/ mixed esm cjs', async () => {
const consolelog = mock.fn()
const trigger = await esmock('../local/usesModuleWithCJSDependency.ts', {}, {
import: {
console: { log: n => consolelog('foo') }
console: { log: () => consolelog('foo') }
}
})

Expand Down

0 comments on commit 289cb62

Please sign in to comment.