-1

After going down the rabbit-hole, this is as far as I seem to be able to get. I've got my husky pre-commit hook "working", but it's not finding tests when there clearly is a test, and it appears to be looking in the correct folder for it.

This is all cobbled together from searching and troubleshooting, just to get to the point where jest wouldn't simply hang and do nothing.

React 18 via create-react-app. Here's my package.json:

{
    "name": "my-web-app",
    "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",
        "web-vitals": "^2.1.4"
    },
    "jest": {
        "testMatch": [
            "<rootDir>/**/*.test.{js,jsx,ts,tsx}",
            "<rootDir>/src/**/*.test.{js,jsx,ts,tsx}",
            "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,ts,tsx}"
        ]
    },
    "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test --watchAll=false",
        "eject": "react-scripts eject",
        "lint": "eslint -c .eslintrc.js ./src",
        "lint:fix": "eslint -c .eslintrc.js ./src --fix",
        "prepare": "husky install"
    },
    "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"
        ]
    },
    "devDependencies": {
        "husky": "^8.0.3"
    }
}

In the root of my-web-app, I've got this:

enter image description here

Here's logging.test.js:

const TIMEOUT = (5 * 60) * 1000;

beforeAll(done => {
    done();
});
beforeEach((done) => {
    done();
});
afterEach((done) => {
    done();
});
afterAll(() => {
});

test("Fake test", async () => {
    expect(true).toBeTruthy();
}, TIMEOUT);

setupTests.test.js:

import "@testing-library/jest-dom";

pre-commit:

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run lint && npm run test

Here's the entirety of my output when trying to commit:

> my-web-app@0.1.0 lint
> eslint -c .eslintrc.js ./src


> my-web-app@0.1.0 test
> react-scripts test --watchAll=false

No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In /Users/me/projects/my-web-app
  3 files checked.
  testMatch: /Users/me/projects/my-web-app/**/*.test.{js,jsx,ts,tsx}, /Users/me/projects/my-web-app/src/**/*.test.{js,jsx,ts,tsx}, /Users/me/projects/my-web-app/src/**/?(*.)(spec|test).{js,jsx,ts,tsx} - 0 matches
  testPathIgnorePatterns: /node_modules/ - 3 matches
  testRegex:  - 0 matches
Pattern:  - 0 matches

How is testMatch: /Users/me/projects/my-web-app/**/*.test.{js,jsx,ts,tsx} not finding the fake test in my __tests__ folder? Even hard-coding the path yields the same result. I must be doing something dumb.

Tsar Bomba
  • 1,047
  • 6
  • 29
  • 52
  • Is this anything to do with Husky - do you get different results when you run `npm t` directly? Presumably the pattern's not quite right - why not restore Jest's default testMatch (see https://jestjs.io/docs/configuration#testmatch-arraystring), which would cover your files? – jonrsharpe Jul 22 '23 at 13:07
  • @jonrsharpe Default behavior causes a hang on commit, which led me to this: https://stackoverflow.com/questions/70718606/react-scripts-and-jest-no-tests-found-on-running-npm-test Then, upon doing the recommended, I was able to get jest to actually run properly, which led me to the current issue that I'm asking about. This is what I found and tried...which gave me the result above, and where I'm at: https://stackoverflow.com/a/69671746/1167996 – Tsar Bomba Jul 22 '23 at 16:28
  • I mean use _Jest's_ default values, as linked to, rather than CRA's (https://create-react-app.dev/docs/running-tests/#filename-conventions). – jonrsharpe Jul 22 '23 at 16:37
  • @jonrsharpe I got it. I removed the "jest" section I added to package.json and just moved the `__tests__` folder to /src, and it ran the test. I had to keep `--watchAll=false` in place on the npm command or it hangs w/ no tests found. – Tsar Bomba Jul 22 '23 at 16:40

1 Answers1

1

Got it. In the end, I just needed to move the __tests__ folder into the /src folder. This negated the need for the "jest" section in package.json. It will still hang with no tests found unless I use the --watchAll=false option on the npm command, like so:

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --watchAll=false",
    "eject": "react-scripts eject",
    "lint": "eslint -c .eslintrc.js ./src",
    "lint:fix": "eslint -c .eslintrc.js ./src --fix",
    "prepare": "husky install"
}
Tsar Bomba
  • 1,047
  • 6
  • 29
  • 52