创建React App找不到测试 [英] Create React App cannot find tests

查看:98
本文介绍了创建React App找不到测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近使用create-react-app启动了一个新项目。我将 App.test.js / src 文件夹外部移到了根目录 / tests 文件夹,所以我的文件夹结构现在看起来像这样:

I recently started a new project using create-react-app. I moved the App.test.js from outside the /src folder into a root level /tests folder so my folder structure looks like this now:

> node_modules
> public
> src
   ...
   App.js
> tests
   App.test.js
...

这就是全部App.test.js文件:

And here's the entire App.test.js file:

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运行测试时,没有执行测试。它说找不到测试。如何获得create-react-app将新的 / tests 文件夹识别为所有测试的新位置并运行它们?

So when I try npm run test, no tests get executed. It says that no tests can be found. How can I get create-react-app to recognize the new /tests folder as the new location for all tests and run them?

推荐答案

来自 create-react-app 文档:


文件名约定
Jest将使用以下任何流行的命名约定查找测试文件:

Filename Conventions Jest will look for test files with any of the following popular naming conventions:

__ tests __ 文件夹中带有.js后缀的文件。带.test.js
后缀的文件。带.spec.js后缀的文件。 .test.js / .spec.js文件(或
__ tests __ 文件夹)可以位于src顶部
级别文件夹下的任何深度。

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.

我们建议将测试文件(或 __ tests __ 文件夹)放在
代码旁边正在测试,以便相对进口显得更短。以
为例,如果App.test.js和App.js位于同一文件夹中,则测试
只需导入'./App'而不是较长的相对路径。
托管还可以帮助在较大的项目中更快地找到测试。

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.

希望它可以为您提供帮助

Hope it helps you

这篇关于创建React App找不到测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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