Hacker News new | ask | show | jobs
by henryaj 1613 days ago
What's wrong with using `let`?
2 comments

I personally am ambivalent about it, but the argument against let is generally about keeping as much of the context of your tests inside it. The error message of the cop in TFA alludes to this when it recommends using the four phase pattern (setup/exercise/verify/teardown). That way, you can almost look at a test in isolation and understand everything about it, which may not be true for a complex let.
Using `let` carries the risk of increasing the cognitive load required to understand why a test is failing or passing if it's not used carefully.

- they bring example execution order into play, especially with nested contexts and nested `let`s shadowing other above.

- they invite DRYing up test code, making it really easy to couple unrelated tests together and hard to understand tests in isolation.

- the corollary to the above is creating a brittle test suite. (in any case if DRY is a footgun in production code, it's doubly so in test code.)

- they require you to divert your attention from the examples to see what the states of your test objects are going to be.

These pitfalls can be avoided if, for example, you favor building up your test object graphs inside your examples.

(@r-s That's not the same thing though, they're talking about the eagerly evaluated version of `let`.)