what should i unit test and what shouldn't I?
Hi, a dev new to unit testing here! I'm wondering if, in a typescript project, should i test functions against runtime or not.
example:
should i test that functions throw errors if arguments do not match what is typed? for me, i think that it isnt useful, since as the project has typescript, therefore a type checking script when compiling, those kind of errors will appear there if not shown by the linter before.
what do you think? what is the "standard" for this? would love some advice!
5 Replies
oh interesting question, i would test cases when it would go wrong
i have no idea why it has sent this sticker 💀
Definitely not worth overtesting from the start. General type safety should not be tested via unit tests as you can verify it with tsc. The only exception that comes to mind is when type safety is not guaranteed (e.g. data comes from third party server and/or is typed as
any
).
In general, I test most common positive/negative outcomes to guarantee, that no refactoring will break any user-facing functionalities. You can assume some target code coverage if you work with more people to have some rule of thumb, but don't enforce anything larger than 80% as you can overtest and overwhelm team with edge-case testing.
If it happens, that a new scenario comes up (e.g. as a bug report), that can be added to tests, you can then add it to the test suite if it feels like an important case.
Btw, there is no standard for this 😉 Anyone will have different opinion, so in the end you should choose whatever is comfortable for you and your team. Not unit testing at all is as valid strategy if you have other means to cover testing scenarios from time to time.HAHAHAH
yes, i agree with you. it is time taking to test that much, and it feels useless since there is type checking with tsc. ty < 3
it is probably because your first language wasnt strongly type one.
take Java.
public void math(Integer num);
when you would write calling line of that method it will display error if you pass String
just like with typescript, which was one the purpose of typescript
true