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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 8df08937..dbd9a023 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # 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 * 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..8763b254 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -17,10 +17,20 @@ 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 + +// 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.keys(defLive).reduce( + (prev, k) => (prev[k] = defLive[k], prev), Object.create(defLive)), def) const esmockModuleMergeDefault = (defLive, def) => - (isObj(defLive) && isObj(def)) - ? Object.assign(Object.create(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!') })