jest updateSnapshot for specific test file - reactjs

I'm trying to figure out how o update a single snapshot file. In the docs it says just add the -t and I presume the file name but thats not working for me.
Example, in terminal I use.
jest -u -t test/js/tests/components/myTestApp.test.js
Any ideas why this would not work. I also added it as a string.

According to doc https://facebook.github.io/jest/docs/en/cli.html -t option is searching not for file name, but for specific spec name. They provided an example
jest -u -t="ColorPicker"
where "ColorPicker" is name of the spec, given in describe or test blocks.

In order to update particular snapshot, you can pass name as an argument to your script test command.
In package.json
"test": "jest --verbose"
In command line
npm test "<spec name>" -- -u
OR
npm test "<spec name>" -- --updateSnapshot

this works
jest /path/to/file --updateSnapshot

If you run Jest via npm test, you can use arguments by inserting a -- between npm test and the Jest arguments - path & snapshot flag:
npm test -- path/to/file -u

Related

" 'env' is not recognized as an internal or external command, " in Next.js

I was working in a project which I cloned from a GitLab repo, while I was running it locally it gave me this error
npm run dev
> ideeza-web-app#1.0.0 dev
> env TAILWIND_MODE=watch next -p 3200
'env' is not recognized as an internal or external command,
operable program or batch file.
I was expecting it to run after installing dotnev,
I installed dotnev and did an " npm update " too, however still, it's not working.
Try installing cross-env with
npm install --save-dev cross-env
and then in package.json change env to cross-env, as suggested here. There's also the win-node-env module but I don't think it knows about that environment variable.
After changing that, the line in package.json should be:
"dev": "cross-env TAILWIND_MODE=watch next -p 3200",
If you need to handle signals in your program use cross-env-shell command (also provided by cross-env module) instead of cross-env.

How to hide variables in package.json

I have the following scripts in my package.json file
"docker-build": "docker build -t mouchin/my-image-name .",
"docker-push": "docker push mouchin/my-image-name:latest",
"deploy-server": "ssh root#myserverip 'docker pull mouchin/my-image-name:latest'",
"deploy": "npm run docker-build && npm run docker-push && npm run deploy-server"
Problem is that i want to hide
mouchin/my-image-name and root#myserverip
Using some sort of env, maybe saving my variables in .env.prod , but i dont know if i can read the variables saved there directly into package.json
You can use environment variables in your rpm scripts just as you would if you execute the command on the command line (for example $SSH_HOST). However those variables will need to be set directly in the shell that executes the nom script.
Now in order to get the environment variables from an env file loaded, you have to do so manually. For example using a snippet like this:
if [ ! -f .env ]
then
export $(cat .env | xargs)
fi
Source
To execute this before any other script, you could use the built-in lifecylce scripts of npm.
Perhaps, you also want to change the snippet code to load one or the other .env file in case you have one for production and one for development. You will probably be able to use the environment variable NODE_ENV for this, as it is used in most setups, however this last step really depends on your build setup.

configuring package.json on windows

