Last active
May 13, 2019 18:32
-
-
Save Rendez/03e73dd75eab1475befa383715245a37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
checkGitFile() { | |
git ls-files --error-unmatch $1 > /dev/null 2>&1 | |
} | |
# checking for test existence of files using react hooks | |
filesUsingHooks=$(git diff --cached --name-only --diff-filter=ACR | grep -l -E '^import.+\<use(State|Effect|Context|Reducer|Callback|Memo|Ref|ImperativeHandle|LayoutEffect|DebugValue)' --include \*.js --exclude \*.\*.js) | |
if [ -n "$filesUsingHooks" ]; then | |
headline "Checking if your react hooks have tests" | |
for fileUsingHooks in "$filesUsingHooks" | |
do | |
unitTestName=$(basename $(sed 's|\.js|\.spec\.js|' <<< "$fileUsingHooks")) | |
checkGitFile "*/$unitTestName" | |
if [[ $? != 0 ]]; then | |
echo "No test found for $fileUsingHooks" | |
echo "Please add a unit test named $unitTestName" | |
exit 1 | |
fi | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a git pre-commit hook preventing a new .js file from being added without a matching
.spec.js
file with a test case. Useful to introduce React hooks while increasing your test coverage.