New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ReferenceError: cov_${id} is not defined #499
Comments
This issue was posted in the old organisation (gotwarlost/istanbul#922) by mistake first. There has already been discussion with another community member, @robertleeplummerjr, who created a package (npmjs.com/package/istanbul-spy) that seems to hack a fix in by prepending a definition for each cov_id variable to each generated code string/array. |
I believe #481 fixes this bug. This patch is released to |
Awesome thanks @coreyfarrell We appreciate the quick response! |
See also jestjs/jest#9192 for WIP updating jest to use the new versions of istanbul. |
I believe this issue is resolved through #481, now released to istanbul-lib-instrument 4.0.0. If anyone can reproduce the error with that version please comment and I can reopen this issue. |
Hey @coreyfarrell, the new Jest version with upgraded Istanbul packages has been released, and I've begun attempting to upgrade (which involves un-commenting out all the tests affected by this bug). It seems that this bug has not been fixed in
I think we're going to disable code coverage now, as we'd rather have a functioning test suite than code coverage. Choosing the other path would be rather ironic in my eyes, using a code coverage package that prevents additional tests from being added to the codebase. |
@HugoKawamata Can we re-open this please until it is fixed 👍 |
In my case GPU.js and Brain.js, we're working with |
I appreciate the trouble everyone is having but without a link to a (minimal) reproduction it's unlikely I'll be able to help. |
Reproducing this is (I assume) almost impossible, or at least would be very time consuming, since
I don't think I can reasonably provide much more information than what is already in the PR body above. Sorry if this means the issue will go unsolved. |
One thing I just thought of, if anyone is running functions remotely those functions need to be excluded from coverage. For example if using selenium-webdriver to run a function in the browser: /* istanbul ignore next */
browser.executeScript(() => console.log('this is run in the browser')); The way this works is This is the only tip I can give without seeing a demonstration of the problem. Absolutely nothing wrong with using istanbul to measure test coverage of proprietary software but if you need help then it is your responsibility to provide the necessary information. |
End to end steps to reproduce:
There should be a way to prevent this. Even if it is an inline comment or something. |
@robertleeplummerjr you need to add |
I am not going to fill my source code with a bunch of istanbul comments. I want code coverage, I just don't want it in this exact and specific context. That would be the equivalent of turning off code coverage. It would be easy to detect calls to |
istanbul does not support turning off coverage "in a specific context". Code is instrumented at the source level so in the example of a specific function it's either instrumented or it's not. Injecting source parsing and manipulation to the runtime so we can de-instrument code being passed to I feel like maybe c8 would be a better fit for your needs as it works without modifying the sources (so |
Ok. What we simply had an option to turn on checking if the value exists? Like: (typeof cov_id !== 'undefined' ? cov_id++ : undefined) This way when we're using |
Ty for pointing me to c8! |
Using Does c8 solve your issue? IMO istanbul cannot support the use case of not ignoring coverage on a function but then using |
For me, it solves the issue. |
I'm going to close this issue as out of scope. istanbuljs is unable to support the use case of evaluating / running Using c8 for situations where this is needed is my recommendation. |
In our case, we hit this case in a test-only code path, this simple hack seems to work: /**
* Hack to work around coveralls issue which prevents functions from being detached and evaluated in a new bound context.
*
* See https://github.com/istanbuljs/istanbuljs/issues/499
*
* @param fnSource Javascript function source to evaluate, usually sourced from `Function.toString()`
* @returns
*/
function removeCovCalls(fnSource: string) {
const regex = /\bcov_[^;,]+[;,]/g;
return fnSource.replace(regex, "");
} |
If you are using jest for your code coverage & babel-plugin-istanbul to instrument your code then disable babel-plugin-Istanbul for your test files [eg: index.test.jsx / index.test.js/ index.test.ts]. You can disable it in babel.config.js where you are configuring your Istanbul. babel.config.js
|
Context
At Tanda, we run tests on CircleCI as part of our deployment process. Recently when running with
--coverage
flag, our tests are only failing on CircleCI, not locally. @builtbyproxy and I spent a whole day walking through the process to identify what may throw the error mentioned below. These are our findings.Prerequisites
package.json versions
We touch:
istanbul-lib-instrument
babel-plugin-istanbul
babel-jest
istanbul-reports
Description
When running tests in our local env, everything passes as intended. However, when deployed to CircleCI, we get an error in the form of
ReferenceError: cov_14jyvcatev is not defined
. The id that comes after thecov_
changes depending on which file it is referring to. The file referred to by thecov_${id}
has (so far) never been the file that is being tested.This only happens on CircleCI, and when the jest
--coverage
flag is on.This also only happens to one test per test run. It's always the same test, and the cov_id is always the same (but does not refer to the test or the tested component that fails). For example, we test component B, and component J's cov_id will be the cause. We have had cases where the two components are completely unrelated.
Generating the cov_id
This
cov_${id}
is generated inistanbul-lib-instrument/dist/visitor.js
using the functiongenVar
.genVar
is called in the constructor for the VisitState class, and is assigned tothis.varName
. A VisitState object is created in the functionprogramVisitor
and used in theenter
andexit
functions, which is the default export foristanbul-lib-instrument/dist/visitor.js
The cov_id's journey
With our programVisitor default export we found it is imported in
istanbul-lib-instrument/dist/index.js
and exported again asprogramVisitor
.Then it is imported by
babel-plugin-istanbul/lib/index.js
and is given types, realPath, and other options. With the result being assigned tothis.__dv__
:Then it gets imported and exported by
babel-jest/build/index.js
and exports it as a part of the transformer (it's thebabelIstanbulPlugin
part)We logged
transformResult
and got a big string with two consistent values ofcode
andmap
.code
looked something like this:This gave us our first reference to the
cov_${id}
that was being used as a value, thus pointing to a potentialReferenceError: cov_id is not defined
.We searched our entire repo for instances of
code
being used and found the fileistanbul-reports/lib/html/annotator.js
running a map on thecode
variable after it's been split on newlines to generate an array of code lines. The istanbul-reports code looks like this:When running the same split on the same
code
string fromtransformResult
(as mentioned above), we got an array looking like this:Furthermore, when scrolling through we noticed instances of cov_id value being specifically mutated. This is the only place we can find reason for a
ReferenceError: cov_id is not defined
. (Pictured is the babel-plugin-graphql-tag import example, however the cov_id increments are inserted throughout our code as well.)istanbul-report
generates an interactive directory of coverage reports. In these reports, there is a counter beside each code line which indicates how often it's called in tests. The GUI for this looks like this:We believe that there is a race condition assigning the
cov_${id}
which isn't finishing before babel(or istanbul-reports or something?) tries to increment it.The text was updated successfully, but these errors were encountered: