Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 590
WHITECOLOR Avatar asked Sep 26 '12 05:09

WHITECOLOR


People also ask

Where do I put test files in JavaScript?

Where to put test files. Unit tests run against specific lines of code. So it makes sense to place them right next to that code. Integration tests run against many lines of code in many files.

What is Spec js file in NodeJS?

Use of spec. js is for writing you unit test cases for your angular application. We write test cases in angular using Jasmine & Karma. Jasmine is a Behavior Driven Development testing framework for JavaScript. It does not rely on browsers, DOM, or any JavaScript framework.

Can you write a NodeJS test without an external library?

js unique: you can write applications in Node. js without the use of external libraries.


1 Answers

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 :)

like image 172
Bill Avatar answered Nov 07 '22 12:11

Bill