A simple automated workaround is to use a bash script to create-and-run the jest command:
tools/runJest.sh
#!/bin/bash
function main() {
local testDir="test"
local testFiles="$(grep -rl '\.only(' $testDir | grep '\.test\.js$' | xargs)"
if [ -z "$testFiles" ]; then
runJest "--testPathPattern=test/unit"
else
runJest "$testFiles"
fi
}
function runJest() {
local testFiles="$1"
local cmd="jest --logHeapUsage -c jest.config.js $testFiles"
echo "Running cmd: $cmd"
$cmd
}
main "$@"
What it does:
- find all test files that have
.only(
in them
- if there are
.only(
hits, run jest only on the files with hits
- otherwise, run regular jest config
Make sure to add executable permissions to the bash script
chmod +x tools/runJest.sh
Also modify the script's search criteria (folder, filename patterns) to your specific setup.
package.json scripts.test
Instead of
{
"scripts": {
"test": "jest --logHeapUsage --testPathPattern=test/unit -c jest.config.js"
}
}
Do this:
{
"scripts": {
"test": "tools/runJest.sh"
}
}