Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practices to add tests files in the tsconfig.json exclude field?

I often see "exclude": ["**/*.test.ts", "**/*.test.tsx"] in project tsconfig file.

Sometimes there is a tsconfig.json without this but another tsconfig.build.json which override this file and add this exclusion.

My questions are:

  • what are the pros and cons of doing so?
  • Is it considered as a good practice?
like image 360
p9f Avatar asked Oct 28 '25 15:10

p9f


2 Answers

What are the pros and cons of doing so?

  • Pros
    • The tests won't be compiled to JavaScript
    • Smaller build size
    • Smaller compile time
  • Cons
    • To run the tests, you need to have TypeScript installed
    • (And some testing library which supports TypeScript out of the box)

Is it considered as a good practice?

Depends on your use case. Smaller builds are good, but at what cost? Does your module need to be tested by everyone, or is a single test on the build enough?

Also, if you have something like ts-jest in your devDependencies, your tests will be run as pure TypeScript, so you wouldn't need to compile them.

like image 113
MarvinJWendt Avatar answered Oct 31 '25 06:10

MarvinJWendt


I agree with the first answer. Just wanted to comment (but don't have enough reputation yet to do it).

In case we want to exclude those from tsconfig.json and want to apply the quality standards to code anyway we can create another file, for instance, tsconfig.eslint.json and extend tsconfig there. Then pass an empty array to exclude object. Then link this file for eslint config to use.

Example of tsconfig.eslint.json:

{
  "extends": "./tsconfig",
  "exclude": []
}
like image 20
Natalia Bizitza Avatar answered Oct 31 '25 05:10

Natalia Bizitza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!