Documenting running tests in precommit hook
Created by: vivekkalyan
I want a git pre-commit hook that runs test on staged files before commits (similar to this)
This will require access to jest configs such as bail and findRelatedTests.
currently i am running this pre-commit
#!/bin/sh
PASS=true
echo "running tests"
CI=true npm test
if [[ "$?" == 0 ]]; then
echo "\t\033[32mJest Tests Passed\033[0m"
else
echo "\t\033[41mJest Tests Failed\033[0m"
PASS=false
fi
but this results in every single test being run, which takes a long time. i want to effectively run
#!/bin/sh
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep "js$")
if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
PASS=true
echo "running tests"
jest --bail --findRelatedTests $STAGED_FILES
if [[ "$?" == 0 ]]; then
echo "\t\033[32mJest Tests Passed\033[0m"
else
echo "\t\033[41mJest Tests Failed\033[0m"
PASS=false
fi
I believe allowing for this will encourage better practices and am willing to take a stab at it if the maintainers think that it would be useful.
(also open to other solutions that people might use to achieve the same functionality)