Skip to content

Instantly share code, notes, and snippets.

@MichaelDimmitt
Last active July 8, 2024 05:54
Show Gist options
  • Save MichaelDimmitt/5b82abc956974fc5b3f9ea2bc8b3159c to your computer and use it in GitHub Desktop.
Save MichaelDimmitt/5b82abc956974fc5b3f9ea2bc8b3159c to your computer and use it in GitHub Desktop.
bash helpers for jest

Identify what jest files have failing tests:

yarn test --maxWorkers=50% 2>&1 | grep FAIL | sort -u

How to find tests that take longer than 400ms

yarn test --testTimeout=400 2>&1 | grep FAIL

Some of these tests may take some time as they have user interaction.

pro tip:

  1. check if there are setTimeouts that can be replaced with jest fake timers.
  2. or network requests that can be mocked with whatwg-fetch

Identify which test is causing the act(...) warning

yarn test --maxWorkers=50% 2>&1 | grep 'act((' -B 8 | grep 'PASS\|FAIL'

explain:

  1. grep 'act((' , search for the nearest unique identifier of the warning to test name.
  2. -B 8, means grap 8 lines before. explanation link: stackoverflow, grep lines before/after
  3. grep PASS , shrink down to only show the test file-names that are passing tests.

results:

PASS yourapp/file/file1.test.tsx 
PASS yourapp/file/file2.test.tsx 
PASS yourapp/file/file3.test.tsx 
PASS yourapp/file/file4.test.tsx 

You can change to FAIL to see failing tests.

Send all of the files you just found to vscode.

(warning), try first without | xargs code , to make sure it is still grabbing the file names at this point. You can debug these commands and adjust them by removing the pipes | to get more info and making adjustments as you add the pipes back.

<previous command> | grep 'PASS\|FAIL' | awk '{print $2}' | xargs code

example without xargs:

yarn test --maxWorkers=50% 2>&1 | grep 'act((' -B 8 | grep 'PASS\|FAIL' | awk '{print $2}'

example:

yarn test --maxWorkers=50% 2>&1 | grep 'act((' -B 8 | grep 'PASS\|FAIL' | awk '{print $2}' | xargs code

System Info

Macintosh Computer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment