簡體   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