76

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

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

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

command not found: jest

但开玩笑已安装。

在此处输入图像描述

4

15 回答 15

98

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

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

于 2018-05-02T15:45:15.830 回答
43

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

./node_modules/.bin/jest

或运行npm test

于 2018-05-02T15:45:39.083 回答
43

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

npm install -g jest
于 2019-06-02T16:04:59.850 回答
13

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

npm install --save-dev jest-cli

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

于 2018-09-12T09:02:49.100 回答
6

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

要解决这个问题:

  1. 我删除了node_modules/jest目录
  2. 重新运行npm installjest安装命令。
于 2019-01-15T19:24:19.007 回答
6

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

于 2020-09-08T17:33:43.793 回答
4

只需使用命令

npm test或者npm t

于 2018-05-02T15:44:19.643 回答
2

尝试使用命令

npx jest <folder>

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

我也安装了 jest CLI

你可以在你的shell中使用这个命令来安装它

npm install --save-dev jest-cli
于 2021-12-21T16:25:35.663 回答
1

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

于 2019-08-12T22:24:52.750 回答
1

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

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

Jest 将不会被识别,但会自动使用 npx jest 执行

于 2020-02-10T11:58:56.133 回答
1

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

最终我添加了

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

到我的管道 .yml 和 violá

笔记

您可以使用pathOR untracked详细了解每种方法,看看哪种方法最适合您

于 2020-07-29T15:19:23.427 回答
1

你可以运行ln -s ./node_modules/.bin/jest jest 然后运行jest --init 它会工作。或者你可以安装 jest clinpm install --save-dev jest-cli然后运行jest --init它也可以工作。

于 2021-09-02T01:09:35.223 回答
0

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

于 2021-07-11T11:06:01.100 回答
0

我用yarn. 添加jestjest-cli到 node_modules 与我尝试运行jest mytest.test.js. 除了提到的步骤之外,以下步骤有助于使其运行:

yarn jest mytest.test.js
于 2021-08-29T21:47:45.503 回答
0

就我而言,我试图在管道上安装 jest 和 yarn 以运行测试,并且因为我安装了 jest,因为devDependency它没有安装在 yarn install 上。

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

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

yarn install --production=false
于 2022-03-03T16:42:00.247 回答