How to run jest in create react app before commit? I have found couple articles but they are too complicated , without explaining what to do step by step. Could you please explain me how to make it work
You need to use husky package. Here is basic configuration (put it in package.json). It adds pre-commit hook to your git configuration.
"husky": {
"hooks": {
"pre-commit": "CI=true npm run test",
}
}
You can also consider using lint-staged to lint files which you commit. You can see full configuration here.
Maybe you can utilise some npm packages for this.
So, this is not a direct solution but you can include as many things you want instead of using git commands, shells and yml files
Install package Pre-Commit and install git-cz from npm. Using these you can make use of pre-commit and commit in package.json and desired things to them.
Now you can make use of these packages in your packages.json like below
{
"start": "node index.js",
"pre-commit": "lint-staged",
"commit": "git-cz",
"lint": "eslint . --ext .js,.jsx",
}
For example you want to run test cases then pre-commit: npm run test && lint-staged
Because in our project we needed to update documents, checking style-lint, eslint and test cases, so we were using these combination.
But you should not commit directly using git commit -m "message" but with npm run commit.
Hope, this helps.
To do locally you can just write a script which first runs the tests and pipes the exit code and decides whether to really push or not based on the exit code.
By default npm test runs in interactive mode
To exit after running the tests use CI=true
i.e. CI=true npm test
This worked for me:
npm install --save-dev pre-commit
https://www.npmjs.com/package/pre-commit
Then, in package.json:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "CI=true react-scripts test", //<-- update
"eject": "react-scripts eject",
}
Adding CI=true runs all tests without making it interactive
Finally,
"devDependencies": {
"pre-commit": "test"
}
Related
While dealing with a very immature team, I want the react-typescript build to fail when ESLint gives warnings.
src/modules/security/components/ForgotPasswordBox/index.tsx
Line 8:18: 'FormikHelpers' is defined but never used #typescript-eslint/no-unused-vars
src/modules/security/components/OTPVerificationBox/modalverficationcode.tsx
Line 54:18: Expected '!==' and instead saw '!=' eqeqeq
Line 63:29: Expected '===' and instead saw '==' eqeqeq
But on the net I only find the reverse where people would want to turn off warnings or build to continue even with warnings which is the exact opposite of what I'm looking for.
I do not use build pipeline or any such thing. We have a react -typescript app with NPM as package manager.
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"lint": "eslint . --max-warnings=0"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
kindly let me know how can I achieve this behavior?. Please note that typescript compiler settings strict is already set to true.
Instead of having your build script fail, you can either update your CI to run npm run lint && npm run build or create a new script to use in your CI (e.g. "production": "npm run lint && npm run build").
This way the CI build will fail on the lint script before even starting the build script. This exposes the logic, instead of hiding it within the build script.
In the future you could consider running these in parallel and cause either one failing to fail the CI build and/or require a passing GitHub status check (based on success/fail of each script).
Working on an Electron app with React. Right now, to get things started, I run the typical npm start command which runs the react-scripts start script.
This starts the dev server. Once the dev server is started, I open a second terminal window and run a script to start electron npm run start-electron which opens my React app in the Electron window.
This works as expected, but I was curious if there was a way to create a script that would:
Start the dev server
Wait for dev server to be started
Then start electron
I tried setting up a sequential script in package.json but it only starts up the dev server. For example npm run start && npm run start-electron.
This isn't make or break. The two terminal option works fine, just didn't know if this was possible.
Yes it is possible, I use concurrently to do it within my projects
npm i concurrently
and add a new script, let's call it dev for example, then in your scripts:
"dev": "concurrently \"npm run start\" \"npm run start-electron\""
All that remains to do now is npm run dev
I have the exact same situation, and below script work for me (remember to install wait-on)
"scripts": {
"start-reactjs": "PORT=25610 react-scripts start",
"start-electron": "wait-on http://localhost:25610 && electron .",
"start": "npm run start-electron & npm run start-reactjs"
}
you can create a script in the root directory with extension .sh
it could contain all operations for you
npm start
npm run start-electron
The second approach you could create a custom script in package.json
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"customStart":"npm run start && npm run start-electron"
},
to run this script
npm run customStart
You could try start-server-and-test npm module. It has different purpose, but it could work for your scenario.
From documentation:
This command is meant to be used with NPM script commands. If you have
a "start server", and "test" script names for example, you can start
the server, wait for a url to respond, then run tests. When the test
process exits, the server is shut down.
"scripts": {
"start-server": "npm start",
"test": "mocha e2e-spec.js",
"ci": "start-server-and-test start-server http://localhost:8080 test"
}
}
To execute all tests simply run npm run ci.
Another alternative could be concurrently combined with wait-on as #Slim said
From documentation:
wait-on will wait for period of time for a file to stop growing before
triggering availability which is good for monitoring files that are
being built. Likewise wait-on will wait for period of time for other
resources to remain available before triggering success.
I solved it by using concurrently
npm i concurrently
In my package.json
"build": "concurrently \"npm run build-react\" && npm run build-jsx",
I'm using it to build an Adobe extension using react, and I need to bundle my extendscript after the react-scripts build, which would otherwise delete my .jsxbin
npm run build creates production build of the project. How do I create development build? I'm using gradle-node-plugin to build the artifact. The project has standard create-react-app configuration.
This is not really doable with just create-react-app, there is an open issue Here and it doesn't look like it will be added anytime soon.
However, you can use a package called dotenv for that, following are the steps you should take:
Install dotenv (make sure to add save dev) npm install dotenv-cli --save-dev
In package.json scripts section add new script: "build:dev": "dotenv -e .env.development react-scripts build",
And you can build for development with npm run build:dev
PS: if you want to avoid mistakenly deploying dev builds to production (as mentioned here) you can add build:prod to package.json and disable the regular build command, see code:
"build:dev": "dotenv -e .env.development react-scripts build",
"build:prod": "dotenv -e .env.production react-scripts build",
"build": "echo \"Please use build:dev or build:prod \" && exit 1",
Also note that process.env.NODE_ENV will still be production but it'll load your .env.development file
Thanks, #Moses. This is an extension to the answer posted by Moses Schwartz. You can also make the build pick the environment files dynamically by exporting the value of the environment(development, test, production) in the bash shell. And then you don't have to have different build commands for different environments.
You can use this in your package.json
"start": "dotenv -e .env react-scripts start",
"build": "dotenv -e .env.${REACT_APP_ENVIRONMENT} react-scripts build",
So when your run npm start, it will pick the environment values from .env
and when you run npm run build, it will pick the environment values from .env.{REACT_APP_ENVIRONMENT}
To define the REACT_APP_ENVIRONMENT, you can do:
export REACT_APP_ENVIRONMENT="development" or
export REACT_APP_ENVIRONMENT="test" or
export REACT_APP_ENVIRONMENT="production"
Hope this helps. This will help in staging the react application for multiple environments.
Thanks to #Tx_monster comment
github.com/Nargonath/cra-build-watch
A script for create-react-app that writes development builds to the disk
npm install -D cra-build-watch
package.json:
{
"scripts": {
"watch": "cra-build-watch"
}
}
npm run watch
I'm working on a React.js project using node. I usually run the unit tests with npm test to make sure all of them pass, then I create an optimized build using npm run build. The optimized build is minimized and bundled.
Because it's a React.js project, the package.json uses the react-scripts under the hood:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"lint": "standard"
},
I'm wondering if it is possible to run the tests on the optimized build, so when I give this build to a user, the user can also run the tests to make sure everything works on his/her environment.
I'm using an earlier version of node (v8.11.4) and npm (5.6.0).
I am new in Angular. I accidentally ejected my project using command-line, i don't remember which command i used. but when i try to run command
ng serve --open
it throws me an error of
An ejected project cannot use the build command anymore.
I also re-installed angular-cli as said in this Stackoverflow Question but no use.
Can anyone help me with this?
open the angular-cli.json, you should see this at the top
"project": {
"name": "proj-name",
"ejected": true,
}
remove the ejected part and it should work
once you ejected the project you will get a webpack.config file
And also you can see some commands in package.json script tag
"scripts": {
"ng": "ng",
"start": "webpack-dev-server --port=4200",
"build": "webpack",
"test": "karma start ./karma.conf.js",
"lint": "ng lint",
"e2e": "protractor ./protractor.conf.js",
"pree2e": "webdriver-manager update --standalone false --gecko false --quiet"
},
you can still run the project by using npm start
you can still build the project by using npm run build