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

Issue

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?

Solution

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.

Answered By – MarvinJWendt

Answer Checked By – Marie Seifert (AngularFixing Admin)

Leave a Reply

Your email address will not be published.