简体   繁体   English

JavaScript单元测试:保存测试/规格文件的位置(客户端和节点)

[英]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? 应该考虑什么?

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/visionmedia/express
https://github.com/learnboost/mongoose https://github.com/learnboost/mongoose
https://github.com/mbostock/d3 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 :) 总的来说,每当我对项目结构有疑问时,我只需查看GitHub上的热门项目并自由借用:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM