Jest: tests only succeed once. No tests found; Jest, Babel, lint-staged, husky.

Created on 21 Jul 2020  ·  6Comments  ·  Source: facebook/jest

🐛 Bug Report

When ran via cli (not husky precommit hook), npm run test will succeed. Also, when running the precommit git hook after initially creating or renaming a test file, npm run test will succeed. However, I'm failing after rerunning the git commit hook with a previously committed test.

➜ git commit -m "test"         
husky > pre-commit (node v14.2.0)
✔ Preparing...
⚠ Running tasks...
  ❯ Running tasks for ./src/**/*.{js,jsx,ts,tsx}
    ✖ npm run test [FAILED]
↓ Skipped because of errors from tasks. [SKIPPED]
✔ Reverting to original state because of errors...
✔ Cleaning up... 

✖ npm run test:
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test: `jest --notify --coverage "/Users/alane/Documents/projects/bots/project/src/index.js"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/alane/.npm/_logs/2020-07-21T02_00_31_704Z-debug.log

> [email protected] test /Users/alane/Documents/projects/bots/project
> jest --notify --coverage "/Users/alane/Documents/projects/bots/project/src/index.js"

No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In /Users/alane/Documents/projects/bots/project
  48 files checked.
  testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 1 match
  testPathIgnorePatterns: /node_modules/ - 48 matches
  testRegex:  - 0 matches
Pattern: /Users/alane/Documents/projects/bots/project/src/index.js - 0 matches
husky > pre-commit hook failed (add --no-verify to bypass)

To Reproduce

package.json

"scripts": {
    "start": "pm2 start processes.json --only prod",
    "dev": "pm2 start processes.json --only dev",
    "devmon": "nodemon --exec 'babel-node --inspect=0.0.0.0 src/index.js'",
    "config": "nodemon configure.sh",
    "stop": "pm2 delete processes.json",
    "prettier": "prettier --write ./src/**/*",
    "lint": "eslint src/**/*.js --fix --color",
    "build": "rm -rf build && babel ./src --out-dir ./build",
    "watch:test": "jest --watch",
    "test": "jest --notify --coverage"
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "./src/**/*.{js,jsx,ts,tsx}": [
      "npm run prettier",
      "npm run lint",
      "npm run build",
      "npm run test"
    ]
  },
  "jest": {
    "testEnvironment": "node",
    "collectCoverage": true,
    "coverageDirectory": "coverage",
    "collectCoverageFrom": [
      "src/**/*.js"
    ],
    "coveragePathIgnorePatterns": [
      "/node_modules/"
    ]
  },
  "babel": {
    "only": [
      "src"
    ],
    "env": {
      "test": {
        "presets": [
          "@babel/preset-env"
        ],
        "plugins": [
          "@babel/plugin-proposal-optional-chaining",
          "@babel/plugin-transform-runtime",
          "@babel/plugin-transform-modules-commonjs",
          "@babel/plugin-proposal-class-properties"
        ],
        "sourceMaps": "inline",
        "retainLines": true
      },
      "production": {
        "presets": [
          "@babel/preset-env"
        ],
        "plugins": [
          "@babel/plugin-proposal-optional-chaining",
          "@babel/plugin-transform-runtime",
          "@babel/plugin-transform-modules-commonjs",
          "@babel/plugin-proposal-class-properties"
        ]
      },
      "development": {
        "presets": [
          "@babel/preset-env"
        ],
        "plugins": [
          "@babel/plugin-proposal-optional-chaining",
          "@babel/plugin-transform-runtime",
          "@babel/plugin-transform-modules-commonjs",
          "@babel/plugin-proposal-class-properties"
        ],
        "sourceMaps": "inline",
        "retainLines": true
      }
    }
  }

Expected behavior

Previously committed tests would be found, ran, and succeed.

envinfo

MacOSX, also Ubuntu 18.04 Docker image

  System:
    OS: macOS 10.15.5
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 14.2.0 - /usr/local/bin/node
    npm: 6.14.5 - /usr/local/bin/npm
  npmPackages:
    jest: ^26.1.0 => 26.1.0 
Bug Report Needs Repro Needs Triage

All 6 comments

I'm also seeing this, reverting to jest@25 fixes it

it does succeed the first time because the tests are recognized as 'new'. My 'Test matches' show three, but the 'pattern' shows 0 matches. I have to use "jest --coverage --findRelatedTests --bail", and it succeeds. Is that absolutely necessary? At least for now, I would like to run all tests every commit, regardless of which staged changes are relevant to which test.

lint-staged passes the names of the files that have changed, so jest is called like jest file-1.js file-2.js (or /Users/alane/Documents/projects/bots/project/src/index.js in your specific case).

I guess we could make --all ignore any passed testPathPattern? That way you could stick jest --all into your lint-staged config.

If you _don't_ wanna run all files but only related tests you can do jest --findRelatedTests

/cc @thymikee @jeysal thoughts?

I'm not in favor of adding the --all flag. It's a rare use case to run all files when a list is passed by another tool and would further clutter the config.

I would like to run all tests every commit, regardless of which staged changes are relevant to which test

In such case, Jest could be run from a simple Node.js script which would prevent passing unwanted arguments to Jest.

In such case, Jest could be run from a simple Node.js script which would prevent passing unwanted arguments to Jest.

can you give an example of how I could locally run all jest tests with your method before a commit

Same issue here

✖ npm run test:unit:
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test:unit: `vue-cli-service test:unit "/Users/cdl/g/projects/missionbio/falcon-ui/package.json" "/Users/cdl/g/projects/missionbio/falcon-ui/src/App.vue" "/Users/cdl/g/projects/missionbio/falcon-ui/src/components/FilesTable.vue" "/Users/cdl/g/projects/missionbio/falcon-ui/src/components/RunsTable.vue" "/Users/cdl/g/projects/missionbio/falcon-ui/src/store/modules/session/actions.ts"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test:unit script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/cdl/.npm/_logs/2020-10-22T04_03_26_016Z-debug.log

