[SalesForce] Any way to use sfdx force:apex:test:report to poll

To work around another problem (EAI_AGAIN errors when pushing or running unit tests in a newly created scratch org from Jenkins CI using SFDX), I'd like to (gently) poll for the test results produced by sfdx force:apex:test:run to see if that helps. But the following command does not appear to be designed for that purpose.

The closest I can get is:

sfdx force:apex:test:report --testrunid 707K000001JUhgY --json --wait 2

but the result of that isn't JSON but rather the message:

Socket timeout occurred while listening for results.

and the exit status is 1, just as it is for other error conditions.

Is there an alternate, clean way, to poll say once every 30 seconds?

(Unanswered here
Waiting for Test Report from Salesforce DX CLI
.)

Best Answer

Here's my suggestion:

sfdx force:data:soql:query can run a Tooling API query with -t. Run something like

sfdx force:data:soql:query -t -q "SELECT Status FROM ApexTestRunResult WHERE Id = '$TEST_RUN_ID'" --json

Check the Status value, and sleep 30s if it's not 'Completed', iterating until that Status is reached.

Related Topic