From fe0c832ecb2c204c29fc0885b99cacdc099c94a9 Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 3 Apr 2023 19:41:00 -0700 Subject: [PATCH 1/4] added failing test --- src/esmockModule.js | 1 + tests/local/usesNodeProcess.js | 3 +++ tests/tests-node/esmock.node.test.js | 9 +++++++++ 3 files changed, 13 insertions(+) create mode 100644 tests/local/usesNodeProcess.js diff --git a/src/esmockModule.js b/src/esmockModule.js index 1342e6bb..729500e1 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -33,6 +33,7 @@ const esmockModuleMergeDefault = (defLive, def) => (isObj(defLive) && isObj(def)) ? esmockModuleMerge(defLive, def) : def const esmockModuleApply = (defLive, def, fileURL) => { + console.oog() def = Object.assign({}, defLive || {}, { default: esmockModuleMergeDefault( isDefaultIn(defLive) && defLive.default, diff --git a/tests/local/usesNodeProcess.js b/tests/local/usesNodeProcess.js new file mode 100644 index 00000000..84a2cff6 --- /dev/null +++ b/tests/local/usesNodeProcess.js @@ -0,0 +1,3 @@ +import nodeprocess from 'node:process' + +export default nodeprocess diff --git a/tests/tests-node/esmock.node.test.js b/tests/tests-node/esmock.node.test.js index 728520c7..4b1a258c 100644 --- a/tests/tests-node/esmock.node.test.js +++ b/tests/tests-node/esmock.node.test.js @@ -5,6 +5,15 @@ import esmock from 'esmock' import sinon from 'sinon' import esmockCache from '../../src/esmockCache.js' +test('should mock node:process', async () => { + // has direct and in-direct calls to `process.cwd()` + const thingBeingTested = await esmock('../local/usesNodeProcess.js', {}, { + 'node:process': { cwd: () => 'tempDir' } + }) + + assert.strictEqual(thingBeingTested.cwd(), 'tempDir') +}) + test('should mock package, even when package is not installed', async () => { const component = await esmock(`../local/notinstalledVueComponent.js`, { vue: { From 75ca7116ab227167d93990d0b97ee5e2c1c8abb1 Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 3 Apr 2023 19:42:16 -0700 Subject: [PATCH 2/4] remove bad line --- src/esmockModule.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/esmockModule.js b/src/esmockModule.js index 729500e1..1342e6bb 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -33,7 +33,6 @@ const esmockModuleMergeDefault = (defLive, def) => (isObj(defLive) && isObj(def)) ? esmockModuleMerge(defLive, def) : def const esmockModuleApply = (defLive, def, fileURL) => { - console.oog() def = Object.assign({}, defLive || {}, { default: esmockModuleMergeDefault( isDefaultIn(defLive) && defLive.default, From b3f7af549a9c0f5eb50c8837263843befaa9257a Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 3 Apr 2023 20:18:25 -0700 Subject: [PATCH 3/4] use Object.defineProperty. update CHANGELOG. --- CHANGELOG.md | 5 +++++ src/esmockModule.js | 5 ++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dbd9a023..38064c34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # changelog + * 2.2.1 _Apr.03.2023_ + * [use Object.defineProperty](https://github.com/iambumblehead/esmock/pull/197) to write mock definitions protected on inherited prototype chain + * 2.2.0 _Mar.23.2023_ + * [throw error when](https://github.com/iambumblehead/esmock/pull/193) esmock.strictest is called with empty mock definition, @koshic @Swivelgames + * [update jest and jest-light-runner packages](https://github.com/iambumblehead/esmock/pull/194) at unit-tests, @koshic * 2.1.0 _Nov.29.2022_ * [add node v19](https://github.com/iambumblehead/esmock/pull/189) to ci-test pipeline * [use live default export](https://github.com/iambumblehead/esmock/pull/189) to populate enumerable properties of mock definition diff --git a/src/esmockModule.js b/src/esmockModule.js index 1342e6bb..2d2f17e8 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -27,7 +27,10 @@ const isPlainObj = o => Object.getPrototypeOf(o) === objProto const esmockModuleMerge = (defLive, def) => isPlainObj(defLive) ? Object.assign({}, defLive, def) : Object.assign(Object.keys(defLive).reduce( - (prev, k) => (prev[k] = defLive[k], prev), Object.create(defLive)), def) + (prev, k) => (Object.defineProperty(prev, k, { + value: defLive[k], + writable: true + }), prev), Object.create(defLive)), def) const esmockModuleMergeDefault = (defLive, def) => (isObj(defLive) && isObj(def)) ? esmockModuleMerge(defLive, def) : def From 2a857e51d3875043dc0429f17e088dd13148e269 Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 3 Apr 2023 20:19:35 -0700 Subject: [PATCH 4/4] patch-increment version number --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f370294b..8ed2c32b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "esmock", "type": "module", - "version": "2.2.0", + "version": "2.2.1", "license": "ISC", "readmeFilename": "README.md", "description": "provides native ESM import mocking for unit tests",