问题
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 toimport 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