Is there a way to solve any jest package issues?

Viewed 102

I have a react.js project and have just gotten started with testing. I installed the react-testing-library and jest to get started with it.

However, when I run

npm test

I get the following error.

/Users/Tomascdmota/Downloads/Trust-Motores-main/node_modules/react-scripts/node_modules/jest-cli/build/cli/index.js:40
  execute(argv, argv.projects, result => {
  ^

TypeError: execute is not a function
    at Object.run (/Users/Tomascdmota/Downloads/Trust-Motores-main/node_modules/react-scripts/node_modules/jest-cli/build/cli/index.js:40:3)
    at Object.<anonymous> (/Users/Tomascdmota/Downloads/Trust-Motores-main/node_modules/react-scripts/scripts/test.js:104:6)
    at Module._compile (node:internal/modules/cjs/loader:1091:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1120:10)
    at Module.load (node:internal/modules/cjs/loader:971:32)
    at Function.Module._load (node:internal/modules/cjs/loader:812:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47

My jest version is 26.6.0 I haven't added anything.

I have deleted the node_modules, package-lock.json, yarn.lock and removed jest from the dependencies/DevDependencies. After that I ran yarn install.

I still get that same error. I have googled the error and looked it up here on SO but came short-handed.

Any helps is appreciated.

1 Answers

I have deleted the node_modules, package-lock.json, yarn.lock

If you had package-lock.json and yarn.lock files at the same time, it looks like at some point you probably used both yarn and npm to install packages, which can definitely cause conflicts.

In terms of this specific issue, I ran into the exact same problem and it was due to a package conflict with jest-cli. The solution in my case was to upgrade react-scripts, e.g. (pick one):

  • Yarn

    yarn add react-scripts
    
  • Or NPM

    npm i react-scripts@latest
    

Details

In case it helps, here are more details on my specific issue and how I resolved it. I got the same error and it looks like I had two different versions of jest-cli installed:

$ npm list jest-cli
├─┬ jest-enzyme@4.2.0
│ └─┬ jest@27.5.1
│   └── jest-cli@27.5.1
└─┬ react-scripts@1.1.0
  └─┬ jest@20.0.4
    └── jest-cli@20.0.4

In my case, the solution was to upgrade react-scripts:

npm i react-scripts@latest

It looks like the dependency conflict is now resolved:

$ npm list jest-cli
└─┬ jest-enzyme@4.1.1
  └─┬ jest@27.5.1
    └── jest-cli@27.5.1

In fact, it doesn't even show jest-cli as a dependency of react-scripts any more, because it's using the same version of jest as jest-enzyme now:

$ npm list jest
├─┬ jest-enzyme@4.1.1
│ └── jest@27.5.1
└─┬ react-scripts@5.0.0
  ├─┬ jest-watch-typeahead@1.0.0
  │ └── jest@27.5.1 deduped
  └── jest@27.5.1 deduped
Related