Style Guide: Where to put your test files

Posted on

During my time working with different projects, I have seen different approaches to organize test files. Some projects put test files in the same folder as the source code, some projects put test files in a separate folder. I'll put my thoughts on both approaches below.

Put test files in the same folder

|-- src
| |-- components
| |-- Button
| |-- Button.tsx
| |-- Button.test.tsx

I notice some benefits of this approach

I truly like this approach ❤️. The idea is to keep related files near each other.

Looking at some existing Frontend frameworks, only Angular that explicitly recommends this approach. See Angular Testing Guide.

Put test files in a separate folder

|-- src
| |-- components
| |-- Button
| |-- Button.tsx
|-- tests
| |-- components
| |-- Button
| |-- Button.test.tsx

It actually has many disadvantages

Summary

I prefer to put test files in the same folder as the source code. It is easier to find, easier to import, and easier to refactor.