Skip to content

Instantly share code, notes, and snippets.

@KennFatt
Last active May 16, 2025 14:27
Show Gist options
  • Save KennFatt/229c4b3c25a9434a1c8930442267af1c to your computer and use it in GitHub Desktop.
Save KennFatt/229c4b3c25a9434a1c8930442267af1c to your computer and use it in GitHub Desktop.
React Testing Library (RTL) and Jest - Run all tests from one file and show the coverage in browser
local jest_bin=./node_modules/jest/bin/jest.js
local node_bin=node
if [[ -f $jest_bin ]]; then
# node node_modules/jest/bin/jest.js Counter.test.tsx --coverage --reporters=jest-junit --watchAll=false --coverageDirectory=reportcoverage -c ./jest.config.ts -t 'Counter'
function dev-jest-coverage() {
if [[ -z $1 ]]; then
echo "Usage: $0 <file_name: string|regex> <test_name: string|regex>"
echo "Example: $0 Counter.test.tsx 'Counter component'"
return
fi
local reportcoverage_dir='reportcoverage'
# test cleanup
rm -rf junit.xml $reportcoverage_dir
# run the jest
$node_bin $jest_bin $1 \
--runInBand \
--coverage \
--reporters=default \
--verbose \
--reporters=jest-junit \
--coverageReporters=html \
--coverageDirectory=$reportcoverage_dir \
--collectCoverageFrom=''
local jest_coverage_report=$reportcoverage_dir/index.html
# open the output in browser
local report_file_path=$(pwd)/$jest_coverage_report
echo "Opening file: $report_file_path in the brower..."
$chromium_bin --app=file://$report_file_path >/dev/null 2>&1 &
disown
}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment