jhildebrand at mozilla.com
Mon Nov 20 16:05:34 UTC 2017
You could also potentially use a Proxy object:
> On Nov 20, 2017, at 9:00 AM, Richard Newman <rnewman at 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-dev at mozilla.org
More information about the firefox-dev