Skip to content

Commit

Permalink
Merge pull request #224 from iambumblehead/resolve-218-import-key-cjs…
Browse files Browse the repository at this point in the history
…-modules

use nextLoad().format rather than context.format
  • Loading branch information
iambumblehead authored Aug 7, 2023
2 parents 17cbe8d + bbb5502 commit fd4b0e3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# changelog

* 2.3.6 _Aug.07.2023_
* [resolve global mocking issues](https://github.com/iambumblehead/esmock/pull/224) when using mixed esm cjs import trees
* 2.3.4 _Jul.30.2023_
* [do not error when mocking commonjs](https://github.com/iambumblehead/esmock/pull/220) global values, found by @tommy-mitchell
* 2.3.3 _Jul.28.2023_
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "esmock",
"type": "module",
"version": "2.3.5",
"version": "2.3.6",
"license": "ISC",
"readmeFilename": "README.md",
"description": "provides native ESM import and globals mocking for unit tests",
Expand Down
5 changes: 3 additions & 2 deletions src/esmockLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,16 @@ const load = async (url, context, nextLoad) => {
if (treeid) {
const [specifier, importedNames] = parseImportsTree(treeidspec)
if (importedNames && importedNames.length) {
const source = String((await nextLoad(url, context)).source)
const nextLoadRes = await nextLoad(url, context)
const source = String(nextLoadRes.source)
const hbang = (source.match(hashbangRe) || [])[0] || ''
const sourcesafe = hbang ? source.replace(hashbangRe, '') : source
const importexpr = context.format === 'commonjs'
? `const {${importedNames}} = require('${specifier}');`
: `import {${importedNames}} from '${specifier}';`

return {
format: context.format === 'commonjs' ? 'commonjs' : 'module',
format: nextLoadRes.format,
shortCircuit: true,
responseURL: encodeURI(url),
source: hbang + importexpr + sourcesafe
Expand Down
6 changes: 6 additions & 0 deletions tests/local/usesModuleWithCJSDependency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env ts-node-esm
import meow from 'meow'

const cli = meow('foo', { importMeta: import.meta })

export default () => console.log(cli.help)
15 changes: 14 additions & 1 deletion tests/tests-nodets/esmock.node-ts.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'node:test'
import test, { mock } from 'node:test'
import assert from 'assert'
import esmock from 'esmock'

Expand All @@ -12,3 +12,16 @@ test('should mock ts when using node-ts', { only: true }, async () => {
assert.strictEqual(main.pathbasenamewrap(), 'hellow')
assert.ok(true)
})

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: consolelog }
}
})

trigger()
trigger()
assert.strictEqual(consolelog.mock.calls.length, 2)
})

0 comments on commit fd4b0e3

Please sign in to comment.