繁体   English   中英

'找不到命令:开玩笑'

[英]'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已安装,但可能位于您的./node_modules/.bin目录中。 您可以将其附加到您的命令./node_modules/.bin/jest --updateSnapshot 由于您已经在package.json中将jest作为scripts命令,您还可以使用npm test -- --updateSnapshot运行它。 npm 会自动将./node_modules/.bin添加到您的路径中。

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

你需要这样运行它:

./node_modules/.bin/jest

或运行npm test

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

npm install -g jest

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

npm install --save-dev jest-cli

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

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

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

要解决此问题:

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

只需使用命令

npm testnpm t

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

npx jest --version


npx jest --init

删除 node_modules 并再次运行npm install为我解决了这个问题 “新的” 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á

笔记

您可以使用pathuntracked了解有关每个的更多信息以查看最适合您的方法

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

就我而言,我试图在管道上安装 jest with yarn 以运行测试,并且由于我将 jest 作为devDependency安装,因此它没有安装在 yarn install 上。

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

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

yarn install --production=false

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

我用yarn jestjest-cli添加到 node_modules 与我尝试运行jest mytest.test.js类的测试没有任何区别。 除了提到的步骤,以下有助于让它运行:

yarn jest mytest.test.js

尝试使用命令

npx jest <folder>

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

我还安装了 jest CLI

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

npm install --save-dev jest-cli

有同样的问题,并能够通过运行 npm install 解决它

面临同样的问题。 但这是由于节点版本错误。 如果您使用最新的 jest v29 ,则需要 Node 版本 14 或更高版本。

或者,只需将jest模块添加到package.json依赖项。

{
  "dependencies": {
    ...
    "jest": "^29.3.1",
    ...
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM