Jest is picking up an old version of a package and thus my tests fail unless I use --no-cache. I can even delete the package folder from folder node_modules and Jest is happy to run the tests (almost all are passing).
So how do I clear the Jest cache?
As of Jest 22.0.0+, you can use the --clearCache option:
Deletes the Jest cache directory and then exits without running tests. Will delete cacheDirectory if the option is passed, or Jest's default cache directory.
Just run:
jest --clearCache
If you have installed Jest as a dependency in your Node.js project and the jest command doesn't work, just create a new script inside your package.json file.
{
...
"scripts:" {
"clear_jest": "jest --clearCache"
}
...
}
And then, run in your terminal:
npm run clear_jest
With modern NPM, you could also run (credits to johny):
npx jest --clearCache
You can find the cache location by running jest --showConfig. Look for the cacheDirectory key. Its value is the name of the folder you'll need to remove.
First, you need to know the Jest version:
yarn jest --version
Jest >= 22.0.0
yarn jest --clearCache
Jest < 22.0.0
yarn jest --showConfig | grep cacheDir
Returns (you need to remove that folder)
"cacheDirectory": "/tmp/jest_rs",
Then, you remove it
rm -rf /tmp/jest_rs
If you don’t use Yarn, do instructions with npx jest.
If using nx monorepo run following command to clear the cache
nx test <packageName> --clearCache
Related
enter image description here
my code is returning the same value even if i change the code
I want to know what happened
Try the following:
Add the rimraf lib:
yarn add rimraf
Then create a new script in the scripts section of the package.json:
"prebuild":"rimraf dist"
I highly recommend you to remove dist file first and build and rerun your project.
rm -rf dist
npm run build
npm run start:dev
I'm not sure you faced the issue while adding a new model or updated the schema of orm module.
If yes, According to this link NestJS won't automatically copy your assets to the dist folder moreover as I described earlier is not compatible with glob static paths such as entities property in TypeOrmModule.
I have to add jest locally to every single project folder I create in order to use it. I can't use the "jest" command on Terminal, but I can do "npm run test".
zsh: command not found: jest
Jest Error is installed, but it's in my ./node_modules directory. People have said in a previous discussion post that I should run the command
./node_modules/jest –updateSnapshot
However, this leads to the error:
zsh: permission denied: ./jest
I would like to run jest on Terminal. Please give some advice.
You could try to run it this way :
./node_modules/.bin/jest
If that doesn't work, try installing jest globally.
npm install -g jest
if you don't want to install jest globally,
(there are a couple of reasons why you do not want to install npm things globally - for example this one),
you can do:
npx jest <all your jest flags or files>
I have just started with a mono repo with multiple packages using lerna.
On all my packages I will have unit tests using mocha. But as it stands, only one of my packages is ready for unit tests.
From my understanding, shouldn't I be able to only install mocha on the root package.json and then run lerna run test in order to run it only in the packages that have available test scripts in their num packages, correct?
Unfortunately as it stands this is not working.
I have mocha installed as a devDependency in the root. However, if I do run lerna run test --scope=myPackage I get the following message:
mocha: command not found
Because it is in fact installed in the root, shouldn't it work for all packages? If it isn't the case, what am I missing here? DO I have to install mocha in all of them after all?
Just remove the test script from package.json wherever the unit tests are not ready and lerna wont call it.
I am new to Node Js and Webpack. I tried to start a project with module-loaders.
Firstly, I installed nodeJs and NPM and created a new directory called tutorial. I used the command prompt to cd into this directory and then ran the following command npm init and then installed webpack via npm using the command below :
npm install -S webpack
The 1st command installed webpack locally into the project under the 'node-modules' directory and I can run my project by doing this:
nodejs node-modules/webpack/bin/webpack.js
The problem with this is that I have to place my webpack.config.js file inside of this directory which I want to place in my project root.
One solution to this problem was to install webpack globally on my machine which I did using the command below :
npm install -g webpack
This installed Webpack and now I do have a Webpack command. However, this command does not seem to be working or doing anything at all. When I try to run this from my project's root directroy it does not do anything at all (See Screenshot)
Please tell me what I am doing wrong!!
webpack is not only in your node-modules/webpack/bin/ directory, it's also linked in node_modules/.bin.
You have the npm bin command to get the folder where npm will install executables.
You can use the scripts property of your package.json to use webpack from this directory which will be exported.
"scripts": {
"scriptName": "webpack --config etc..."
}
For example:
"scripts": {
"build": "webpack --config webpack.config.js"
}
You can then run it with:
npm run build
Or even with arguments:
npm run build -- <args>
This allow you to have you webpack.config.js in the root folder of your project without having webpack globally installed or having your webpack configuration in the node_modules folder.
You can run npx webpack. The npx command, which ships with Node 8.2/npm 5.2.0 or higher, runs the webpack binary (./node_modules/.bin/webpack) of the webpack package.
Source of info: https://webpack.js.org/guides/getting-started/
I had to reinstall webpack to get it working with my local version of webpack, e.g:
$ npm uninstall webpack
$ npm i -D webpack
npm i webpack -g
installs webpack globally on your system, that makes it available in terminal window.
The problem with my setup was webpack was installed but webpack-cli was missing
npm i -g webpack webpack-cli
If you prefer to install locally then install without -g flag
The quickest way, just to get this working is to use the web pack from another location, this will stop you having to install it globally or if npm run webpack fails.
When you install webpack with npm it goes inside the "node_modules\.bin" folder of your project.
in command prompt (as administrator)
go to the location of the project where your webpack.config.js is located.
in command prompt write the following
"C:\Users\..\ProjectName\node_modules\.bin\webpack" --config webpack.config.vendor.js
Installing webpack with -g option installs webpack in a folder in
C:\Users\<.profileusername.>\AppData\Roaming\npm\node_modules
same with webpack-cli and webpack-dev-server
Outside the global node_modules a link is created for webpack to be run from commandline
C:\Users\<.profileusername.>\AppData\Roaming\npm
to make this work locally, I did the following
renamed the webpack folder in global node_modules to _old
installed webpack locally within project
edited the command link webpack.cmd and pointed the webpack.js to look into my local node_modules folder within my application
Problem with this approach is you'd have to maintain links for each project you have. Theres no other way since you are using the command line editor to run webpack command when installing with a -g option.
So if you had proj1, proj2 and proj3 all with their local node_modules and local webpack installed( not using -g when installing), then you'd have to create non-generic link names instead of just webpack.
example here would be to create webpack_proj1.cmd, webpack_proj2.cmd and webpack_proj3.cmd
and in each cmd follow point 2 and 3 above
PS: dont forget to update your package.json with these changes or else you'll get errors as it won't find webpack command
Actually, I have got this error a while ago. There are two ways to make this to work, as per my knowledge.
Server wont update the changes made in the index.js because of some webpack bugs. So, restart your server.
Updating your node.js will be helpful to avoid such problems.
I'm having trouble getting Mocha to work as expected, and I'd love to say as documented, but there (appears) to not be much documentation on actually getting the thing running.
I've installed it using npm (both globally and locally), and each time when I run it I get:
$ mocha
mocha: command not found
Ok, so I figured it's not in my PATH, so I tried running it directly,
$ ./node_modules/mocha/bin/mocha
execvp(): No such file or directory
Finally, I tried hitting the other bin file, and got,
$ ./node_modules/mocha/bin/_mocha
path.existsSync is deprecated. It is now called `fs.existsSync`.
.
✔ 1 tests complete (1ms)
How can I just execute my tests with a single command? Vows seems to let you, but I've heard Mocha is the better choice, I just can't seem to get it working correctly.
And any thoughts on the error I got above in my third attempt?
Edit:
I'm running,
Ubuntu 11.10 64-bit
Node.js 0.7.5
npm 1.1.8
mocha 0.14.1
should 0.6.0
since npm 5.2.0, there's a new command "npx" included with npm that makes this much simpler, if you run:
npx mocha <args>
Note: the optional args are forwarded to the command being executed (mocha in this case)
this will automatically pick the executable "mocha" command from your locally installed mocha (always add it as a dev dependency to ensure the correct one is always used by you and everyone else).
Be careful though that if you didn't install mocha, this command will automatically fetch and use latest version, which is great for some tools (like scaffolders for example), but might not be the most recommendable for certain dependencies where you might want to pin to a specific version.
You can read more on npx here
Now, if instead of invoking mocha directly, you want to define a custom npm script, an alias that might invoke other npm binaries...
you don't want your library tests to fail depending on the machine setup (mocha as global, global mocha version, etc), the way to use the local mocha that works cross-platform is:
node node_modules/.bin/mocha
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
After further reading, and confirmation from Linus G Thiel above, I found I simply had to,
Downgrade to Node.js 0.6.12
And either,
Install Mocha as global
Add ./node_modules/.bin to my PATH
To run Mocha with mocha command from your terminal you need to install mocha globally on this machine:
npm install --global mocha
Then cd to your projectFolder/test and run mocha yourTestFileName.js
If you want to make mocha available inside your package.json as a development dependency:
npm install --save-dev mocha
Then add mocha to your scripts inside package.json.
"scripts": {
"test": "mocha"
},
Then run npm test inside your terminal.
While installing the node modules for mocha I had tried the below commands
npm install
npm install mocha
npm install --save-dev mocha
npm install mocha -g # to install it globally also
and on running or executing the mocha test I was trying
mocha test
npm run test
mocha test test\index.test.js
npm test
but I was getting the below error as:
'Mocha' is not recognized as internal or external command
So , after trying everything it came out to be just set the path to environment variables under the System Variables as:
C:\Program Files\nodejs\
and it worked :)
For windows :
Package.json
"scripts": {
"start": "nodemon app.js",
"test": "mocha"
},
then run the command
npm run test
Late answer but I think will work.
Install mocha globally
npm install --global mocha
If you have already installed mocha then set the path to bin