Theo's Typesafe CultTTC
Theo's Typesafe Cult4y ago
1 reply
Grifn

Testing with Typescript

When writing tests for a Typescript project, should functions still be written with fail conditions in mind so they can be tested properly? Or are those tests pointless because the project is using TS?

Example: Testing a getFirstDayOfWeek function takes a Date as an argument. Of course if a string is provided, the function will fail when it tries to getDay() on the string. Should I check if the date is an instanceof Date and return undefined if not? This can then be tested by providing something other than a Date.

I don't know how other developers on the (large) team will interact with the code I'm writing and thinking I should add the fail condition code, but it means a lot of the other code that uses this getFirstDayOfWeek function needs to suffer by expecting either a Date or undefined as a result of the function.

Or in this specific case, if a Date isn't provided should I just use today's date and always return a Date.
Was this page helpful?