在哪里将公共数据放入笑话测试中 [英] Where to put common data in jest tests

查看:48
本文介绍了在哪里将公共数据放入笑话测试中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何组织代码进行笑话测试.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