jest: `jest` fails to generate coverage when using the `testRegex` config option

๐Ÿ› Bug Report

jest@24.0.0-alpha.1 fails to generate coverage when using the testRegex config param. This issue has been introduced recently by https://github.com/facebook/jest/pull/7209.

To Reproduce

A very simple end to end test can be created in e2e/__tests__/coverage_report.test.js to reproduce the issue:

test('generates coverage when using the testRegex config param ', () => {
  const {stdout, status} = runJest(DIR, [
    '--no-cache',
    '--testRegex=__tests__',
    '--coverage',
  ]);
  expect(status).toBe(0);
});

Expected behavior

jest should execute correctly when generating coverage with the testRegex config param.

Additional information

This is the stack trace from the error:

ERROR: regex.test is not a function
      STACK: TypeError: regex.test is not a function
          at config.testRegex.config.testRegex.some.regex (~/jest/packages/jest-runtime/build/should_instrument.js:64:42)
          at Array.some (<anonymous>)
          at shouldInstrument (~/packages/jest-runtime/build/should_instrument.js:64:22)
          at Function.shouldInstrument (~/packages/jest-runtime/build/index.js:241:74)
          at exports.default (~/packages/jest-cli/build/generateEmptyCoverage.js:14:51)
          at Object.worker (~/packages/jest-cli/build/reporters/coverage_worker.js:52:80)
          at execFunction (~/packages/jest-worker/build/child.js:146:17)
          at execHelper (~/packages/jest-worker/build/child.js:128:5)
          at execMethod (~/packages/jest-worker/build/child.js:132:5)
          at process.on (~/packages/jest-worker/build/child.js:43:7)

The issue seems to be caused by the fact that the coverage is generated from a worker, so the config object gets serializer when itโ€™s sent to the worker by jest-worker.

jest-worker does not have any special logic to serialize regular expressions, so it defers it to Nodeโ€™s child_process logic, which converts a RegExp into an empty object when serializing and deserializing it.

Potential solutions

A couple of potential solutions that occur to me:

  1. Make the serialization of objects a bit smarter in jest-worker: it could even use jest-serializer for it, which handles RegExps (this may affect the performance though).
  2. Do not normalize the testRegex config param to an array of RegExp objects, but to an array of strings, and generate the RegExp object on demand when needed (similarly than how it was done before https://github.com/facebook/jest/pull/7209).

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (14 by maintainers)

Most upvoted comments

@rafeca see #7251 ๐Ÿ˜‰

As a short-term fix, the easiest thing IMHO would be to implement the second solution that I suggested. It implies very short amount of changes and it will leave the logic as it was before #7209 (but without removing the feature of having an array of regexps).

After we get this fixed (this issue is blocking the deploy of the new version of jest at FB) we can discuss whatโ€™s going to be the best way to pass the configuration to the workers, what do you folks think?

I had similar issues when I replaced Object with Map in the data structures used by the haste map, and I did a custom serialization there (serialization, deserialization). We could do the same here until we find a general solution to this problem.