Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create React App cannot find 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
...

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?

like image 270
Z_z_Z Avatar asked Apr 01 '18 20:04

Z_z_Z


1 Answers

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

like image 198
The Reason Avatar answered Oct 22 '22 20:10

The Reason