Home > Software design >  React app cannot find my tests "No tests found"
React app cannot find my tests "No tests found"

Time:11-21

I am new to the react (Jest) tests and I wanted to start practice but for some reason I get an error that there are no tests found.

package.json:

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "5.0.1",
    "socket.io-client": "^4.5.2",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --watchAll",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

my test

import {render, screen, cleanup} from '@testing-library/react';
import Home from '../src/Home.js';

test('test', () => {
    expect(true).toBe(true);
})

the test is located in another folder called testing inside the src folder (where the original jsx file is). I am trying to run it with npm test but it never finds any tests. The main file is Home.js and the test file is called Home.test.js

CodePudding user response:

Hope this help

From create-react-app docs:

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.

CodePudding user response:

You need to name your file with mytest.test.jsx

  • Related