Skip to content

Commit

Permalink
Merge pull request #190 from iambumblehead/use-live-default-to-popula…
Browse files Browse the repository at this point in the history
…te-enumerable

use live object to assign enumerable properties
  • Loading branch information
iambumblehead authored Nov 29, 2022
2 parents c2ce03a + aa79a92 commit 8838e6a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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_
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.0.9",
"version": "2.1.0",
"license": "ISC",
"readmeFilename": "README.md",
"description": "provides native ESM import mocking for unit tests",
Expand Down
14 changes: 12 additions & 2 deletions src/esmockModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 \'#<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 || {}, {
Expand Down
4 changes: 4 additions & 0 deletions tests/tests-mocha/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!')
})
Expand Down

0 comments on commit 8838e6a

Please sign in to comment.