command not found: jest

2020-02-26 03:08发布

问题:

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 packaje.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

but jest is installed.

回答1:

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.



回答2:

You need to run it this way :

./node_modules/.bin/jest

or run npm test



回答3:

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

npm install -g jest


回答4:

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.



回答5:

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.


回答6:

just use command

npm test or npm t



回答7:

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



回答8:

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