【问题标题】:command not found: jest找不到命令:开玩笑
【发布时间】:2018-10-12 19:02:01
【问题描述】:

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

但 jest 已安装。

【问题讨论】:

  • 为什么要把create-react-app自带的测试脚本注释掉?这对你来说应该很好玩。
  • 我已经尝试过更新失败但没有工作的快照(运行 jest --updateSnapshot 后“找不到 jest 命令”)

标签: javascript reactjs jestjs


【解决方案1】:

只需使用命令

npm testnpm t

【讨论】:

    【解决方案2】:

    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},它应该可以工作。

    【讨论】:

    • npm test 究竟是什么——在这种情况下是做什么的?这 - 记录在哪里?
    • teststartscripts' that can be executed without npm run. npm test 中的特殊字段,与 npm run test 的命令相同
    • 如果您修改了包测试脚本并希望通过节点模块快速直接访问 jest,您还可以添加系统链接。 ln -s ./node_modules/.bin/jest jest 然后和./jest一起使用
    • 我刚刚遇到了这个问题,我一直在 CLI 上使用 jest,直到突然 jest 是一个“未知命令”。原来我使用 NVM 切换到旧版本的节点(10.13.0),我认为这也切换了 NPM 版本。切换回 10.17.0 为我解决了这个问题。所以我假设 NPM 在以后的版本中将 ./node_modules/.bin 添加到路径中。
    【解决方案3】:

    你需要这样运行它:

    ./node_modules/.bin/jest
    

    或运行npm test

    【讨论】:

      【解决方案4】:

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

      npm install --save-dev jest-cli
      

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

      【讨论】:

        【解决方案5】:

        在我的例子中,npm 出于某种原因没有安装jest 命令。

        解决这个问题:

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

        【讨论】:

        • 当没有任何帮助删除 node_modules 时,感谢您的提醒 :)
        【解决方案6】:

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

        npm install -g jest
        

        【讨论】:

        • 这对我也有用。全局安装后,我能够执行“jest”命令。如果你没有全局安装它,你可以使用“npx jest”命令。
        • 我不提倡通过全局安装 npm 模块来解决路径问题。它会产生对主机环境的依赖,并可能导致 CI​​ 环境中断。
        【解决方案7】:

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

        【讨论】:

          【解决方案8】:

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

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

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

          【讨论】:

          • 我相信这依赖于全局安装的 jest
          【解决方案9】:

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

          最后我添加了

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

          到我的管道 .yml 和 violá

          注意

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

          【讨论】:

            【解决方案10】:

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

            【讨论】:

              【解决方案11】:

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

              【讨论】:

                【解决方案12】:

                我使用yarn。将jestjest-cli 添加到node_modules 对我尝试运行jest mytest.test.js 之类的测试没有任何影响。除了上述步骤之外,以下步骤有助于使其运行:

                yarn jest mytest.test.js
                

                【讨论】:

                  【解决方案13】:

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

                  【讨论】:

                    【解决方案14】:

                    尝试使用命令

                    npx jest <folder>
                    

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

                    我也安装了 jest CLI

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

                    npm install --save-dev jest-cli
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2021-05-30
                      • 2021-01-01
                      • 1970-01-01
                      • 2020-04-12
                      • 2019-12-28
                      • 2021-11-17
                      • 1970-01-01
                      相关资源
                      最近更新 更多