Skip to content

Instantly share code, notes, and snippets.

@lamchau
Created December 13, 2023 23:13
Show Gist options
  • Save lamchau/0294b8da94a20778baaa283aceed55df to your computer and use it in GitHub Desktop.
Save lamchau/0294b8da94a20778baaa283aceed55df to your computer and use it in GitHub Desktop.
`jest` runner for multiple disparate ts[x] files
#!/usr/bin/env bash
script_name="${0##*/}"
# By default `fzf` uses `find`, try using one of the options listed here
# https://github.com/junegunn/fzf#tips to ignore .gitignore files
check_fzf_default_command() {
if [ -x "$(command -v gfind)" ]; then
export FZF_DEFAULT_COMMAND='gfind . -path "*__tests__*" -iname "*.ts" -o -path "*__tests__*" -iname "*.tsx"'
else
export FZF_DEFAULT_COMMAND='find . -path "*__tests__*" -iname "*.ts" -o -path "*__tests__*" -iname "*.tsx"'
fi
}
fzf_preview_command="head -n 500 {}"
if [ -x "$(command -v bat)" ]; then
fzf_preview_command="bat --style=numbers --color=always {}"
fi
fzf_header="select test file(s):"
while [[ "$#" -gt 0 ]]; do
case $1 in
-h | --help)
echo "usage: $script_name /path/to/test/file1 /path/to/test/file2 ... /path/to/test/filen"
echo "usage: $script_name <use fzf to select files>"
exit 0
;;
-s | --staged)
test_files="$(git diff --name-only --cached | grep -E "\.test\.(ts|tsx)$")"
if [ "${#test_files[@]}" -eq 0 ]; then
echo "WARNING: No staged test files found, launching interactive file selection"
fi
shift
;;
--keys)
fzf_header=$(
cat << EOF
keybindings:
<tab> select ctrl-u clear query ? toggle preview
ctrl-a select all ctrl-b page up ctrl-p preview half page up
ctrl-d deselect all ctrl-f page down ctrl-n preview half page down
ctrl-t toggle selection
EOF
)
shift
;;
*)
test_files=("$@")
shift
;;
esac
done
fzf_select_opts="\
--ansi \
--multi \
--reverse \
--height=65% \
--inline-info \
--bind=?:toggle-preview \
--bind=ctrl-a:select-all \
--bind=ctrl-b:page-up \
--bind=ctrl-d:deselect-all \
--bind=ctrl-f:page-down \
--bind=ctrl-n:preview-half-page-down \
--bind=ctrl-p:preview-half-page-up \
--bind=ctrl-t:toggle-all\
--bind=ctrl-u:clear-query \
--header=\"$fzf_header\" \
--preview=\"$fzf_preview_command\" \
--preview-window=bottom:60% \
"
if [ "${#test_files[@]}" -eq 0 ]; then
if [ -x "$(command -v fzf)" ]; then
check_fzf_default_command
while IFS='' read -r line; do test_files+=("$line"); done < <(eval "fzf $fzf_select_opts")
if [ "${#test_files[@]}" -eq 0 ]; then
exit 0
fi
else
echo "INFO: For an interactive file selection filtering install 'brew install fzf'"
exit 1
fi
fi
if [ "${#test_files[@]}" -ge 0 ]; then
npx jest --collectCoverage=false --watch --verbose=false "${test_files[@]}"
# history depends on shell, show command so you can copy and paste so it can persist
echo "test command:"
echo "npx jest --collectCoverage=false --watch --verbose=false \\"
# print all files on separate lines except the last one
for file in "${test_files[@]::${#test_files[@]}-1}"; do
echo " ${file} \\"
done
echo " ${test_files[-1]}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment