Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use live object to assign enumerable properties #190

Merged
merged 7 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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