regex - 创建 React App 找不到测试

标签 regex reactjs testing jestjs create-react-app

我最近开始了一个使用 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 to import 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/

相关文章:

java - 使用 Java 正则表达式进行单词提取和拆分

Javascript 匹配和正则表达式

javascript - React onClick 是否更像一个 JavaScript addEventListener 而不是一个标准的 JavaScript onclick 属性?

css - Material-UI 对话框覆盖?

javascript - 自动化测试中有多少断言太多?

java - Selenium Java,用于跨测试运行相同设置的类

c# - 如何使用 C# 编写自动化系统测试?

java - 使用扫描仪和 useDelimiter/右正则表达式解析文本

javascript - 从网站提取元数据

javascript - 发布版本失败(链接器,___gxx_personality_v0")