`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:
- Make the serialization of objects a bit smarter in
jest-worker
: it could even usejest-serializer
for it, which handlesRegExp
s (this may affect the performance though). - Do not normalize the
testRegex
config param to an array ofRegExp
objects, but to an array of strings, and generate theRegExp
object on demand when needed (similarly than how it was done before https://github.com/facebook/jest/pull/7209).
Issue Analytics
- State:
- Created 5 years ago
- Comments:15 (9 by maintainers)
Top GitHub 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?