Comment by apothegm
11 days ago
Unit tests are super useful when first writing a function or class to confirm it does what you think it does.
Then throw away the unit tests and write integration or E2E tests instead. Then you can refactor under the hood while ensuring overall system behavior is as expected.
There are some exceptions where you might want to hang on to a small subset of unit tests. They can be useful for demonstrating how to use an interface or class. They can help support particularly complex bits of logic. If a certain part of your codebase is fragile and regression prone, unit test coverage can help.
Otherwise, they just calcify the code.
Your unit tests shouldn’t break from a refactor, unless you’re testing the implementation instead of the behavior, which would be dumb.
A refactor often changes boundaries. Which breaks unit tests. Its integration tests that are truly agnostic to implementation.