How to update snapshot with Jest and vue-cli - javascript

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.

Related

After I installed nodemon by "npm i nodemon" , when I tried to run it by "nodemon server.js" , it gives me this error. What should I do?

Screenshot form VS code terminalnodemon : The term 'nodemon' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the
path is correct and try again.
I just meet that question.
First, you should make sure that you had installed nodemon in a global way (npm install -g nodemon), and make sure global package dir is in the environment variables.
Secondly, you should RESTART VScode if you are opening it now.
I spent lots of time to make sure the previous one, but it still fails, then when I restart VScode, everything is fine!
Try to install nodemon globally:
https://github.com/remy/nodemon
npm install -g nodemon
and edit Your package.json for example like that:
"scripts": {
"start": "node server",
"dev": "nodemon server"
},
then in terminal enter the command =>
npm run dev
It should works for You now ;-)
Good Luck and Best regards !
Below are some solutions
This is how you can install nodemon
npm install -g nodemon
after that you have to run this command: npm run dev
here dev is (script) server name like
{
"main": "app.js",
"scripts": {
"dev": "nodemon app.js"
},
}
after that if you are facing error like
" 'nodemon' is not recognized as an internal or external command,
operable program or batch file. "
Then after install nodemon globally write below command:
npm config get prefix
in output you will get PATH and then past this path in to the Environment Variables and it solved
Restart the terminal and run this command
nodemon run dev
I hope after that it will work properly :)
I just had to use npx instead of npm
for e.g. - npx nodemon <server.js>

'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 install nodemon Nodejs on macOS? nodemon keeping show : command not found

Currently I already install nodemon with command npm install -g nodemon. And I got Permissions issue, so I do command with sudo npm install -g nodemon and i did it. But when I make "nodeman" command was always show nodemon: command not found.
If for any reasons you are unable to set a Global PATH then under your current project directory, run
npm install nodemon --save-dev
then under "scripts" in your package.json file, add "start": "nodemon app.js" like this -
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
}
then run
npm start
If you need to install nodemon globally on mac OS, try
sudo npm install -g nodemon.
Then you'll have to enter your password. Once the installation is completed successfully, run
nodemon -v
to check nodemon version on terminal.
According to this, Create a new directory to store your global packages. So that there is no permission issue.
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
Edit your .profile or .bash_profile to add the new location to your PATH:
export PATH=~/.npm-global/bin:$PATH
Then install the package without sudo:
npm install -g nodemon
If you want to install global nodemon use SUDO, because if you need to be a global user, you need to be a super user
The other answer is correct but my advice is that it's better to not install packages globally if you can help it, this makes your application self sufficient without relying on the environment and avoids versioning issues between applications.
npm install -D nodemon
You can now execute nodemon from scripts in package.json:
"scripts": {
"start": "nodemon src/index.js"
}
Or you can execute it yourself using npx if you're in that directory from the terminal. npx executes local scripts, e.g. npx nodemon --inspect ./src/index.js 8080
Just run these commands and the error will get solved.
Specially for MAC People:
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
Enter your laptop password
npm install i -g nodemon or npm install -g nodemon
All set .....🎉

Why I am not able to use mocha through nodemon?

I am trying to run test cases using mocha framework. I am trying to run it through nodemon, but getting an error.
I have installed nodemon already through npm install nodemon and similarly with mocha.
How do I run my project through nodemon so that my test cases show the result using mocha framework?
In the Command Prompt, my test cases are not running for whatever I do with nodemon.
Lets assume you have an entry like following in your package.json -
scripts": {
"start:test": "mocha test/ --recursive --exit"
}
To run the mocha test using nodemon please use the following command:
nodemon --exec "npm run start:test"
"test": "nodemon --exec 'mocha -R min'"
then: npm run test

How to run Jasmine tests in watch mode for TypeScript

I have a Node.js app using TypeScript and now I want Jasmine to run tests automatically each time I make changes in .ts files. So I'm just trying to find an appropriate command to be run as npm test in command line or a package that can watch my .ts files compile them on changes and run jasmine. Does anybody know a solution for it?
The easiest way I found is
installing dependencies: npm install --save-dev jasmine-ts nodemon
initializing jasmine: node_modules/.bin/jasmine-ts init
In the package.json:
"scripts": {
"test": "nodemon --ext ts --exec 'jasmine-ts \"src/**/*.spec.ts\"'"
}
Edit: the above solution doesn't work as of the 11th of Apr, 2019. I published a modified working example at https://github.com/erosb/ts-node-jasmine-example
This may be done with two commands launched in separate terminals. Assuming packages are installed in global mode.
First command launches TypeScript compiler in watch mode:
tsc --watch
The second starts nodemon that watches .js files and restarts on changes. Each time it executes jasmine test runner:
nodemon --ext js --exec 'jasmine JASMINE_CONFIG_PATH=jasmine.json'
This solution is fast enough though it also has a drawback of running in two terminals. So it is not ideal but the best I've found so far.
As a result scripts section in package.json looks like:
"scripts": {
/* ... */
"watch": "tsc --watch",
"test": "nodemon --ext js --exec 'jasmine JASMINE_CONFIG_PATH=jasmine.json'",
"devstart": "nodemon ./bin/www"
},
devstart also works in couple with watch restarting server each time .ts files are changed (after they are compiled to .js).
You might consider using jasmine-node. I don't think that jasmine itself has a watch option.
npm i -g jasmine-node
Assuming that your test command in your package.json scripts block is something like this:
"scripts": {
...
"test": "jasmine some-directory-or-glob-pattern"
...
}
Use jasmine-node and add the --autotest and --watch flags to that command:
"scripts": {
...
"test": "jasmine-node --autotest --watch some-directory-or-glob-pattern"
...
}
Previously described methods either did not work, or were slow to compile code. Here is my attempt to solve this, both fast and convenient, works great for me. The only downside is that jasmine would not know which tests are affected by TS recompilation and would run all the tests.
yarn add tsc-watch --dev
yarn run tsc-watch --onSuccess "yarn run jasmine --config=jasmine.json"
NPM version:
npm -i tsc-watch
npm run tsc-watch --onSuccess "npm run jasmine --config=jasmine.json"
In my case I needed to correctly map TS paths. The full command looks like this:
yarn run tsc-watch --onSuccess \
"node -r tsconfig-paths/register node_modules/jasmine/bin/jasmine \
--config=jest/jasmine.json --require=dist/jest/setup.js $targetFile"
jasmine.json
{
"spec_dir": "dist/src",
"spec_files": ["**/*.e2e.js", "**/*.unit.js", "**/*.spec.js", "**/*.test.js"],
"env": {
"random": false
}
}
Just an example, please adjust to your needs.
tsc-watch starts a TypeScript compiler with --watch parameter, with the ability to react to successful compilation and start tests.

Categories

Resources