You could also potentially use a Proxy object:

https://gist.github.com/hildjj/1ac6e3d52e4e0d23f6289d73c1840a5a

> On Nov 20, 2017, at 9:00 AM, Richard Newman <rnew...@mozilla.com> wrote:
> 
> Are there alternative ways we could achieve the same without the (or with 
> low) complexity/overhead?
> 
> 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`.
> 
> 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).
> 
> 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_.
> 
> 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.
> _______________________________________________
> firefox-dev mailing list
> firefox-...@mozilla.org
> https://mail.mozilla.org/listinfo/firefox-dev

— 
Joe Hildebrand

_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to