Should unit tests be written in typescript?

My team is currently migrating from javascript to typescript. Should we migrate our unit tests from javascript to typescript as well?

My thinking is that there is not a huge benefit of having our unit tests in typescript, and that compiling our unit tests from typescript to javascript before they are run is a waste of time.

For context, we are using Jest and React Testing Library for a large React project.

1 answer

  • answered 2021-01-13 19:29 TLadd

    Not a right or wrong answer here. I personally like using TypeScript for my tests for similar reasons I like using TypeScript in my app code. It provides a nice dev environment and quicker feedback when something is broken. For example, if I change the arguments a function takes, I know quickly which tests need to be changed. The downsides are you spend a bit more time, particularly in the early goings, figuring out how to make TypeScript happy (especially for things like mocked functions).

    And yeah, something has to remove the types, either ts-jest or babel-jest, when actually executing the tests. You'll already have to do this with your app code anyways though (which presumably is a fair amount larger than the tests), so I don't think that's much of a concern. It's certainly not something I think about anyways.