Skip to content

Commit

Permalink
Merge pull request #197 from iambumblehead/resolve-cannot-assign-read…
Browse files Browse the repository at this point in the history
…only-property

added failing test
  • Loading branch information
iambumblehead authored Apr 4, 2023
2 parents fbdbe12 + 2a857e5 commit 4425101
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
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.2.0",
"version": "2.2.1",
"license": "ISC",
"readmeFilename": "README.md",
"description": "provides native ESM import mocking for unit tests",
Expand Down
5 changes: 4 additions & 1 deletion src/esmockModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/local/usesNodeProcess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import nodeprocess from 'node:process'

export default nodeprocess
9 changes: 9 additions & 0 deletions tests/tests-node/esmock.node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 4425101

Please sign in to comment.