> [email protected] test:unit /Users/cdl/g/projects/missionbio/falcon-ui
> vue-cli-service test:unit "/Users/cdl/g/projects/missionbio/falcon-ui/package.json" "/Users/cdl/g/projects/missionbio/falcon-ui/src/App.vue" "/Users/cdl/g/projects/missionbio/falcon-ui/src/components/FilesTable.vue" "/Users/cdl/g/projects/missionbio/falcon-ui/src/components/RunsTable.vue" "/Users/cdl/g/projects/missionbio/falcon-ui/src/store/modules/session/actions.ts"

No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In /Users/cdl/g/projects/missionbio/falcon-ui
  101 files checked.
  testMatch: **/tests/unit/**/*.spec.[jt]s?(x), **/__tests__/*.[jt]s?(x) - 8 matches
  testPathIgnorePatterns: /node_modules/ - 101 matches
  testRegex:  - 0 matches
Pattern: /Users/cdl/g/projects/missionbio/falcon-ui/package.json|/Users/cdl/g/projects/missionbio/falcon-ui/src/App.vue|/Users/cdl/g/projects/missionbio/falcon-ui/src/components/FilesTable.vue|/Users/cdl/g/projects/missionbio/falcon-ui/src/components/RunsTable.vue|/Users/cdl/g/projects/missionbio/falcon-ui/src/store/modules/session/actions.ts - 0 matches
husky > pre-commit hook failed (add --no-verify to bypass)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

vitalibozhko picture vitalibozhko  ·  138Comments

SimenB picture SimenB  ·  69Comments

benmonro picture benmonro  ·  119Comments

paularmstrong picture paularmstrong  ·  66Comments

timoxley picture timoxley  ·  76Comments