Search code examples
javascriptreactjsjestjshusky

React 18 with Husky pre-commit and jest - No tests found, exiting with code 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:

> [email protected] lint
> eslint -c .eslintrc.js ./src


> [email protected] 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.


Solution

  • 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"
    }