How to make a dynamic path on a package.json script - testing

On my package.json I have this script
"test": "hardhat test".
I would like to make another one to only run tests from a subfolder, and not the entire test suite.
Something like "test:single": "hardhat test ./tests/subfolder/${runThisOne}" and call it like yarn test:single coolTest which would result in yarn test:single ./tests/subfolder/coolTest. Is this possible?

For Npm use this,
"scripts":{
"test": "hardhat test",
"sale": "hardhat test test/Sale.js" // here test will run sale.js
}

Related

How to run shell scripts cross-platform, using npm

I have nestJS project with that line in package.json:
"scripts": {
...
"test": "sh scripts/test.sh",
...
},
And on my windows machine, it can't run.
How to rewrite it, to run cross-platform? May be example using cross-env-shell or add some package in dependencies?

WebdriverIO how to specify the test to run from npm command

I am using WebdriverIO.
I start my tests from the command line using 'npm test', which triggers the command: wdio wdio.conf.js as specified in my package.json here:
"scripts": {
"test": "wdio wdio.conf.js"
},
What I want to do is specify the actual test to run like this:
wdio wdio.conf.js --spec ./test/specs/e2e/login.js
My question is, how do I pass in the testcase from the npm command, i.e. how can I pass this --spec ./test/specs/e2e/login.js from the npm command into the wdio command?
You can do something like this.
npm run test -- --spec ./test/specs/e2e/login.js
Check this out. https://stackoverflow.com/a/14404223/3879644

When running npm test, is it necessary to place "CI=true" in the beginning of the test command?

My package.json contains
"scripts": {
"test": "CI=true react-scripts test --env=jsdom"
}
What is the difference if I rewrite the code as
"scripts": {
"test": "react-scripts test --env=jsdom CI=true"
}
Will the unit test fail when built?
The CI=true at the beginning of the line is a way to set environment variables only for the command on the same line.
If you run this on the CLI, this would also work:
export CI=true
react-scripts test
But all following commands will also have the "CI" environment variable set.
To avoid this, you can write it in the same line:
CI=true react-scripts test
But if you put the CI=true at the end, it will be passed to the react-scripts command as a parameter, which is most likely ignored.
In all these examples, react-scripts is the command which is executed, even when it's not the first word in the line!

Setting argv in the package.json and running a different script

I have two versions of my application, for one I set --extended, and for the other not, like this
"scripts": {
"build": "webpack --mode production",
"extended": "webpack --mode production --extended",
// ...
}
Now, in my webpack, I access the extended like this
module.exports = (_env,argv)=> {
argv.extended
}
I am trying to improve this in a cross platform way to do something like
"scripts": {
"build": "webpack --mode production",
"extended": "--extended npm run build"
}
as to run the build script from extended but still access the --extended variable.
I there a way to achieve this? Thank you
I read this whole question How to set environment variables from within package.json but can't find a way
Change the scripts section of your package.json to the following:
"scripts": {
"build": "webpack --mode production",
"extended": "npm run build -- --extended"
}
Explanation:
As stated in the npm-run-script documentation which can be found here:
... The special option -- is used by getopt to delimit the end of the options. npm will pass all the arguments after the -- directly to your script:
So, essentially when you run the following command via your CLI:
$ npm run extended
npm invokes the extended script, which then runs the build script and passes the --extended argument to the end of it (i.e. it passes --extended to the end of the build script).
Is there another way?
Yes, you could also consider simplifying the scripts section of your package.json further by deleting your extended script completely.
For instance:
"scripts": {
"build": "webpack --mode production"
}
Then you can do either of the following:
Run the following command via your CLI:
$ npm run build
This will to invoke your build script without the --extended argument/option.
Or run the following command via your CLI instead:
$ npm run build -- --extended
This will invoke your build script with the --extended argument/option.

Jest tests broken after implementing detox

I'd like to be able to run my detox tests and my Jest unit tests separately. For example, run detox tests with detox build && detox test, and my Jest unit tests with npm test.
After implementing detox (using mocha as the test runner), running npm test results in immediate error, and looks like its trying to run my detox tests (not what I'd expect)! Here's the first error I get.
FAIL e2e/auth.spec.js
Not sure why its trying to run detox tests, when my package.json is pointing the test script to Jest.
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
}
How do I run my jest tests now?
By default jest runs all files in your project directory, that have the .test. or .spec. extension to them. That's why it picks up your detox test files and fails to execute them.
https://facebook.github.io/jest/docs/en/configuration.html#testmatch-array-string
You have to override this default behavior in order for the two not to clash. Here's what we use in our package.json just for reference, you might want to change it:
"jest": {
"testMatch": [
"<rootDir>/__tests__/**/*.test.js?(x)",
"<rootDir>/src/**/*.test.js"
]
}
If you dont keep you files in on folder like tests just add this to package.json
"jest": {
"testMatch": ["**/*+(.test.js)"]
}