question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

🐛 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).

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
rubennortecommented, Oct 23, 2018

@rafeca see #7251 😉

2reactions
rafecacommented, Oct 23, 2018

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Jest
Test files are normally ignored from collecting code coverage. With this option, you can overwrite this behavior and include otherwise ignored files in...
Read more >
Jest passing tests but --covering option not picking up files
You are missing a setting in the jest.config.js, collectCoverage: true module.exports = { preset: "ts-jest", testEnvironment: "node", ...
Read more >
Setting Up a Node & TypeScript Project - Configure Jest
To setup Jest with our project we will need to install three dev dependencies. ... Next we will generate our jest configuration (jest.config.js) ......
Read more >
Configuring code coverage in Jest, the right way
If you're using Jest, here are three options that should always be present in your Jest configuration: collectCoverage; collectCoverageFrom ...
Read more >
Generating code coverage report in Jest
Real world example of code coverage report generation for Jest testing framework through configuration and report analysis.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found