在哪里将公共数据放入笑话测试中 [英] Where to put common data in jest tests
问题描述
我不确定如何组织代码进行笑话测试.
I am not sure how to organize code for jest testing.
我在__tests__
下进行了所有测试,在__mocks__
下进行了所有模拟测试.现在,我想在测试之间共享一些数据:它们不是现有功能的模拟,它们只是我想在不同文件中使用的一些javascript对象.
I have all my tests under __tests__
and all my mocks under __mocks__
. Now I have some data I want to share between tests: they are not a mock of an existing function, they are just some javascript object I'd like to use in different files.
我应该创建一个__data__
目录吗?
Should I create a __data__
directory?
还是将它们放在__mocks__
下?
还是在__tests__
目录中而不将-test
放在文件名中?
Or in the __tests__
directory without putting -test
in the file name?
推荐答案
简短的答案在您想要的任何地方.
The short answer is anywhere you want.
JavaScript的生命经历了许多不同的阶段,使用它的人也很多.这可能就是为什么如今大多数工具都是高度可配置的,以允许进行个性化设置(自定义)的原因.
JavaScript has had a lot of different stages in its life and a lot of different types of people using it. Which is probably why most tools these days are highly configurable, to allow for personalization (customization.)
甚至开玩笑本身也显示出这些迹象.例如,测试匹配器将在__tests__
文件夹中或包含.spec
或.test
的文件中查找测试.
Even Jest itself shows these signs. For instance the test matcher will look for tests in either __tests__
folders or with files that are contain .spec
or .test
.
或按照他们的文档以视觉方式:
Or as per their docs in a visual manner:
├── __tests__
│ └── component.spec.js # test
│ └── anything # test
├── package.json # not test
├── foo.test.js # test
├── bar.spec.jsx # test
└── component.js # not test
对于夹具和其他测试文件,答案是相同的,没有一种方法可以实现.
With regards to fixtures and other test files the answer is the same, there is no one way to do it.
选择最适合您的东西.
对于__tests__
结构,我建议将夹具数据放置在使用它的测试附近,如果需要访问多个测试,则将其进一步移动到项目中,直到其位于同一位置.
I recommend for the __tests__
structure placing fixture data close to the test thats using it, and if multiple tests need access then move it further up the project until its in a common place.
我的首选项是tests
文件夹,用于将测试,固定装置和测试与src
代码分开.
My preference is a tests
folder to keep the tests, fixtures, and testing separate to the src
code.
这篇关于在哪里将公共数据放入笑话测试中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!