Create React App cannot find tests

為{幸葍}努か 提交于 2020-02-24 04:18:45

问题


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
...

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);
});

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?


回答1:


From create-react-app docs:

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.

Hope it helps you



来源:https://stackoverflow.com/questions/49602402/create-react-app-cannot-find-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!