Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

projects.map is not a function for jest-cli

I am writing a react app and testing it with jest. However, whenever I run the jest command from the terminal, I keep this error.

TypeError: projects.map is not a function
    at Object.<anonymous> (/Users/DavidHu/Desktop/coding/projects/swapnow/node_modules/jest-cli/build/cli/runCLI.js:172:28)
    at next (native)
    at step (/Users/DavidHu/Desktop/coding/projects/swapnow/node_modules/jest-cli/build/cli/runCLI.js:18:30)
    at /Users/DavidHu/Desktop/coding/projects/swapnow/node_modules/jest-cli/build/cli/runCLI.js:34:14
    at Object.<anonymous> (/Users/DavidHu/Desktop/coding/projects/swapnow/node_modules/jest-cli/build/cli/runCLI.js:15:12)
    at Object.module.exports [as runCLI] (/Users/DavidHu/Desktop/coding/projects/swapnow/node_modules/jest-cli/build/cli/runCLI.js:2 03:17)
    at Object.run (/Users/DavidHu/.nvm/versions/node/v6.2.1/lib/node_modules/jest-cli/build/cli/index.js:42:17)
    at Object.<anonymous> (/Users/DavidHu/.nvm/versions/node/v6.2.1/lib/node_modules/jest-cli/bin/jest.js:16:25)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)

I went into node_modules to look at the line of code that is causing the error, projects is a string of the project current path.

Here is my jest configuration in package.json

  "jest": {
    "transform": {
      ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
    },
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ]
  }

Has anyone encountered this error and know how to fix it?

like image 703
davidhu Avatar asked May 07 '17 23:05

davidhu


3 Answers

Just ran into the same problem. If you look at your stack trace closely you'll notice that you run two different Jest setups:

Globally installed jest:

...(/Users/DavidHu/.nvm/versions/node/v6.2.1/lib/node_modules/jest-cli/bin/jest.js:16:25)

Jest from the project:

...(/Users/DavidHu/Desktop/coding/projects/swapnow/node_modules/jest-cli/build/cli/runCLI.js:172:28)

They are probably have different (and incompatible) versions. If you remove jest from your project (or from global node_modules) that should solve the problem.

like image 108
jafrog Avatar answered Oct 19 '22 17:10

jafrog


This will happen if you update jest in your project but run it using just jest from the command line (not from node_modules).

To fix you'll need to update your global jest:

npm install -g jest

like image 31
Rick Hanlon II Avatar answered Oct 19 '22 15:10

Rick Hanlon II


I had the same issue. I was passing a string to the 'projects' parameter instead of an array.

Wrapping the string 'projectPath' in [] fixed it for me (this is located inside a Gulp task):

var jestCLI = require('jest-cli');
...
jestCLI.runCLI({'onlyChanged': true}, [projectPath], callback);
like image 3
Koen Avatar answered Oct 19 '22 16:10

Koen