在开玩笑测试中将公共数据放在哪里 [英] 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__
下?
Or put them under __mocks__
anyway?
或者在__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.)
甚至 Jest 本身也显示出这些迹象.例如,测试匹配器将在 __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屋!