我有一些我在各种Jest测试中使用的utils函数,例如像这样的函数,用于模拟一个fetch响应:
export const mockFetchJsonResponse = (data) => {
ok: () => true,
json: () => data
};
Run Code Online (Sandbox Code Playgroud)
我希望以我可以导入它们并在我的测试中重用的方式共享这些功能.例如:
// Some .spec.jsx file
...
import {mockFetchJsonResponse} from some/path/to/shared/tests/utils.jsx
// Then I can use mockFetchJsonResponse inside this test
// ...
Run Code Online (Sandbox Code Playgroud)
我应该在哪里放置这样的常用工具功能?
我的项目文件夹如下所示:
components/
CompOne/
__tests__
index.jsx
CompTwo/
__tests__
...
utils/
__tests__
http.js
user.js
...
Run Code Online (Sandbox Code Playgroud)
我应该将它们utils
与我用于项目的其他utils函数一起放在文件夹中吗?那么我应该为这些功能编写单元测试吗?