marchaos / jest-mock-extended

Type safe mocking extensions for Jest https://www.npmjs.com/package/jest-mock-extended
MIT License
828 stars 57 forks source link

MockImplementations are mocked wihout using deepMock #68

Open s-montigny-desautels opened 3 years ago

s-montigny-desautels commented 3 years ago

Hi!

When passing a mock implementations to mock, every sub object get wrapped in a Proxy. For example,

class ObjectWithMap {
  map = new Map();
}

const test = mock({ objectWithMap: new ObjectWithMap() });
console.log(util.types.isProxy(test)); // -> true
console.log(util.types.isProxy(test.objectWithMap)); // -> true
console.log(util.types.isProxy(test.objectWithMap.map)); // -> true

Which I think is quite unexpected. I believe it should only happen when using the deepMock function. Also, it break some built-in object (Map, Set, Date, etc) as seen in #59.

Is it the expected behavior?

mikolajgrzyb commented 1 year ago

I have the same issue when trying to work with rxjs. This method seems to be the problem

abdatta commented 4 months ago

This is a major issue for all my use cases, which has circular references which lead to infinite loop when I use with mock

abdatta commented 4 months ago

Found a way out to prevent unintentional deep mocks. You can put the mock implementation inside Object.create function, which makes the keys non enumerable, thus the mock function won't be able to iterate its keys and do deep mocks.