Skip to content

Instantly share code, notes, and snippets.

@melon-husk
Created July 16, 2026 09:06
Show Gist options
  • Select an option

  • Save melon-husk/6574136a976799bd516b8e76bc021efb to your computer and use it in GitHub Desktop.

Select an option

Save melon-husk/6574136a976799bd516b8e76bc021efb to your computer and use it in GitHub Desktop.
Script used to build the jest test command from git uncommitted changes
#!/bin/bash
# This script is used to build the jest test command from git uncommitted changes
#
# Usage:
# get-test-command # directory-based command (default)
# get-test-command -e # explicit file-based command
# get-test-command --explicit # explicit file-based command
# get-test-command -r # run the command instead of just printing it
# get-test-command --run # run the command instead of just printing it
# Parse flags
explicit=false
run=false
for arg in "$@"; do
case "$arg" in
-e|--explicit)
explicit=true
;;
-r|--run)
run=true
;;
esac
done
files=$(git ls-files --modified && git ls-files --others --exclude-standard)
# cmd_args is used to run the command reliably (no copy-paste spacing issues).
cmd_args=(yarn test)
if [ "$explicit" = true ]; then
# Explicit mode: list each changed file individually.
# Test/spec files become test targets, and every other changed file is used
# as a collectCoverageFrom source so coverage is scoped to exactly what changed.
# for example yarn test helpers/services/appInsights.test.ts --coverage --coverageDirectory='coverage' --collectCoverageFrom='helpers/services/appInsightsClient.ts'
test_targets=()
coverage_targets=()
for file in $files; do
case "$file" in
*.test.*|*.spec.*)
test_targets+=("$file")
;;
*)
coverage_targets+=("$file")
;;
esac
done
cmd_args+=("${test_targets[@]}" --coverage --coverageDirectory=coverage)
for src in "${coverage_targets[@]}"; do
cmd_args+=("--collectCoverageFrom=$src")
done
else
# Directory mode (default): extract unique directory paths
dirs=$(echo "$files" | awk -F/ '{OFS="/"; $NF=""; print $0}' | sort -u)
# Build test command
# for example yarn test components/test-folder1/ components/test-folder2/ --coverage --coverageDirectory='coverage' --collectCoverageFrom='components/test-folder1/**/*.*' --collectCoverageFrom='components/test-folder2/**/*.*'
test_targets=()
coverage_targets=()
for dir in $dirs; do
test_targets+=("$dir")
coverage_targets+=("${dir}**/*.*")
done
cmd_args+=("${test_targets[@]}" --coverage --coverageDirectory=coverage)
for src in "${coverage_targets[@]}"; do
cmd_args+=("--collectCoverageFrom=$src")
done
fi
# Build a copy-paste-friendly string (each argument quoted) for display.
test_command=""
for arg in "${cmd_args[@]}"; do
case "$arg" in
--collectCoverageFrom=*)
test_command="$test_command --collectCoverageFrom='${arg#--collectCoverageFrom=}'"
;;
*)
test_command="$test_command $arg"
;;
esac
done
test_command="${test_command# }"
# Output the test command
echo "$test_command"
# Optionally run the command.
# yarn (via a version manager) is typically only on PATH in the interactive
# login shell, so run through it to load that environment. printf %q keeps each
# argument safely quoted, immune to spacing issues.
if [ "$run" = true ]; then
run_cmd=$(printf '%q ' "${cmd_args[@]}")
exec "${SHELL:-bash}" -ic "$run_cmd"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment