Aessandro
Aessandro

Reputation: 5761

'command not found: jest'

I have a test file like so: (I am using create-react-app)

import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/Calculator';

import { getAction, getResult } from './actions/'

import {shallow} from 'enzyme';
import toJson from 'enzyme-to-json';

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });

it('renders without crashing', () => {
  const wrapper = shallow(<App />)
  expect(toJson(wrapper)).toMatchSnapshot();
});

it('displays the choosen operator', () => {
  const action = {
      type: 'GET_ACTION',
      operator: '+'
    };
  expect(getAction("+")).toEqual(action)
})

it('displays the typed digit', () => {
  const action = {
    type: 'GET_RESULT',
    n: 3
  };
  expect(getResult(3)).toEqual(action);
})

it('checks that the clickevent for getNumber is called',() => {
  const clickEvent = jest.fn();
  const p = shallow(<p data-n="1" onClick={clickEvent}>1</p>)
  p.simulate('click')
  expect(clickEvent).toBeCalled();
})

and a package.json:

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-scripts": "1.1.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    // "test": "react-scripts test --env=jsdom",
    "test": "jest",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "enzyme": "^3.3.0",
    "enzyme-adapter-react-16": "^1.1.1",
    "enzyme-to-json": "^3.3.3",
    "jest": "^22.4.3"
  }
}

when I run jest --updateSnapshot I get:

command not found: jest

enter image description here

but jest is installed.

Upvotes: 120

Views: 217922

Answers (22)

Defcon41
Defcon41

Reputation: 169

In my case the issue was that I didn't install the jest package go to your node_modules/ folder and look for jest if it does exist just delete it and npm install it else if it doesn't just install it and add this script to "test": "jest" the package.json and run npm install

Upvotes: 0

use

npx jest --init

or

./node_modules/.bin/jest --init

Upvotes: 1

kob003
kob003

Reputation: 3727

running jest directly from terminal i.e npm jest shows this error.

what worked for me was adding jest script in package.json file i.e

{
  "scripts" : {
    "test":"jest"
  }
}

and then running npm test works.

--------- OR -------------

you can use npx to run jest commands directly from terminal i.e npx jest

Upvotes: 1

Lukasz Dynowski
Lukasz Dynowski

Reputation: 13610

Alternatively, just add jest module to package.json dependencies.

{
  "dependencies": {
    ...
    "jest": "^29.3.1",
    ...
  }
}

Upvotes: 0

Roopa Rauniyar
Roopa Rauniyar

Reputation: 121

Faced the same issue. But it was due to the wrong node version. If you use the latest jest v29, you need Node version 14 or higher.

Upvotes: 1

Willian Arana
Willian Arana

Reputation: 845

A way to solve the error is to use the "npx" command.

npx jest --version


npx jest --init

Upvotes: 15

Abdul-Hammid
Abdul-Hammid

Reputation: 71

Had the same issue and was able to solve it by running npm install

Upvotes: 1

Abraham
Abraham

Reputation: 9865

In my case, I was trying to install jest with yarn on a pipeline to run tests and since I had jest installed as a devDependency it wasn't installing on yarn install.

I found this bug on GitHub https://github.com/yarnpkg/yarn/issues/2739 that it seems that Yarn will not install devDependencies when NODE_ENV=production.

I just needed to change the NODE_ENV and after that, it was working, otherwise, run it like this:

yarn install --production=false

Upvotes: 1

Muhammad Zeeshan Ejaz
Muhammad Zeeshan Ejaz

Reputation: 186

try using the command

npx jest <folder>

I ran into the same problem. I tried multiple solutions and this worked.

I also have jest CLI installed

you can install it by using this command in your shell

npm install --save-dev jest-cli

Upvotes: 9

Ismael Antonio
Ismael Antonio

Reputation: 141

you can run ln -s ./node_modules/.bin/jest jest and then run jest --init it will work. Or you can install jest cli with npm install --save-dev jest-cli and then run jest --init it will also work.

Upvotes: 1

Roman
Roman

Reputation: 21765

I use yarn. Adding jest and jest-cli to node_modules did not make any difference with my attempts to run tests like jest mytest.test.js. In addition to mentioned steps, the following helped to get it running:

yarn jest mytest.test.js

Upvotes: 1

Hien Nguyen
Hien Nguyen

Reputation: 156

You can run the test using npx jest [parameters]. npx is the package runner. It will help you execute a locally installed package.

Upvotes: 0

CaptEmulation
CaptEmulation

Reputation: 4586

Jest is installed, but is likely in your ./node_modules/.bin directory. You can append that to your command ./node_modules/.bin/jest --updateSnapshot. Since you already have jest as a scripts command in your package.json you can also run it with npm test -- --updateSnapshot. npm automatically adds ./node_modules/.bin to your path.

update: Newer versions of yarn will resolve node module bin scripts, so you can also just run yarn jest {cmd} and it should work.

Upvotes: 145

cJilbert504
cJilbert504

Reputation: 133

I was getting zsh: command not found: jest after installing jest and trying to use the command jest. The solution that worked for me was running npx jest

Upvotes: 12

Jacksonkr
Jacksonkr

Reputation: 32207

My situation was caused by my git pipeline. I wasn't caching node_modules nor was I caching untracked files.

Ultimately I added

cache:
  # untracked: true
  key:
    files:
      - package-lock.json
  paths:
    - node_modules

to my pipeline .yml and violá

Note

you can either use path OR untracked, find out more about each to see what works best for you

Upvotes: 2

Diego Ulloa
Diego Ulloa

Reputation: 639

Just reload your bash config file after install jest:

source ~/.bashrc # on linux ?
source ~/.bash_profile # on macOs

Jest will be not recognized but executed with npx jest automatically

Upvotes: 1

kidroca
kidroca

Reputation: 3856

Removing node_modules and running npm install again fixed this for me Also the "new" npm ci command can fix this as it deletes (or clears) node modules and performs a clean install each time, plus it's faster compared to manually deleting node_modules and re-installing

Upvotes: 2

Sidd Thota
Sidd Thota

Reputation: 2249

I ran into similar issue. I fixed it by installing jest globally.

npm install -g jest

Upvotes: 73

tywhang
tywhang

Reputation: 309

In my case, npm didn't install the jest command for some reason.

To fix this:

  1. I deleted the node_modules/jest directory
  2. Re-ran npm install and got the jest command installed.

Upvotes: 6

sybozz
sybozz

Reputation: 937

Install the Jest command-line interface (Jest CLI):

npm install --save-dev jest-cli

Then run the jest command. Working for me in a linux instance by docker on Windows 10.

Upvotes: 23

Hamza El Aoutar
Hamza El Aoutar

Reputation: 5657

You need to run it this way :

./node_modules/.bin/jest

or run npm test

Upvotes: 49

аlex
аlex

Reputation: 5698

just use command

npm test or npm t

Upvotes: 3

Related Questions