Skip to content

Instantly share code, notes, and snippets.

@mcamiano
Created August 16, 2019 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcamiano/97e1e8797bc70ba2c01a50852d2062bf to your computer and use it in GitHub Desktop.
Save mcamiano/97e1e8797bc70ba2c01a50852d2062bf to your computer and use it in GitHub Desktop.
Jest command line menu
# Usage: stick it in your .bashrc or other startup file and then
# jest list
# jest [--updateSnapshot] {SomeTestNamePrefix}
# jest [--updateSnapshot]
#
# The list argument presents a selection list, from which you may pick one test to execute.
# The second form runs the tests whose name is a leftmost match to the given prefix.
# The unqualified jest cli executes all tests under ./jest/*
# The --updateSnapshot option will update the html snapshot, which is recommended as a basic regression case for each test.
#
function jest() {
clear;
(
watch=""
d=$(pwd)
trap "cd $d" SIGINT SIGTERM EXIT
arg=""
cd $(git rev-parse --show-toplevel)
JEST="./node_modules/jest/bin/jest.js"
if [[ "$1" = "list" ]]
then
shift
opts=$(find $viewhome/jest -name "*.test.js" | while read name ; do echo $(basename ${name%%.test.js}); done; echo "All"; echo "Quit";)
num=$(echo $opts | wc -w)
select opt in $opts; do
if [[ "$REPLY" = "$num" || "$REPLY" = "q" || "$REPLY" = "Q" || "$REPLY" = "Quit" ]]
then
return;
fi
let allnum=($num-1)
if [[ "$REPLY" = $allnum || "$REPLY" = "a" || "$REPLY" = "A" || "$REPLY" = "All" ]]
then
break
fi
arg="$opt";
break
done
fi
if [[ "$1" = "--watch" ]]
then
watch="$1"
shift
fi
snap=""
if [[ "$1" = "--updateSnapshot" ]]
then
snap="$1"
shift
fi
export testfile=${arg:-$1}
files=$( ( cd jest && find . -name "${testfile}*" ) | grep '.test.js$')
arguments=${files:-$1}
$JEST $watch $snap $arguments
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment