
How to Fix “Create React App cannot find tests” Issue
If you’re using create-react-app and have recently moved your test files to a new /tests folder, you might encounter an issue where running npm run test yields a “no tests found” error. This problem arises because the default configuration of create-react-app expects test files to be located in specific directories.
Filename Conventions
According to the create-react-app documentation, Jest (the testing framework used by default) looks for test files with specific naming conventions:
- Files with a
.jssuffix in__tests__folders - Files with a
.test.jssuffix - Files with a
.spec.jssuffix
The .test.js or .spec.js files (or the __tests__ folders) can be located at any depth under the src top level folder. To avoid confusion and improve code readability, it is recommended to place the test files or __tests__ folders in close proximity to the code they are testing.
For example, if your App.test.js and App.js files are in the same folder, the test file can simply import the app like this:
import App from './App'
This approach eliminates the need for long relative paths and helps locate tests more easily, especially in larger projects.
Configuring create-react-app
To resolve the issue of create-react-app not recognizing the new /tests folder as the location for your tests, you can make a few adjustments:
- Ensure that your test files adhere to one of the aforementioned naming conventions, such as
.test.js. - Place your test files in either the
__tests__folder or in a folder with one of the supported naming conventions. - If you prefer a non-standard location for your test files, you can update the configuration file,
package.json. Within thejestconfiguration, modify thetestMatchproperty to include the appropriate glob pattern for your new test file location.
Once you’ve made these adjustments, running npm run test should now successfully execute your tests located in the new /tests folder.
Conclusion
By following the recommended conventions and appropriately configuring create-react-app, you can overcome the issue of tests not being found when you move them to a different location. Taking advantage of the flexibility and power of create-react-app allows you to organize your code and tests efficiently, leading to smoother development and improved maintainability of your React applications.
reference :
https://stackoverflow.com/questions/49602402/create-react-app-cannot-find-tests
Read Another Article :