Skip to content

Instantly share code, notes, and snippets.

@Rendez
Last active May 13, 2019 18:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rendez/03e73dd75eab1475befa383715245a37 to your computer and use it in GitHub Desktop.
Save Rendez/03e73dd75eab1475befa383715245a37 to your computer and use it in GitHub Desktop.
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
@Rendez
Copy link
Author

Rendez commented May 2, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment