WHITECOLOR
WHITECOLOR

Reputation: 26140

JavaScript unit tests: where to keep test/specs files (Client and Node)

Is it ok to store test file in the location of module itself:

Lets say I have a structure:

project\
  module1\
    submodule\
       submodule.js
       submodule.test.js

Or it is better to make a separate location that will repeat the structure of the project:

project\
  module1\
    submodule\
       submodule.js
tests\
  module1\
    submodule\
       submodule.js

I've seen both variants. What considerations should be taken?

Upvotes: 10

Views: 4744

Answers (1)

Bill
Bill

Reputation: 25565

Projects pretty universally keep the tests in a separate location, but don't necessarily repeat the project structure. The test structure is usually flatter than the project structure.

You can see some examples of how people do it at:
https://github.com/visionmedia/express
https://github.com/learnboost/mongoose
https://github.com/mbostock/d3

As to why it is done this way, it makes executing the tests easier since they are all in one folder and makes packaging code easier since the source files and test files aren't intermixed. It also helps when doing things like code coverage builds since you need to instrument all of your source files but don't want to instrument your test code.

In general, whenever I have questions about project structure I just look at popular projects on GitHub and borrow liberally :)

Upvotes: 17

Related Questions