Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Create-React-App Tests not in Watch Mode

Tags:

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?

like image 979
Yuschick Avatar asked Mar 07 '18 10:03

Yuschick


People also ask

Is it required to run NPX create react app my app every time when you need to create a react app?

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.

What is setupTests js in react?

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.


2 Answers

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" ], 
like image 191
Boogie Avatar answered Sep 19 '22 05:09

Boogie


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

like image 44
Yuschick Avatar answered Sep 21 '22 05:09

Yuschick