'command not found: jest'

'command not found: jest'

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

和一个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"
  }
}

当我 运行 jest --updateSnapshot 我得到:

command not found: jest

但是安装了 jest。

只需使用命令

npm testnpm t

Jest 已安装,但可能在您的 ./node_modules/.bin 目录中。您可以将其附加到您的命令 ./node_modules/.bin/jest --updateSnapshot。由于您已经在 package.json 中将 jest 作为 scripts 命令,因此您也可以 运行 使用 npm test -- --updateSnapshot。 npm 会自动将 ./node_modules/.bin 添加到您的路径中。

更新:较新版本的 yarn 将解析节点模块 bin 脚本,因此您也可以 运行 yarn jest {cmd} 它应该可以工作。

你需要这样 运行 :

./node_modules/.bin/jest

或运行npm test

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

npm install --save-dev jest-cli

然后运行 jest 命令。 docker 在 linux 实例中为我工作 Windows 10.

就我而言,npm 出于某种原因没有安装 jest 命令。

解决这个问题:

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

我运行遇到了类似的问题。我通过全局安装 jest 修复了它。

npm install -g jest

删除 node_modules 和 运行 npm install 再次为我解决了这个问题 "new" npm ci 命令也可以修复此问题,因为它会删除(或清除)节点模块并每次执行全新安装,而且与手动删除 node_modules 并重新安装

只需在安装 jest 后重新加载 bash 配置文件:

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

Jest 将无法识别,但会自动使用 npx jest 执行

我的情况是由我的 git 管道引起的。我没有缓存 node_modules 也没有缓存未跟踪的文件。

最后我加了

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

到我的管道 .yml 和 violá

备注

您可以使用 pathuntrackedfind out more about each 来查看最适合您的方法

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

您可以 运行 使用 npx jest [parameters] 进行测试。 npx 是包 运行ner。它将帮助您执行本地安装的包。

我用yarn。将 jestjest-cli 添加到 node_modules 对我尝试 运行 测试(如 jest mytest.test.js)没有任何影响。除了提到的步骤之外,以下内容有助于获得它 运行ning:

yarn jest mytest.test.js

你可以运行ln -s ./node_modules/.bin/jest jest 然后 运行 jest --init 就可以了。或者您可以使用 npm install --save-dev jest-cli 安装 jest cli 然后 运行 jest --init 它也可以工作。

尝试使用命令

npx jest <folder>

我运行陷入同样的​​问题。我尝试了多种解决方案,这很有效。

我也安装了 jest CLI

您可以在 shell

中使用此命令安装它
npm install --save-dev jest-cli

在我的例子中,我试图在管道上安装 jest 和 yarn 以进行 运行 测试,因为我已经将 jest 安装为 devDependency 它没有安装在 yarn install 上。

我在 GitHub https://github.com/yarnpkg/yarn/issues/2739 上发现了这个错误,似乎 Yarn 在 NODE_ENV=production 时不会安装 devDependencies。

我只需要更改 NODE_ENV 之后,它就可以工作了,否则,运行 就像这样:

yarn install --production=false

有同样的问题,可以通过 运行 npm install

解决