命令未找到:开玩笑

Ale*_*dro 26 javascript reactjs jestjs

我有一个像这样的测试文件:(我正在使用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();
})
Run Code Online (Sandbox Code Playgroud)

和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"
  }
}
Run Code Online (Sandbox Code Playgroud)

当我运行"jest --updateSnapshot"时,我得到:

command not found: jest
Run Code Online (Sandbox Code Playgroud)

但是安装了jest.

在此输入图像描述

Cap*_*ion 43

Jest已安装,但可能在您的./node_modules/.bin目录中.您可以将它附加到您的命令./node_modules/.bin/jest --updateSnapshot.既然你已经拥有jest了一个scripts命令,你package.json也可以运行它npm test -- --updateSnapshot.npm会自动添加./node_modules/.bin到您的路径中.

  • 到底是什么npm测试-在这种情况下呢?记录在哪里? (2认同)

El *_*mza 16

你需要以这种方式运行它:

./node_modules/.bin/jest
Run Code Online (Sandbox Code Playgroud)

或跑 npm test


Wil*_*ana 13

解决该错误的一种方法是使用“npx”命令。

npx jest --version


npx jest --init
Run Code Online (Sandbox Code Playgroud)


小智 8

尝试使用命令

npx jest <folder>
Run Code Online (Sandbox Code Playgroud)

我遇到了同样的问题。我尝试了多种解决方案并且有效。

我还安装了 jest CLI

您可以在 shell 中使用此命令来安装它

npm install --save-dev jest-cli
Run Code Online (Sandbox Code Playgroud)


Sid*_*ota 7

我遇到了类似的问题。我通过在全球范围内安装笑话来修复它。

npm install -g jest
Run Code Online (Sandbox Code Playgroud)

  • 我不提倡通过全局安装 npm 模块来解决路径问题。它会产生对主机环境的依赖,并可能导致 CI​​ 环境出现故障。 (14认同)
  • 这对我也有用。全局安装后我能够执行“jest”命令。如果您没有全局安装它,您可以使用“npx jest”命令。 (4认同)

syb*_*ozz 6

安装Jest命令行界面(Jest CLI):

npm install --save-dev jest-cli
Run Code Online (Sandbox Code Playgroud)

然后运行jest命令。在Windows 10上由docker在Linux实例中为我工作。


小智 6

zsh: command not found: jest在安装 jest 并尝试使用命令后得到了jest。对我有用的解决方案正在运行npx jest


小智 5

就我而言,npmjest由于某种原因没有安装该命令。

要解决此问题:

  1. 我删除了node_modules/jest目录
  2. 重新运行npm installjest安装了命令。