Amy Blankenship
1 min readMay 1, 2023

--

Since you intend for your function to always call its internal getContext (or other function it knows where to get), you're not making it more testable doing this. You're just making your tests more misleading. Instead, you should just load up the context in the normal way that you would do when using your module, and if that's hard, _there's_ your problem.

I see the testability problem in your function in a very different area--you have to mock the DOM in order to test it, and your code needs to know the DOM exists and reference it. If I were refactoring your code for testability, I'd provide the _DOM element_ by DI, not the function. Very likely I'd also just inject the result of calling the function vs. the function itself. You wouldn't even need that function if you just stored the timestamp formatted once, rather than formatting it every time it's accessed.

--

--

Amy Blankenship
Amy Blankenship

Written by Amy Blankenship

Full Stack developer at fintech company. I mainly write about React, Javascript, Typescript, and testing.

Responses (1)