Javascript 未找到命令:jest

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50138532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:33:30  来源:igfitidea点击:

command not found: jest

javascriptreactjsjestjs

提问by Alessandro

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 packaje.json:

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

when I run "jest --updateSnapshot" I get:

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

command not found: jest

but jest is installed.

但是安装了 jest。

enter image description here

在此处输入图片说明

回答by CaptEmulation

Jestis installed, but is likely in your ./node_modules/.bindirectory. You can append that to your command ./node_modules/.bin/jest --updateSnapshot. Since you already have jestas a scriptscommand in your package.jsonyou can also run it with npm test -- --updateSnapshot. npm automatically adds ./node_modules/.binto your path.

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

回答by El Aoutar Hamza

You need to run it this way :

你需要这样运行它:

./node_modules/.bin/jest

or run npm test

或运行 npm test

回答by Siddhartha Thota

I ran into similar issue. I fixed it by installing jest globally.

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

npm install -g jest

回答by sybozz

Install the Jest command-line interface (Jest CLI):

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

npm install --save-dev jest-cli

Then run the jest command. Working for me in a linux instance by docker on Windows 10.

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

回答by tywhang

In my case, npm didn't install the jestcommand for some reason.

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

To fix this:

要解决此问题:

  1. I deleted the node_modules/jestdirectory
  2. Re-ran npm installand got the jestcommand installed.
  1. 我删除了node_modules/jest目录
  2. 重新运行npm installjest安装了命令。

回答by аlex dyky?

just use command

只需使用命令

npm testor npm t

npm test或者 npm t

回答by kidroca

Removing node_modules and running npm installagain fixed this for me Also the "new" npm cicommand can fix this as it deletes (or clears) node modules and performs a clean install each time, plus it's faster compared to manually deleting node_modulesand re-installing

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

回答by Diego Ulloa

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 执行