Search code examples
typescriptunit-testingtestingautomated-teststdd

Why do we write an extra test folder for programming languages that can also run tests in the src folder?


I was wondering why in many typescript tutorials where it was also about testing there was always an extra test folder created?

Isn't it easier to write the test directly next to the class e.g. like this: (Not possible in every language, but I mean this example only for the languages where it is possible. e.g. typescript)

src/folder/
 Class.*
 Class.test.*

It's even easier for big projects, because you don't have to scroll down so far in the IDE to get to the test, but have it right next to it.

But I see very often that a test folder is always created extra like this:

src/folder/
 Class.*
test/folder/
 Class.test.*

(In the end, everyone can decide for himself how he wants to design his project, but I can't get the question out of my head)

If possible, write about how you organize your tests, whether you are satisfied with the test organization and whether you were able to decide for yourself how you want to organize your tests.


Solution

  • Keeping tests and production separate allows you to compile and deploy them separately, among other things. You really don't want to mix production code with test code. Being able to compile the production code separately ensures that it doesn't depend on any of the test code. Being able to deploy your production code separately is essential; test code is just for the test and should not be available in production for many reasons. For example: test code calls internal api. Deploying this test code, exposes the internal api.