'command not found: jest' - javascript

I have a test file like so: (I am using 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:
{
"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:
command not found: jest
but jest is installed.

Jest is installed, but is likely in your ./node_modules/.bin directory. You can append that to your command ./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. npm automatically adds ./node_modules/.bin to your path.
update: Newer versions of yarn will resolve node module bin scripts, so you can also just run yarn jest {cmd} and it should work.

I ran into similar issue. I fixed it by installing jest globally.
npm install -g jest

You need to run it this way :
./node_modules/.bin/jest
or run npm test

Install the Jest command-line interface (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.

I was getting zsh: command not found: jest after installing jest and trying to use the command jest. The solution that worked for me was running npx jest

A way to solve the error is to use the "npx" command.
npx jest --version
npx jest --init

In my case, npm didn't install the jest command for some reason.
To fix this:
I deleted the node_modules/jest directory
Re-ran npm install and got the jest command installed.

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
you can install it by using this command in your shell
npm install --save-dev jest-cli

just use command
npm test or npm t

Removing node_modules and running npm install again fixed this for me
Also the "new" npm ci command 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_modules and re-installing

My situation was caused by my git pipeline. I wasn't caching node_modules nor was I caching untracked files.
Ultimately I added
cache:
# untracked: true
key:
files:
- package-lock.json
paths:
- node_modules
to my pipeline .yml and violá
Note
you can either use path OR untracked, find out more about each to see what works best for you

Just reload your bash config file after install jest:
source ~/.bashrc # on linux ?
source ~/.bash_profile # on macOs
Jest will be not recognized but executed with npx jest automatically

I use 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. In addition to mentioned steps, the following helped to get it running:
yarn jest mytest.test.js

you can run ln -s ./node_modules/.bin/jest jest
and then run jest --init it will work. Or you can install jest cli with npm install --save-dev jest-cli and then run jest --init it will also work.

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.
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.
I just needed to change the NODE_ENV and after that, it was working, otherwise, run it like this:
yarn install --production=false

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.

You can run the test using npx jest [parameters]. npx is the package runner. It will help you execute a locally installed package.

Had the same issue and was able to solve it by running npm install

Alternatively, just add jest module to package.json dependencies.
{
"dependencies": {
...
"jest": "^29.3.1",
...
}
}

Related

Getting error "babel" is not recognized as an internal or external command,

I have a simple tests.js file that I wish to follow up with a source map file. I have tried initiating the dependencies yet the prompt shows the error. If anyone could specify the problem and the solution would be grateful ^^
tests.js
var add = (...arr) => {
return arr.reduce((sum, el) =>{
return sum+el;
}, 0)
}
console.log(add(1,2,3));
I've tried these commands at first
npm install -g babel-cli
npm install babel-preset-es2015
babel tests.js --out-file tests.dist.js --source-maps --presets=es2015
But received the same error. I have followed another solution from the community but it still didn't work. The solution was to remove node_modules and reinitiate the dependencies.
npm install --save-dev #babel/core #babel/cli #babel/preset-env #babel/node
and add "start": "nodemon --exec babel-node index.js", in the dependencies.
I checked the node_modules and it had the existence of these files too.
node_modules/.bin/babel-node
node_modules/.bin/babel-node.cmd - windows only
node_modules/#babel/node/bin/babel-node.js
The solution I followed Still, I couldn't figure out how to solve this issue. This is my first time working with node and babel. My node version is v16.13.1
Edited
Folder Structure
Y:.
| index.html
| package-lock.json
| package.json
| tests.js
| tree.txt
|
\---node_modules
| .package-lock.json
|
+---.bin
| babel
.....
It's huge!
package.json
{
"dependencies": {
"#babel/cli": "^7.16.8",
"#babel/core": "^7.16.12",
"#babel/preset-env": "^7.16.11"
}
}
.babelrc
{
"presents": [
"#babel/preset-env"
]
}
./node_modules/#babel/cli/bin/babel.js
require("../lib/babel");
After using bash for the command -
./node_modules/#babel/cli/bin/babel.js example.js --out-file main.dist.js
The error
Error: Unknown option: .presents. Check out https://babeljs.io/docs/en/babel-core/#options for more information about options.
at throwUnknownError (Y:\babel work\node_modules\#babel\core\lib\config\validation\options.js:133:27)
at Y:\babel work\node_modules\#babel\core\lib\config\validation\options.js:118:5
at Array.forEach (<anonymous>)
at validateNested (Y:\babel work\node_modules\#babel\core\lib\config\validation\options.js:94:21)
at validate (Y:\babel work\node_modules\#babel\core\lib\config\validation\options.js:85:10)
at Y:\babel work\node_modules\#babel\core\lib\config\config-chain.js:209:34
at cachedFunction (Y:\babel work\node_modules\#babel\core\lib\config\caching.js:60:27)
at cachedFunction.next (<anonymous>)
at evaluateSync (Y:\babel work\node_modules\gensync\index.js:251:28)
at sync (Y:\babel work\node_modules\gensync\index.js:89:14) {
code: 'BABEL_UNKNOWN_OPTION'
}
Hyy,
[update]
installing babel locally
npm i #babel/core #babel/cli #babel/preset-env
Inside package.json add npm script
"scripts": {
"start-babel": "babel example.js --out-file main.dist.js"
},
You need .babelrc file for all you configuration
// basic need
{
"presets": [
"#babel/preset-env"
]
}
Run script by npm run start-babel this will create main.dist.js js transpiled file
You need .babelrc file with some configuration
if you have installed #bable/core #babel/cli #bable/preset-env locally
Then you have to use the path of of babel.js of node_modules like this
if you installed it globally using -g then you don't need a path just use babel

'npm run test' works, but 'jest --coverage' command does not work

I have MonoRepo project (using Lerna) which consists of multiple packages, and one of them is a React application.
Within the React project package, I utilise Jest for unit testing. However, when I run the jest --coverage command on the WebStorm console, it returns me the following error:
zsh: command not found: jest
Strangely enough, if I use npm run test, the test runs, and my test coverage report is generated. Here is part of my package.json:
"scripts": {
"test": "jest --coverage",
// other stuff
},
As you can see, npm run test runs jest --coverage, which is the exact command I initially entered. Why won't the jest command work on its own?
You need to run it directly from your node_modules like this:
./node_modules/.bin/jest --coverage
See https://jestjs.io/docs/en/getting-started#running-from-command-line for more information
The most straightforward solution is to install jest globally with:
npm i -g jest

How to update snapshot with Jest and vue-cli

I should be able to add -u parameter when running my tests, but I can't figure out why it doesn't work:
npm run test ComponentName.spec.js -u
npm run test ComponentName.spec.js --updateSnapshot
but it doesn't work. My package.json:
"scripts": {
"test": "vue-cli-service test:unit",
I know I can just delete the snapshot files, but I'd like to figure out why the command doesn't work.
Based on the docs:
npm run test -- -u
I verified this works.
In vue-cli 3, your usual npm command calls vue-cli-service and not jest anymore. Vue-cli-service will call jest for you.
Either you can run :
npm run test:unit -- -u
the -- is so that the next arguments have to be passed to the subcommand.
Or
npx vue-cli-service test:unit -u
This will run the tests and upgrade the snapshots.
yarn test -u worked for me. We use yarn.
npm run test -- -u [file_path]
//for particular file (thumps up to echo's answer)
If you are running a project with Lerna monorepo,
You probably want to add a new script to your package's package.json file:
{
// ...
"scripts": {
// ...
"test:update:snapshot": "jest --updateSnapshot"
// ...
}
// ...
}
So you can run
npx lerna run test:update:snapshot
Or you can just enter the package and run
npm run test -- -u
Basically -- tells your command the argument -u is for its child command.

Npm - update and save both deps and depsDev in 1 command line?

I want to update multiple npm dependecies and save them to the respective package.json dependency slot.
My package.json:
{
"dependencies": {
"gulp": "^3.0.0"
},
"devDependencies": {
"gulp-eslint": "^2.8.0"
}
}
So i want to run:
$ npm update gulp gulp-eslint
That's ok but how to save the newer versions both for gulp (dep) and gulp-eslint (devDep) respectively ?
I tried:
$ npm update gulp gulp-eslint --save
but gulp-eslint is devDependency actually must be saved there, how to do all this in 1 command line?
You can check the official documentation for npm-update and notice that there are different specifications according to the npm version you are using.
From the command line you can do:
$ npm update --save --dev
Note: Use sudo if your are on Linux or Mac.
You can also use Yarn, which is a new package manager, with this command:
yarn upgrade
https://yarnpkg.com/en/docs/cli/upgrade

babel-cli issues with npm

I am using babel to build a part of my library and I'm running into issues when i run babel commands through npm.
I have an npm script called build that calls:
{
"prebuild": "rm -rf && mkdir dist",
"build": "babel src/index.js -o dist/index.js"
}
I have run the actual babel command itself in my command line and it works.
However when I do npm run build from my command line it says
The CLI has been moved into the package 'babel-cli'
npm also says that it is that specific line that is failing.
I have already tried the following:
Restart my terminal.
Install babel v5 because I read that v6 split a lot of its functionality.
npm rebuild.
Delete my node_modules and npm install
Any other ideas? as to why npm fails at running this command?
Install babel#5 globally (npm install babel#5 --global) as well as locally: npm install babel#5 --save-dev

Categories

Resources