mocha running with NPM test but not regular mocha CLI command - node.js

I am trying to understand what I am doing wrong in this instance. I have a Node.js project with the following in my package.json
"scripts": {
"test": "mocha --recursive ./src/setup/*.js ./test/**/*.js"
},
"dependencies": {
"mocha": "^2.2.5"
}
When I run 'npm test' the mocha tests are run correctly:
$ npm test (successful run)
However when I try to just run the mocha command I have there in my package.json
$ mocha --recursive ./src/setup/*.js ./test/**/*.js"
This errors with:
-sh: mocha: command not found
I do not have mocha globally installed, I only have installed it via npm to this specific project.
If I install mocha globally then it works. Why doesn't it work when I have simply have mocha installed in the current directory's node_modules, yet it does with 'npm test'?

npm scripts automatically add mocha to the PATH:
If you depend on modules that define executable scripts, like test suites, then those executables will be added to the PATH for executing the scripts.
https://docs.npmjs.com/misc/scripts#path

Related

Errors installing jest in console [duplicate]

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",
...
}
}

Run npm scripts using local deps

Currently I run npm scripts using local deps this way:
package.json:
"scripts": {
"test": "node ./node_modules/karma/bin/karma start",
"node-test": "node ./node_modules/jasmine/bin/jasmine",
"build": "node ./node_modules/gulp/bin/gulp build"
},
I don't want to use global deps, since I can forgot to add deps to the package.json. This way when a local dep is missing, then I got an error message and I don't have problems because some deps are not installed globally, e.g. karma plugins.
Is there a better (shorter) way to define npm scripts using the local libs? Is this travis compatible?
edit:
If it wasn't obvious I have the same libs installed globally, but I want to use the local installs by these projects. That means when I start karma with karma start then the globally installed version will start the karma server, which means that if I don't have all of the karma plugins globally installed, then I got error.
Another problem that I have windows, so the solutions described here: How to use package installed locally in node_modules? do not work. Windows does not recognize the #!/bin/sh and the #!/usr/bin/env node head sections and there is no sh command as far as I can tell. At least not in webstorm terminal. Git bash has the sh command, but I want to run these npm scripts from webstorm terminal.
One possible solution could be to fix somehow webstorm so it could use sh from terminal. After that I could use $(npm bin) I assume. But that's just a guess. I am not sure whether this can be done.
npm automatically puts prepends the path ./node_modules/.bin to your PATH env before it executes commands run by using npm run (including the two "magic" shortcuts npm start and npm test)
npm scripts docs
You can just set this up with:
"scripts": {
"test": "karma start",
"node-test": "jasmine",
"build": "gulp build"
}
Assuming that you have karma, jasmine and gulp-cli listed in either your devDependencies or dependencies (so that they're install when doing npm install)
And yes, it is travis-compatible. Here is an example of a package that is tested on travis using tap which is installed locally as a module:
https://github.com/scriptoLLC/couchdown/

How to run mocha using sudo on Travis

I'm trying to create raw sockets in node, but I'm having trouble running mocha with sudo.
Calling mocha in either package.json or .travis.yml works (with the permissions error for the socket)
Trying to call sudo mocha in either file gets me a file not found error.
$ sudo mocha
sudo: mocha: command not found
The command "sudo mocha" exited with 1.
I do have sudo: true
Try adding the mocha call to your package.json scripts like this:
"scripts": {
"start": // do something
"test": "mocha"
}
Then set it up on travis.yml with:
script: "sudo $(which npm) test"
More help here: https://github.com/travis-ci/travis-ci/issues/1305

How does "npm" run "npm test"?

I always thought that npm test command just launches what I would write in package.json inside scripts: { test: ...} section. But I have this weird bug when it doesn't work.
So, I have this piece of config in package.json
"scripts": {
"start": "node index.js",
"test": "mocha tests/spec.js"
}
When I try to run tests I type npm test in terminal and had this error:
module.js:340
throw err;
^
Error: Cannot find module 'commander'
But everything is OK when I type just mocha tests/spec.js. Any ideas why is that?
UPDATE:
I've tried to install commander and I had an error Cannot find module 'glob'. After installing glob I have
Error: Cannot find module '../'**
But actually question is why do I have these errors and why is everything OK when running mocha tests/spec.js?
You may have two versions of mocha installed: one globally (npm install -g mocha) and one locally, which appears to be broken.
When you run a script through npm, either as npm run-script <name> or with a defined shortcut like npm test or npm start, your current package directory's bin directory is placed at the front of your path. For your package that's probably ./node_modules/.bin/, which contains a link to your package's mocha executable script.
You can probably fix this by removing the local mocha and reinstalling it with --save-dev:
rm -rf node_modules/mocha
npm install --save-dev mocha
That should get you a working local copy of mocha with all its dependencies (commander etc.) installed.

How to run the make command for mocha test for node.js application

I have created a folder : MyFirstNodeAppl and within that, I created a test folder which contains all the tests that need to run using the mocha framework.
I also created a make file named as MakeFile and the content details as mentioned below:
test:
#./node_modules/.bin/mocha -u tdd
.PHONY: test
package.json is in the same level as test and MakeFile with the content as mentioned below:
{
"name": "nockmarket",
"version": "0.0.1",
"private": true,
"dependencies": {
"jquery" : "1.7.3",
"mocha": "1.3.0",
"should": "1.0.0"
}
}
After setting up the above things, I ran the npm install command using the node command prompt window. Now as I know that Mocha uses make to run the test harness, And for windows users we need Cygwin so I installed it in my system. But I am not able to run the make command in order to test the unit test cases.
Can anyone help me to resolve this issue by providing guidance to me in the above.
Folder structure details :
The easiest way to run mocha on your tests is to execute:
npm install -g mocha
Which should make the mocha binary available on your path. Then, from cygwin, just run mocha from the root directory of your project. That will cause it to execute all the tests in *.js files under the test directory.
Install Mocha module in your app or globally:
npm install --save mocha
its saves mocha dependency in your package.json file.
npm puts aliases to all the binaries in your dependencies on that special folder. Finally, npm will add node_modules/.bin to the PATH automatically when running an npm script, so in your package.json you can do just:
"scripts": {
"test": "mocha"
}
and invoke it with
npm test

Resources