News for hackers. Stuff that matters.
Re:JavaScript fundamentals before learning React by sorahn on Thursday July 19, 2018 @10:24PM (#17571977)
Given this super contrived example: https://gist.github.com/sorahn/2b287c2a97a12e318585e84d9a9e8...

If 'baz' was a fat arrow function, you would never be able to test bar in isolation.

for example: Jest is set up so it's super easy to say `jest.spy(Foo.prototype, 'baz').mockReturnValue('whatever')`and test that bar is doing the right thing.

Re:JavaScript fundamentals before learning React by Touche on Thursday July 19, 2018 @10:35PM (#17572026)
Mocking should only be used for extreme circumstances. This class is very easy to test without mocking. `bar` takes no arguments and has no external dependencies and therefore should always return the same result.
Re:JavaScript fundamentals before learning React by sorahn on Thursday July 19, 2018 @10:42PM (#17572054)
OK, you caught me, give bar an argument. (I'll edit the gist)

Why should mocking only be used in 'extreme circumstances'? I want to test what bar does, and I don't care what baz does, and if someone breaks baz, my unit tests for bar shouldn't fail, because it is doing its job.

I would mock it if it was calling some function in another module, so what's the difference if it's calling another function in the class?

Re:JavaScript fundamentals before learning React by yorwba on Friday July 20, 2018 @12:00AM (#17572347)
Mocking a component means that you now have two places where that component's behavior is specified and they can diverge. To prevent that, you'll need an integration test where the components interact directly. Just not using a mock is enough to get such an integration test.

Then the value of the original, mocked unit test is questionable. It only provides additional information in the event that the mock differs from the actual component. If that's unintentional, then either the component is wrong (which should be caught by the tests for that component) or the mock is wrong. In either case, the mocked test provides little or negative value.

Then the remaining case, where mocking is actually useful, is when the mock intentionally shows different behavior. Mocking a slow computation to return the result instantly. Deliberately failing, to test error-handling code. Simulating unlikely events in general. Those are good uses of mocking.

TL;DR: Write more integration tests instead of unit tests with mocking.

Re:JavaScript fundamentals before learning React by sorahn on Friday July 20, 2018 @12:23AM (#17572439)
> TL;DR: Write more integration tests instead of unit tests with mocking.

Interesting. I will investigate what that looks like at work tomorrow. Thanks!