<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Are there alternative ways we could achieve the same without the (or with low) complexity/overhead?<br></blockquote><div><br></div><div>If I'm understanding correctly what you're trying to do, the typical suggestion here is to not use global singletons. That way you don't need to dig into the guts of a globally visible object in order to test it; you can pass your own part-mocked Object instance into whatever mechanism is trying to call `Object.foo`.</div><div><br></div><div>In your `foo`/`bar` case, you'd pass a `MockObject` to `bar`, and verify that `MockObject.foo` was called. We do this all the time in codebases that use less dynamic languages (e.g., Fennec and Firefox for iOS).<br></div><div><br></div><div>This doesn't help you to test existing singleton-based JS code⦠but then, if that code is already using Object.freeze, then you already can't, and you'll be having to change _something_.</div><div><br></div><div>I mostly agree with Nicolas's sentiment; poking at the guts of code outside your own module (or even in your own module!) isn't really a kind of software development that I feel we should encourage. If we find that gut-poking is the only good way to write tests for a component, then I would rather we revisit the design of the component instead of making it mutable. Refactoring for testability is A-OK in my book.<br></div></div></div></div>