107

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.

Henke
  • 4,445
  • 3
  • 31
  • 44
Aessandro
  • 5,517
  • 21
  • 66
  • 139
  • Why did you comment out the test script that comes with create-react-app? That should run jest just fine for you. – djfdev May 02 '18 at 15:54
  • I have tried yo update snapshots that failed but didnt work ("jest command not found" after running jest --updateSnapshot) – Aessandro May 02 '18 at 15:55

20 Answers20

132

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.

CaptEmulation
  • 4,416
  • 2
  • 17
  • 14
  • 4
    what exactly npm test -- do in this case? where this -- documented? – 31415926 Feb 01 '19 at 09:20
  • 1
    `test` and `start` are special fields in `scripts' that can be executed without `npm run`. `npm test` is the same command as `npm run test` – CaptEmulation Feb 03 '19 at 18:14
  • If you have modified your package test script and want quick direct access to jest via your node modules you could also add a system link. `ln -s ./node_modules/.bin/jest jest` and then use it with `./jest` – sam Feb 20 '20 at 11:29
  • Documentation on above answer: https://jestjs.io/docs/en/cli.html#using-with-npm-scripts – sam Feb 20 '20 at 11:30
  • I just ran into this issue where I had been using jest on the CLI fine until suddenly jest was an 'unknown command'. Turns out I had used NVM to switch to a older version of node (10.13.0) and I think this switches the NPM version as well. Switching back to 10.17.0 fixed this issue for me. So I assume NPM adds ./node_modules/.bin to the path in later versions. – James Webb Mar 04 '20 at 15:06
  • Why does `npm test` work on windows but not on linux? In neither system is `jest` installed globally but in windows it runs fine and in linux it says " jest: command not found" – Marc Jun 10 '20 at 11:49
  • do I need jest-cli to run `jest` in cmd? `npm t` should be fine, so why bother with `jest-cli` and add this package? – Timo Mar 24 '21 at 15:20
63

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

npm install -g jest
Sidd Thota
  • 2,040
  • 1
  • 20
  • 24
  • 4
    This worked for me as well. I was able to execute the "jest" command after installing globally. If you don't have it installed globally you can use the "npx jest" command. – Webalation Sep 15 '20 at 15:04
  • 12
    I would not advocate for solving path issues by installing npm modules globally. It creates a reliance on the host environment and can cause breakages in CI environments. – TheRocketSurgeon Sep 16 '20 at 00:16
48

You need to run it this way :

./node_modules/.bin/jest

or run npm test

Hamza El Aoutar
  • 5,292
  • 2
  • 17
  • 23
20

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.

dan-klasson
  • 13,734
  • 14
  • 63
  • 101
sybozz
  • 867
  • 8
  • 7
11

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

cJilbert504
  • 123
  • 1
  • 6
9

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

npx jest --version


npx jest --init
Willian Arana
  • 615
  • 8
  • 7
7

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
  • Many thanks! `npx jest tests` works well for me. The folder `tests` is at the same level as `package.json`. – Emman Feb 27 '22 at 13:19
6

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.
tywhang
  • 299
  • 4
  • 5
3

just use command

npm test or npm t

аlex
  • 5,426
  • 1
  • 29
  • 38
2

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

kidroca
  • 3,480
  • 2
  • 27
  • 44
2

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

Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
1

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

Diego Ulloa
  • 500
  • 8
  • 19
1

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
Roman
  • 19,236
  • 15
  • 93
  • 97
1

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.

1

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
Abraham
  • 8,525
  • 5
  • 47
  • 53
1

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

Abdul-Hammid
  • 61
  • 1
  • 2
1

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.

0

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

Hien Nguyen
  • 156
  • 3
  • 9
0

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

{
  "dependencies": {
    ...
    "jest": "^29.3.1",
    ...
  }
}
Lukasz Dynowski
  • 11,169
  • 9
  • 81
  • 124
0

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

kob003
  • 2,206
  • 2
  • 12
  • 19