Getting all files containing a failed test from a jest run

The past 2 days has seen 2 of us at ahead upgrading from react router 5 to version 6 (or remix router v1 as I like to call it, because it is considerably different to version 5).

Once we did all possible preparations while still being on V5, we could either

Of course we underestimated the effort somewhat, even so after 2 days we can conclude that the upgrade was successful. At one point, however, we were looking at 85 failing tests across numerous test files. If you know jest, and have searched around a bit you will find that it is not even that simple to get a simple straightforward list of file names in which failing tests reside.

The following approach is one way to get a list of file names with failed tests after all.

You can run jest such that it will output a json file containing all outcomes of the tests:

Terminal window
jest --json --outputFile=testrun.json

A typical output looks like that:

{
"numFailedTestSuites": 0,
"": "...",
"snapshot": {
"added": 0,
"": "...",
},
"testResults": [
{
"assertionResults": [
{
"ancestorTitles": ["features/stories/StoryRoutes"],
"": "...",
"title": "unmounts story on onmount"
},
],
"name": "/.../StoryRoutes.spec.tsx",
"": "...",
},

Once you have the test, you can download/install the json query tool jq which will help extract the information that is relevant for us. Since I am on OSX I did

Terminal window
brew install jq

jq has many ways to extract, aggregate and even manipulate json from the command line with a fairly elaborate syntax. With that in mind, we can use a query to obtain the relevant info:

Terminal window
cat testrun.json | jq '[.testResults[] | select(.status == "failed") | .name | split("/") | .[-1:]] | flatten'

The above query will

That way you get a simple list that contains the file names of all test files that contain at least one failed test.