Skip to content

Instantly share code, notes, and snippets.

@Phoenix2k
Last active April 23, 2026 13:22
Show Gist options
  • Select an option

  • Save Phoenix2k/dcb1e230d92c269e2e61ea849e5a5101 to your computer and use it in GitHub Desktop.

Select an option

Save Phoenix2k/dcb1e230d92c269e2e61ea849e5a5101 to your computer and use it in GitHub Desktop.
Jest coverage from one or multiple files
# Function: jestCoverage
# Description:
# Runs Jest with coverage enabled. It automatically maps .spec.ts files
# to their corresponding .ts source files for --collectCoverageFrom.
jestCoverage() {
if [[ $# -eq 0 ]]; then
echo "Error: No arguments provided." >&2
echo "Usage: jestCoverage [jest-flags] <file1.spec.ts> [file2.spec.ts ...]" >&2
return 1
fi
local spec_files=()
local ts_files=()
local other_args=()
local base
# Iterate through all arguments to categorize them
for arg in "$@"; do
if [[ "$arg" == *.spec.ts ]]; then
spec_files+=("$arg")
# Derive the source file name (e.g., app.spec.ts -> app.ts)
base="${arg%.spec.ts}"
ts_files+=("${base}.ts")
else
# This captures flags like --watch, --updateSnapshot, or --config
other_args+=("$arg")
fi
done
# Safety check: Ensure we actually found spec files
if [[ ${#spec_files[@]} -eq 0 ]]; then
echo "Error: No .spec.ts files were identified in the arguments." >&2
return 1
fi
# Build a single brace-expansion glob for --collectCoverageFrom.
# e.g. one file: "src/foo/bar.ts" multiple: "{src/foo/bar.ts,src/baz/qux.ts}"
local IFS=','
local coverage_glob
if [[ ${#ts_files[@]} -eq 1 ]]; then
coverage_glob="${ts_files[0]}"
else
coverage_glob="{${ts_files[*]}}"
fi
unset IFS
# Construct command array for safe execution (no eval needed)
# Order: pnpm jest [coverage-flags] [other-args] [test-files]
local cmd=(
"pnpm"
"jest"
"--coverage"
"--collectCoverageFrom" "${coverage_glob}"
"${other_args[@]}"
"${spec_files[@]}"
)
# Execute the command array directly
echo "Executing: ${cmd[*]}"
"${cmd[@]}"
}
alias jestc='jestCoverage'
# Function: jestCoverage
# Description:
# Runs Jest with coverage enabled. It maps .spec.ts files
# to corresponding .ts files for --collectCoverageFrom.
export def jestCoverage [...args: string] {
if ($args | is-empty) {
error make { msg: "No arguments provided. Usage: jestCoverage [jest-flags] <file1.spec.ts> [file2.spec.ts ...]" }
}
mut spec_files = []
mut ts_files = []
mut other_args = []
for arg in $args {
if ($arg | str ends-with ".spec.ts") {
$spec_files = ($spec_files | append $arg)
let base = ($arg | str replace --regex '\.spec\.ts$' '')
$ts_files = ($ts_files | append ($base + ".ts"))
} else {
$other_args = ($other_args | append $arg)
}
}
if (($spec_files | length) == 0) {
error make { msg: "No .spec.ts files were identified in the arguments." }
}
let coverage_glob = if (($ts_files | length) == 1) {
$ts_files.0
} else {
"{" + ($ts_files | str join ",") + "}"
}
let cmd_preview = (
["pnpm" "jest" "--coverage" "--collectCoverageFrom" $coverage_glob]
++ $other_args
++ $spec_files
)
print $"Executing: ($cmd_preview | str join ' ')"
^pnpm jest --coverage --collectCoverageFrom $coverage_glob ...$other_args ...$spec_files
}
export alias jestc = jestCoverage

Jest coverage

Bash/Zsh and Nushell scripts for targeting Jest coverage to one or more files with flag support for watching the files etc.

Bash

How to use it with Bash/Zsh:

  1. Copy the content of the first file to your .bashrc or .zshrc
  2. Open a new terminal
  3. Run jestc src/test-file.spec.ts or jestc --watch src/test-file-1.spec.ts src/test-file-2.spec.ts

Nushell

How to use it with Nushell:

  1. Save the contents of the .nu file to something like scripts/jest-coverage.nu
  2. Load it in Nushell:
use scripts/jest-coverage.nu *
  1. Open a new terminal
  2. Run jestc src/test-file.spec.ts or jestc --watch src/test-file-1.spec.ts src/test-file-2.spec.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment