Skip to content

Instantly share code, notes, and snippets.

@bfmatei
Last active December 23, 2020 11:20
Show Gist options
  • Save bfmatei/02d23941b38555774a05e059d78df9df to your computer and use it in GitHub Desktop.
Save bfmatei/02d23941b38555774a05e059d78df9df to your computer and use it in GitHub Desktop.
Repeat Jest test

The following is a quick and dirty bash script that runs a specific Jest test 10 times and counts how many times it failed and the number of occurences for the same error.

It uses jest-silent-reporter to keep the output clean and only output the errors, so be sure to install it before running the script.

Example output:

Success: 6
Failed: 4
=====
Counter: 3

  ● MyTestName › does smothing bad

    expect(received).toBeInTheDocument()

    received value must be an HTMLElement or an SVGElement.
    Received has value: null
=====
Counter: 1

  ● MyTestName › does smothing bad 2

    expect(received).not.toBeInTheDocument()

    received value must be an HTMLElement or an SVGElement.
    Received has value: null
#!/bin/bash
success=0;
failed=0;
errors=();
errorsCounters=();
for i in {1..10}; do
result=$(jest --noStackTrace --useStderr --reporters=jest-silent-reporter "--testNamePattern=^MyTestName" --runTestsByPath ./path/to/test.tsx 2>&1);
if [ $? = 0 ]; then
success=$((success+1));
else
failed=$((failed+1));
if [[ ! " ${errors[@]} " =~ " ${result} " ]]; then
errors+=("$result");
errorsCounters+=(1);
else
for i in "${!errors[@]}"; do
if [[ "${errors[$i]}" = "${result}" ]]; then
errorsCounters[$i]=$((errorsCounters[$i]+1));
fi
done
fi
fi
done
echo "Success: $success";
echo "Failed: $failed";
for i in "${!errors[@]}"; do
echo "=====";
echo "Counter: ${errorsCounters[$i]}";
printf "%s\n" "${errors[$i]}";
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment