我最近开始了一个使用 create-react-app 的新项目。我将 App.test.js
从 /src
文件夹移到根级 /tests
文件夹中,所以我的文件夹结构现在看起来像这样:
> node_modules
> public
> src
...
App.js
> tests
App.test.js
...
这是整个 App.test.js 文件:
import React from 'react';
import ReactDOM from 'react-dom';
import App from "../src/App";
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
ReactDOM.unmountComponentAtNode(div);
});
所以当我尝试 npm run test
时,没有执行任何测试。它说找不到测试。如何让 create-react-app 将新的 /tests
文件夹识别为所有测试的新位置并运行它们?
最佳答案
来自 create-react-app
文档:
Filename Conventions Jest will look for test files with any of the following popular naming conventions:
Files with .js suffix in
__tests__
folders. Files with .test.js suffix. Files with .spec.js suffix. The .test.js / .spec.js files (or the__tests__
folders) can be located at any depth under the src top level folder.We recommend to put the test files (or
__tests__
folders) next to the code they are testing so that relative imports appear shorter. For example, if App.test.js and App.js are in the same folder, the test just needs toimport App from './App'
instead of a long relative path. Colocation also helps find tests more quickly in larger projects.
希望对你有帮助
关于regex - 创建 React App 找不到测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49602402/