I am planing to make something similar as lodash custom builds. So in general I want to let user write command like:
lodash category=collection,function
Which create custom module just with category i specified
I read few tutorials how to run scripts with npm bin. Just in case I understand something wrong I write it what i think.
So if I have package.json with this part:
"main": "bin/index.js",
"bin": {
"snippet": "bin/index.js"
},
and I npm install -g console should listen for command snippet and when i write it it run the script index.js in folder bin.
This part looks it works correctly for me. When i have something simple in my index.js i.e. console.log('It Works').
In standard situation you want to let user pass parameters to script. So i found out that all parameters should be in variabile process.argv.
The process.argv property returns an array containing the command line
arguments passed when the Node.js process was launched. The first
element will be process.execPath. The second element will be the path
to the JavaScript file being executed. The remaining elements will be
any additional command line arguments.
So i simply console.log it and run script.
If I run script via command snippet -f -a
Output is : [ 'node', 'path/to/file' ]
If i run script via node bin/index.js -f -a
Output is: [ 'node', 'path/to/file', '-f', '-a' ]
I dont understand that, its same script but different output. However I try it looks like when i call script via bin command it never pass parameters.
Is here someone who have experience with this? And advise me what i am doing wrong?
Or alternativly is there some other way how to make this?
Thanks for any advise.
It take a time however I have a solution now so hope it help someone later.
Where was a problem:
I noticed that my windows has default program to run .js file set to NODE.js and because it's default setting of course all .js files are opening without parameter.
So in my case every .js file open with NODE no matter what, I try to changed it to open with something like PSPAD or similar but this basicly open editor instead of execute file.
How did I fix it:
Instead of using executing .js directly with something I make my ./bin/index.js binary file (basicly removed .js suffix)
Added #!/usr/bin/env node on top of index file
Goes to package.json and changed all dependency on ./bin/index.js to ./bin/index
Woala! it works :)
p.s. As I mentioned at start I believe there is an option to run this with .js as well but I wasn't able to find it. So please if anyone will find it let me know.
Thanks
Related
I am familiar with adding individual environment variables through the CLI by entering:
./node_modules/.bin/cypress run -- --env itemToOverride="val"
But let's say I have an entire JSON that can be a valid cypress.env, how do I pass that in via CLI without having to add each item from the JSON individually?
For example, let's say I have env1.json and env2.json which both have valid structure for my test, but I want to be able to run all my tests with env1 then env2? Similar to:
./node_modules/.bin/cypress run -- --env env1.json
For others that stumble here and want a working answer: The easiest solution to this would be to create a config folder in your project directory containing all the environment JSONs you want. I wrote a script to take an environment file's name as an argument when running it. I decided on a bash script, but you could also use an npm script to achieve this. Basically, to run the tests in "env1" you would type on your command line:
./cypress --env=env1
The way I achieved this was by copying the selected JSON from the config folder to the main directory, then renaming it 'cypress.env.json'. This is a setup function in my bash script to get you started:
function doSetup {
echo "Selecting environment variables from $ENV.env.json"
cp ./cypress/config/"$ENV.env.json" ./
mv "$ENV.env.json" "cypress.env.json"
}
It's an obscure problem that does not have very many answers online, but I hope this helps!
I have 3 different test files
1- testfile-1.js
2- testfile-2.js
3- testfiles-3.js
i can change the test file running order using testfiles feature in cypress.config file but i want to change the order runtime not hard coded.
looking for the solution
Regards
Change the file name to something like this in case you don't want to use testFiles: [] in your cypress.config.
01-testfile.spec.js
02-testfile.spec.js
03-testfile.spec.js
You can comma-separate the file names on the command line,
yarn cypress run --spec "testfile-1.js,testfile-2.js,testfiles-3.js"
Not sure if that guarantees the order though.
If no luck with that, it would be possible to guarantee the order by running with a special script (but it's more work).
i found a way
{
"chromeWebSecurity": false,
}
paste this in cypress.json and it will allow the external iframes to work in cypress
I have a main script (publish-all.js) from which I want to invoke the npm publish script of an Angular project, which also has a sub-script (publish.js that does general stuff (creating folders, copying files, moving folders...) after ng build.
I need to pass some environment variables to that second script.
I am using shelljs to run unix-like commands.
I tried using:
npm run publish -- VERSION=${productVersion} DESTDIR=${productDestinationPath}
From publish-all.js where productVersion and productDestinationPath are constants declared above that line, and which invokes the following script from the package.json:
"publish": "ng build --prod && node ./scripts/publish.js"
But the actual command line I get is
ng build --prod && node ./scripts/publish.js "VERSION=value" "DESTDIR=value"
Finally, in my publish.js script I tried getting those variables the following way:
let version = process.env.VERSION;
let destinationPath = process.env.DESTDIR;
But I get undefined values.
What am I doing wrong? Is the a better way of doing all this?
Should I maybe use process.argv instead??
I am using this strategy because it is what I were told to do, but I would like to know if there is a less confusing way.รง
EDIT 2021-07-13
I tried using export (with shelljs, since I am in Windows and using powershell) but I am getting an exception.
I have the following code in publish-all.js now:
shelljs.exec(`export VERSION=${productVersion}`);
shelljs.exec(`export DESTDIR=${productDestinationPath}`);
shelljs.exec('npm run publish');
And in the publish.js script from the ANGULAR project:
version = process.env.VERSION;
destinationPath = process.env.DESTDIR;
Though it does not get to publish.js. It gets stuck in the shelljs.exec('npm run publish'), with the following exception:
I had to hide the project folder because of privacy policies, but it is a subfolder inside the folder where I am executing publish-all.js.
Environmental variables go BEFORE the command. So, instead of passing them after you can add them BEFORE:
VERSION=${productVersion} DESTDIR=${productDestinationPath} npm run publish
Or,
You can export the variables first then run the script:
export VERSION=${productVersion}
export DESTDIR=${productDestinationPath}
npm run publish
I know that this question could be a duplicate of many similar existing questions however, still want to ask more precisely for help in the scenario I want to understand:
I am using this repo in my example and I have following script block in my package.json
I need to pass one parameter from the command line to identify the environment in which I wish to test, for example something like:
-- testEnv=staging
How can I update the following script blcok to accomodate this change.
I have already tried to set different configurations for world parameter like this:
--world-parameters \"{\\\"environment\\\": \\\"Dev\\\"}\"
however it is now confusing to maintain various version of world parameter configs hence looking to use command line to send variable values through.
"scripts": {
"test-chrome": "./node_modules/.bin/cucumber-js.cmd --tags #Run --format json:./testlab/support/reports/report.json",
}
TestCafe allows you to use environment variables, and have a config.json file to store the baseUrl:
So, you could do
export testEnv=staging
npm run test-chrome
Then enter that value as part of your config file.
{
baseUrl: process.env.testEnv
}
Or, if you want a default baseUrl, you could have a helper class that just returns const targetUrl = process.env.testEnv || config.baseUrl.
Starting with version 1.20.0, TestCafe offers a way to specify the base url for test pages in all interfaces:
CLI
Program API runner.run({baseUrl})
Config file
When using #vue/cli-plugin-unit-jest, I am receiving coverage reports each time I run my unit tests, regardless of whether I have the --coverage flag in the execution line or not. I do not want to receive coverage reports on all of my untested files. When searching for an answer online, there are numerous questions about how to turn that feature on, not turn it off. I can't find it in the documentation either.
How do you disable the Coverage on Untested Files feature in Jest?
Disabling coverage similar to enabling it, just prefix the pattern with an ! like so:
{
"collectCoverageFrom": [
"**/*.{js,jsx}",
"!**/node_modules/**",
"!**/folder-with-untested-files/**"
]
}
Or disable coverage all together with "collectCoverage": false.
If that does not work, then you have this params overridden somewhere in your code.
"collectCoverage": false
in jest.config.js
You can also suppress coverage from the command line. The package I'm working with provides a test script, and I was able to pass the collectCoverage option in as a flag. The relative path here works because my test runner is called by npm and that should set the working directory to the root of my project:
npm run test -- path/to/your.spec.js --collectCoverage=false
And the other way around, you can specific a single file to collect coverage from. It'll override any broad-ranging glob you may have already defined in your project's test config files. One reminder, you collect coverage from your source file, not your spec file. And one other reminder, you can list pretty much any file you want in that coverage option, so make sure you get it right:
npm run test -- path/to/your.spec.js --collectCoverageFrom=path/to/your/source/file.js
Package.json
testw": "jest --watch --collectCoverage=false"
watches the test files for change
npm command
npm run testw Yourfilename.js
"collectCoverage": false
in package.json, will disable coverage, collection, As mentioned by #Herman you can also put ! before file pattern in value of property collectCoverageFrom in package.json
In my case, in package.json I have this statement collectCoverage:false and still I was getting errors. Then I realized I also have collectCoverageFrom line and I removed it since I did not need it. After removing the below line it worked as a charm.
"collectCoverageFrom": [
...,
...
]