From f66a8e658eda8256b8ef506e7cd7150658a3777c Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 28 Nov 2022 19:31:00 -0800 Subject: [PATCH 1/7] use live object to assign enumerable properties --- CHANGELOG.md | 2 ++ package.json | 2 +- src/esmockModule.js | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8df08937..cb66f981 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # changelog + * 2.1.0 _Nov.29.2022_ + * [use live default export](https://github.com/iambumblehead/esmock/pull/189) to populate enumerable properties of mock definition * 2.0.9 _Nov.26.2022_ * [resolve windows modules with correct drive letter](https://github.com/iambumblehead/esmock/pull/189) using [resolvewithplus patch](https://github.com/iambumblehead/resolvewithplus/pull/42) from @mshima * 2.0.8 _Nov.25.2022_ diff --git a/package.json b/package.json index 385fa3db..947cf720 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "esmock", "type": "module", - "version": "2.0.9", + "version": "2.1.0", "license": "ISC", "readmeFilename": "README.md", "description": "provides native ESM import mocking for unit tests", diff --git a/src/esmockModule.js b/src/esmockModule.js index 2ccc701e..a42d1bfa 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -20,7 +20,7 @@ const asFileURL = p => p.startsWith('file://') ? p : url.pathToFileURL(p) const esmockModuleMergeDefault = (defLive, def) => (isObj(defLive) && isObj(def)) - ? Object.assign(Object.create(defLive), def) : def + ? Object.assign(Object.create(defLive), defLive, def) : def const esmockModuleApply = (defLive, def, fileURL) => { def = Object.assign({}, defLive || {}, { From 1c1ef9cae614ef1d54dee562289e158cb37cbd8e Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 28 Nov 2022 19:37:45 -0800 Subject: [PATCH 2/7] added node 19 to test ci --- .github/workflows/node.js.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index f26229b1..b67b04ae 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -18,7 +18,7 @@ jobs: # node-14's npm must be updated, but no longer updatable due to: # https://github.com/npm/cli/issues/2663 # because of this, node-14 test strategy is removed :( - node-version: [16.x, 18.x] + node-version: [16.x, 18.x, 19.x] os: [ubuntu-latest, windows-latest] steps: - uses: actions/checkout@v3 From cd5a7f3a3c0310ff0f8d04085427cf6a52809fe8 Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 28 Nov 2022 19:38:51 -0800 Subject: [PATCH 3/7] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb66f981..dbd9a023 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # changelog * 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 * 2.0.9 _Nov.26.2022_ * [resolve windows modules with correct drive letter](https://github.com/iambumblehead/esmock/pull/189) using [resolvewithplus patch](https://github.com/iambumblehead/resolvewithplus/pull/42) from @mshima From e822e03dca01edd18405e189b523887f1ed85eee Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 28 Nov 2022 19:40:20 -0800 Subject: [PATCH 4/7] change position of enumerable object def --- src/esmockModule.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esmockModule.js b/src/esmockModule.js index a42d1bfa..dae0aff0 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -20,7 +20,7 @@ const asFileURL = p => p.startsWith('file://') ? p : url.pathToFileURL(p) const esmockModuleMergeDefault = (defLive, def) => (isObj(defLive) && isObj(def)) - ? Object.assign(Object.create(defLive), defLive, def) : def + ? Object.assign(Object.create(defLive, defLive), def) : def const esmockModuleApply = (defLive, def, fileURL) => { def = Object.assign({}, defLive || {}, { From 0aa0279360df3666d3518cef1f735cfe103ae6ed Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 28 Nov 2022 19:44:17 -0800 Subject: [PATCH 5/7] change position of enumerable object def --- src/esmockModule.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esmockModule.js b/src/esmockModule.js index dae0aff0..a42d1bfa 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -20,7 +20,7 @@ const asFileURL = p => p.startsWith('file://') ? p : url.pathToFileURL(p) const esmockModuleMergeDefault = (defLive, def) => (isObj(defLive) && isObj(def)) - ? Object.assign(Object.create(defLive, defLive), def) : def + ? Object.assign(Object.create(defLive), defLive, def) : def const esmockModuleApply = (defLive, def, fileURL) => { def = Object.assign({}, defLive || {}, { From e822fc0076528001ebc3241ff184769aef7a3ee7 Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 28 Nov 2022 21:11:02 -0800 Subject: [PATCH 6/7] copy keys of object, if not a plain object --- src/esmockModule.js | 10 ++++++++-- tests/tests-mocha/src/app.js | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/esmockModule.js b/src/esmockModule.js index a42d1bfa..31751124 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -17,10 +17,16 @@ const isDirPathRe = /^\.?\.?([a-zA-Z]:)?(\/|\\)/ const isMetaResolve = typeof import.meta.resolve === 'function' const nextId = ((id = 0) => () => ++id)() const asFileURL = p => p.startsWith('file://') ? p : url.pathToFileURL(p) +const objProto = Object.getPrototypeOf({}) +const isPlainObj = o => Object.getPrototypeOf(o) === objProto + +const esmockModuleMerge = (defLive, def) => isPlainObj(defLive) + ? Object.assign({}, defLive, def) + : Object.assign(Object.create(defLive), Object.keys(defLive).reduce( + (prev, k) => (prev[k] = defLive[k], prev), {}), def) const esmockModuleMergeDefault = (defLive, def) => - (isObj(defLive) && isObj(def)) - ? Object.assign(Object.create(defLive), defLive, def) : def + (isObj(defLive) && isObj(def)) ? esmockModuleMerge(defLive, def) : def const esmockModuleApply = (defLive, def, fileURL) => { def = Object.assign({}, defLive || {}, { diff --git a/tests/tests-mocha/src/app.js b/tests/tests-mocha/src/app.js index 6ca8ed3e..b10b3922 100644 --- a/tests/tests-mocha/src/app.js +++ b/tests/tests-mocha/src/app.js @@ -11,6 +11,10 @@ if (typeof passport.initialize !== 'function') { throw new Error('inavalid mock') } +if (!('_key' in passport)) { + throw new Error('inavalid mock, enumerable props not discoverable') +} + app.get('/', (req, res) => { res.send('Hello World!') }) From aa79a920a723af9e4900e6a2f01a6ac961b80280 Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 28 Nov 2022 21:18:39 -0800 Subject: [PATCH 7/7] added comments --- src/esmockModule.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/esmockModule.js b/src/esmockModule.js index 31751124..8763b254 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -20,10 +20,14 @@ const asFileURL = p => p.startsWith('file://') ? p : url.pathToFileURL(p) const objProto = Object.getPrototypeOf({}) const isPlainObj = o => Object.getPrototypeOf(o) === objProto +// assigning the object to its own prototypal inheritor can error, eg +// 'Cannot assign to read only property \'F_OK\' of object \'#\'' +// +// if not plain obj, assign enumerable vals only. core modules === plain obj const esmockModuleMerge = (defLive, def) => isPlainObj(defLive) ? Object.assign({}, defLive, def) - : Object.assign(Object.create(defLive), Object.keys(defLive).reduce( - (prev, k) => (prev[k] = defLive[k], prev), {}), def) + : Object.assign(Object.keys(defLive).reduce( + (prev, k) => (prev[k] = defLive[k], prev), Object.create(defLive)), def) const esmockModuleMergeDefault = (defLive, def) => (isObj(defLive) && isObj(def)) ? esmockModuleMerge(defLive, def) : def