I'm trying to manage the configuration of a react project on windows, it was previously running on mac. I'm using yarn build. inside the package.json scripts>build was configured as "rm-rf deployment/static;react-scripts build && mv build deployment/static". since the rm-rf and mv commands are for Linux, I tried using rmdir/del and move instead.. but it doesn't seem to work. I'm getting the error: Parameter format not correct - "static".
Windows Solution (cmd.exe)
The equivalent of that build script running via Command Prompt or PowerShell on Windows is:
"scripts": {
"build": "rd /s/q \"deployment/static\" 2> nul & react-scripts build && md \"deployment/static\" && move \"build\" \"deployment/static\""
}
Explanation:
The rm-rf equivalent for Windows (cmd.exe) is rd /s/q
The mv equivalent for Windows (cmd.exe) is move
All directory paths have been wrapped in escaped double quotes \"...\". For example;
deployment/static has been rewritten as \"deployment/static\".
Although escaped double quotes are not entirely necessary in this scenario, it's good practice and necessary to do this when paths may include spaces or punctuation characters.
The semi-colon ; has been replaced with the single & operator to ensure the react-script build part runs regardless of whether the initial rd /s/q ... command fails or succeeds.
The following error message would be printed to the console when using rd to delete a folder/path which may not exist:
The system cannot find the path specified
To prevent this error message from potentially being printed to the console we redirect the error message to NUL using the 2> nul part.
The md \"deployment/static\" part utilizes Windows md command to make the static directory - which is very similar to the mkdir command in bash.
Note: The above syntax will fail on nix based operating systems such as macOS and Linux.
Cross Platform Solution (Windows/Linux/macOS...)
To achieve a cross platform solution, (i.e. one which runs successfully on Windows, Linux, and macOS), I suggest writing two Nodejs utility scripts to substitute the rm -rf and mv bash commands. These two Nodejs scripts can then be invoked via your npm-script.
The following steps describe how this can be achieved.
Install shelljs which provides portable Unix shell commands for Nodejs. To do this, cd to your project directory an run the following command:
npm i -D shelljs
Create a new Nodejs script named rm.js with the following content:
rm.js
const shell = require('shelljs');
const args = process.argv.slice(2);
const dir = args[0];
shell.rm('-rf', dir);
Save this file in the root of your project directory, at the same level as where your projects package.json is stored.
Create a another Nodejs script named mv.js with the following content:
mv.js
const shell = require('shelljs');
const args = process.argv.slice(2);
const src = args[0];
const dest = args[1];
// Check src path has been provided and is valid
if (!src || !shell.test('-d', src)) {
console.log('\x1b[31m\x1b[40mERR!\x1b[0m src path cannot be found: %s', src);
process.exit(1);
}
// Check dest path has been provided.
if (!dest) {
console.log('\x1b[31m\x1b[40mERR!\x1b[0m dest path must be provided:');
process.exit(1);
}
// Make dest directory if necessary.
shell.mkdir('-p', dest);
// Move the file.
shell.mv(src, dest);
Also save this file in the root of your project directory, at the same level as where your projects package.json is stored.
Then configure your build script in package.json as follows:
"scripts": {
"build": "node rm \"deployment/static\" & react-scripts build && node mv \"build\" \"deployment/static\""
}
Note
The two utility scripts rm.js and mv.js are invoked in the npm-script named build via the parts reading; node rm ... and node mv ... respectively.
If you decide to store these two scripts in a different folder instead of the projects root directory, (as suggested in steps 2 and 3 previously), then you'll need to change the paths to the files. For example; if they were both saved in a folder named scripts which is located in the root of your project directory then your build script would be changed to:
"scripts": {
"build": "node scripts/rm \"deployment/static\" & react-scripts build && node scripts/mv \"build\" \"deployment/static\""
}
Edit / Update:
An alternative cross-platform solution, (which wasn't available when originally posting this answer), is to utilize the shx package, which is described as:
shx is a wrapper around ShellJS Unix commands, providing an easy solution for simple Unix-like, cross-platform commands in npm package scripts
Run the following command to install shx:
npm i -D shx
Then change your build script in package.json as follows:
"scripts": {
"build": "shx rm -rf deployment/static & react-scripts build && shx mv build deployment/static"
}
I use rimraf for this very reason.
Install it globally
npm i -g rimraf
and update your script as follows
"rimraf deployment/static;react-scripts build && mv build deployment/static"
The shx package should work very well; they even show rm -rf in the topmost package.json demo
Some of my coworkers use rimraf which is specifically the rm -rf unix command for node
for Windows:
in client folder
},
"scripts": {
"build-win": "react-scripts build && move build ..\\server\\public",
},
that will move folder to server folder

angular-jsdoc with npm

Using angular-jsdoc I am using this command to generate my docs
node .\node_modules\jsdoc\jsdoc.js app -c .\node_modules\angular-jsdoc\common\conf.json -d docs -t .\node_modules\angular-jsdoc\angular-template\
I don't prefer typing such long command every-time I update my doc.
I would like to know some shortcut so that I can avoid typing or copy/pasting this command.
As the command indicates node is being used for execution. I would suggest using npm 's functionality to create project based commands.
This can be done as follows:
Create a package.json file in your master directory (using npm init).
Add the below json object in package.json file
"scripts": {
"gendoc":"node .\node_modules\jsdoc\jsdoc.js app -c .\node_modules\angular-jsdoc\common\conf.json -d docs -t .\node_modules\angular-jsdoc\angular-template\"
}
This will create a gendoc command for your project which can be executed from the command line as follows:
npm run gendoc
Now you don't need to type entire command, just enter the above command, npm will search for the scripts object in package.json and execute the script whose key is gendoc.

How to go from codepen to running locally on pc

I would like to run the following codepen on my PC?
http://codepen.io/kyleledbetter/pen/gbQOaV
I have nodejs installed, and have exported the codepen as a zip, extracted in -
but how do i actually run it?
I have tried 'http-server ./d' ? and 'npm run serve'. Sorry this is a newb question, but I can't find documentation on web for it, or in angularmaterialwebsite.
First install http-server package npmjs.com using following command
$ npm install http-server -g
then on you run a http-server pointing to your extracted folder using following command
$ http-server path/to/your/extracted/folder -p 8989

Resources