From 110bd602c3b4c7d9f48a6aab68b31848238356af Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 7 Aug 2023 14:31:48 -0700 Subject: [PATCH 1/2] use nextLoad().format rather than context.format --- src/esmockLoader.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/esmockLoader.js b/src/esmockLoader.js index f94ae4e2..d506fc9b 100644 --- a/src/esmockLoader.js +++ b/src/esmockLoader.js @@ -138,7 +138,8 @@ 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' @@ -146,7 +147,7 @@ const load = async (url, context, nextLoad) => { : `import {${importedNames}} from '${specifier}';` return { - format: context.format === 'commonjs' ? 'commonjs' : 'module', + format: nextLoadRes.format, shortCircuit: true, responseURL: encodeURI(url), source: hbang + importexpr + sourcesafe From bbb55023a172516fcb97ff96b3fa0ae340a8d26a Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 7 Aug 2023 14:50:46 -0700 Subject: [PATCH 2/2] increment package.json version, update changelog, added unit-test --- CHANGELOG.md | 2 ++ package.json | 2 +- tests/local/usesModuleWithCJSDependency.ts | 6 ++++++ tests/tests-nodets/esmock.node-ts.test.ts | 15 ++++++++++++++- 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 tests/local/usesModuleWithCJSDependency.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 36a7cb88..06394047 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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_ diff --git a/package.json b/package.json index aef55476..36e4eef5 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tests/local/usesModuleWithCJSDependency.ts b/tests/local/usesModuleWithCJSDependency.ts new file mode 100644 index 00000000..5338191a --- /dev/null +++ b/tests/local/usesModuleWithCJSDependency.ts @@ -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) diff --git a/tests/tests-nodets/esmock.node-ts.test.ts b/tests/tests-nodets/esmock.node-ts.test.ts index ad4db8e8..de5f5f21 100644 --- a/tests/tests-nodets/esmock.node-ts.test.ts +++ b/tests/tests-nodets/esmock.node-ts.test.ts @@ -1,4 +1,4 @@ -import test from 'node:test' +import test, { mock } from 'node:test' import assert from 'assert' import esmock from 'esmock' @@ -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) +})