【发布时间】:2018-10-28 22:02:55
【问题描述】:
我在各种 Jest 测试中使用了一些 utils 函数,例如这样的函数,用于模拟获取响应:
export const mockFetchJsonResponse = (data) => {
ok: () => true,
json: () => data
};
我想以一种可以导入它们并在我的测试中重复使用的方式共享这些函数。例如:
// Some .spec.jsx file
// ...
import {mockFetchJsonResponse} from 'some/path/to/shared/tests/utils.jsx'
// Then I can use mockFetchJsonResponse inside this test
// ...
这些常用的utils函数应该放在哪里?
我的项目文件夹如下所示:
components/
CompOne/
__tests__
index.jsx
CompTwo/
__tests__
...
utils/
__tests__
http.js
user.js
...
我应该将它们与我在项目中使用的其他 utils 函数一起放在 utils 文件夹中吗?那么我是否也应该为这些功能编写单元测试?
【问题讨论】:
标签: javascript reactjs jestjs