简体   繁体   English

'找不到命令:开玩笑'

[英]'command not found: jest'

I have a test file like so: (I am using create-react-app)我有一个这样的测试文件:(我正在使用 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 package.json:和一个 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"
  }
}

when I run jest --updateSnapshot I get:当我运行jest --updateSnapshot我得到:

command not found: jest

在此处输入图像描述

but jest is installed.但开玩笑已安装。

Jest is installed, but is likely in your ./node_modules/.bin directory. Jest已安装,但可能位于您的./node_modules/.bin目录中。 You can append that to your command ./node_modules/.bin/jest --updateSnapshot .您可以将其附加到您的命令./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 .由于您已经在package.json中将jest作为scripts命令,您还可以使用npm test -- --updateSnapshot运行它。 npm automatically adds ./node_modules/.bin to your path. npm 会自动将./node_modules/.bin添加到您的路径中。

update : Newer versions of yarn will resolve node module bin scripts, so you can also just run yarn jest {cmd} and it should work.更新:较新版本的纱线将解析节点模块 bin 脚本,因此您也可以运行yarn jest {cmd}并且它应该可以工作。

You need to run it this way :你需要这样运行它:

./node_modules/.bin/jest

or run npm test或运行npm test

I ran into similar issue.我遇到了类似的问题。 I fixed it by installing jest globally.我通过在全球安装 jest 来修复它。

npm install -g jest

Install the Jest command-line interface (Jest CLI):安装 Jest 命令行界面 (Jest CLI):

npm install --save-dev jest-cli

Then run the jest command.然后运行 ​​jest 命令。 Working for me in a linux instance by docker on Windows 10.在 Windows 10 上的 docker 的 linux 实例中为我工作。

I was getting zsh: command not found: jest after installing jest and trying to use the command jest .我在安装 jest 并尝试使用命令jest后收到zsh: command not found: jest jest The solution that worked for me was running npx jest对我npx jest的解决方案是运行npx jest

In my case, npm didn't install the jest command for some reason.就我而言,npm 由于某种原因没有安装jest命令。

To fix this:要解决此问题:

  1. I deleted the node_modules/jest directory我删除了node_modules/jest目录
  2. Re-ran npm install and got the jest command installed.重新运行npm installnpm installjest命令。

just use command只需使用命令

npm test or npm t npm testnpm t

A way to solve the error is to use the "npx" command.解决错误的一种方法是使用“npx”命令。

npx jest --version


npx jest --init

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

Just reload your bash config file after install jest:安装 jest 后只需重新加载 bash 配置文件:

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

Jest will be not recognized but executed with npx jest automatically Jest 不会被识别,但会自动使用 npx jest 执行

My situation was caused by my git pipeline.我的情况是由我的 git 管道引起的。 I wasn't caching node_modules nor was I caching untracked files.我没有缓存node_modules也没有缓存未跟踪的文件。

Ultimately I added最后我加了

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

to my pipeline .yml and violá到我的管道 .yml 和 violá

Note笔记

you can either use path OR untracked , find out more about each to see what works best for you您可以使用pathuntracked了解有关每个的更多信息以查看最适合您的方法

you can run ln -s ./node_modules/.bin/jest jest and then run jest --init it will work.你可以运行ln -s ./node_modules/.bin/jest jest然后运行jest --init它会起作用。 Or you can install jest cli with npm install --save-dev jest-cli and then run jest --init it will also work.或者您可以使用npm install --save-dev jest-cli ,然后运行jest --init它也可以工作。

In my case, I was trying to install jest with yarn on a pipeline to run tests and since I had jest installed as a devDependency it wasn't installing on yarn install.就我而言,我试图在管道上安装 jest with yarn 以运行测试,并且由于我将 jest 作为devDependency安装,因此它没有安装在 yarn install 上。

I found this bug on GitHub https://github.com/yarnpkg/yarn/issues/2739 that it seems that Yarn will not install devDependencies when NODE_ENV=production.我在 GitHub https://github.com/yarnpkg/yarn/issues/2739上发现了这个错误,当 NODE_ENV=production 时,Yarn 似乎不会安装 devDependencies。

I just needed to change the NODE_ENV and after that, it was working, otherwise, run it like this:我只需要更改 NODE_ENV 之后,它就可以工作了,否则,像这样运行它:

yarn install --production=false

You can run the test using npx jest [parameters] .您可以使用npx jest [parameters]运行测试。 npx is the package runner. npx是包运行程序。 It will help you execute a locally installed package.它将帮助您执行本地安装的包。

I use yarn .我用yarn Adding jest and jest-cli to node_modules did not make any difference with my attempts to run tests like jest mytest.test.js .jestjest-cli添加到 node_modules 与我尝试运行jest mytest.test.js类的测试没有任何区别。 In addition to mentioned steps, the following helped to get it running:除了提到的步骤,以下有助于让它运行:

yarn jest mytest.test.js

try using the command尝试使用命令

npx jest <folder>

I ran into the same problem.我遇到了同样的问题。 I tried multiple solutions and this worked.我尝试了多种解决方案,这奏效了。

I also have jest CLI installed我还安装了 jest CLI

you can install it by using this command in your shell你可以在你的 shell 中使用这个命令来安装它

npm install --save-dev jest-cli

Had the same issue and was able to solve it by running npm install有同样的问题,并能够通过运行 npm install 解决它

Faced the same issue.面临同样的问题。 But it was due to the wrong node version.但这是由于节点版本错误。 If you use the latest jest v29 , you need Node version 14 or higher.如果您使用最新的 jest v29 ,则需要 Node 版本 14 或更高版本。

Alternatively, just add jest module to package.json dependencies.或者,只需将jest模块添加到package.json依赖项。

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

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

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