I have a project created using Create-React-App. I am looking to add in a precommit
hook to run our linter and tests with the pre-commit
package.
"pre-commit": [ "precommit-msg", "lint", "test" ],
However, since the test script runs by default in watch mode, this prevents the commit from ever actually happening. How can add the tests not in watch move in the pre-commit?
If you are concerned with the size of it, you do not need to run create-react-app every time. You can make a react project yourself quite easily and by doing so you have much more control and understanding of your project. I recommend looking up how to set up a react project from scratch using the MERN stack.
setupTests. js file is needed. Like Jest and React Testing Library, Jest-DOM is installed with create-react-app. The setupTests. js file is importing the library into our app and giving us access to the matchers. The matcher that was used in our example test was the toBeInTheDocument() method.
You can use the --watchAll=false parameter. So for example you can create another script like this:
"scripts": { "test:nowatch": "react-scripts test --watchAll=false", }
And then run
"pre-commit": [ "precommit-msg", "lint", "test:nowatch" ],
I found a solution for my setup by adding the following script in my package.json
file.
"test:nowatch": "CI=true react-scripts-ts test --env=jsdom",
"pre-commit": [ "precommit-msg", "lint", "test:nowatch" ],
This came from the following thread: https://github.com/facebook/create-react-app/issues/2336
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With