create-jest-runner icon indicating copy to clipboard operation
create-jest-runner copied to clipboard

Support returning array of TestResult

Open tunnckoCore opened this issue 6 years ago • 0 comments
trafficstars

The change is pretty small and useful.

The use case is that I have a runner that uses cosmiconfig to get some configuration, which also can be an array, so I basically return something like

Promise.all(
  configs.map((cfg) => {
    /* runner thing */
  })
)

Currently, we need to return an object only, something like

const results = await Promise.all(
  configs.map((cfg) => {
    /* runner thing */
  })
)

return results[0];

But this way it won't show/report in the terminal all the test results.

The only change needed is this:

https://github.com/jest-community/create-jest-runner/blob/e17075b804f130acb73f5dfc45fe88e3227caa2b/lib/createJestRunner.js#L142-L148

to something like

const runAllTests = Promise.all(
  tests.map(test =>
    runTestInWorker(test)
      .then(testResult => {
        if (Array.isArray(testResult)) {
          testResult.forEach(result =>
            result.errorMessage && result.stats.failures > 0
              ? onError(new Error(result.errorMessage), test)
              : onResult(test, result),
          );
          return;
        }
        onResult(test, testResult);
      })
      .catch(err => onError(err, test)),
  ),
);

Tested. It is working and shows all the tests.


$ jest -c jest.build.config.js
 PASS   build  @tunnckocore/utils/dist/main/index.js
 PASS   build  @tunnckocore/utils/dist/module/index.js
 PASS   build  @tunnckocore/execa/dist/main/index.js
 PASS   build  @tunnckocore/execa/dist/module/index.js

previously was showing only

 PASS   build  @tunnckocore/utils/dist/main/index.js
 PASS   build  @tunnckocore/execa/dist/main/index.js

tunnckoCore avatar Sep 17 '19 20:09 tunnckoCore