Skip to content

Support returning array of TestResult #20

Open
@tunnckoCore

Description

@tunnckoCore

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:

const runAllTests = Promise.all(
tests.map(test =>
runTestInWorker(test)
.then(testResult => onResult(test, testResult))
.catch(error => onError(error, test)),
),
);

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions